GithubHelp home page GithubHelp logo

alttch / neotermcolor Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 0.0 18 KB

modern ANSII Color formatting for output in terminal

License: MIT License

Python 100.00%
terminal colorization colorful termcolor text-color linux windows

neotermcolor's Introduction

modern ANSII Color formatting for output in terminal

What is neotermcolor

neotermcolor library is a fork of old good termcolor, which is widely used. I like it very much, but unfortunately last release was long time ago.

Everything is fully backward compatible with original termcolor:

import sys
from neotermcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')

print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
print_red_on_cyan('Hello, Universe!')

for i in range(10):
    cprint(i, 'magenta', end=' ')

cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)

Installation

    pip3 install neotermcolor

New features

It works in Windows terminal

Yep, right out-of-the-box (tested on Windows 10)

It is readline-safe

When you mix ANSI color codes with readline input, it may cause a problem. neotermcolor has a workaround:

  • new param for cprint and colored: readline_safe=True
  • you may also turn on readline-safe colorizing by default, setting
    import neotermcolor

    neotermcolor.readline_always_safe = True

It has 256-color palette

If color code is specified as an integer (0..255), ANSI 256-color palette is used. You may specify color codes both for foreground and background and mix them with other attributes:

    from neotermcolor import cprint

    cprint('Underline light-green (119) on grey (237)', 119, 237, ['underline'])

It is TTY-aware

neotermcolor will not colorize text if process stdout or stderr is not a TTY.

This feature is on by default, but you may turn it off:

    import neotermcolor

    neotermcolor.tty_aware = False

It has palette overriding

You may define own color names or override existing ones: e.g. you may use standard palette for 16-color terminals, but override it when your program detect terminal with 256-color support or when it's forced by user:

    import neotermcolor

    neotermcolor.set_color('red', 197)
    neotermcolor.cprint('Red color is now purple', 'red')

It has styles

Styles are alternative to classical defining a "style" for certain type of messages with functools.partial or lambda. A style may contain color, on_color and attributes:

    import neotermcolor

    neotermcolor.set_style('error', color='red', attrs='bold')
    neotermcolor.cprint('ERROR MESSAGE', style='error')
    # or
    neotermcolor.cprint('ERROR MESSAGE 2', '@error')

Note: if you specify both style and e.g. attrs, the style attrs will be overriden.

Single attribute can now be specified as a string

    # as list or tuple
    cprint('test', attrs=['bold'])
    # as a string
    cprint('test', attrs='bold')

How to use it instead of old termcolor in the existing projects

    import neotermcolor as termcolor

I'll do my best to keep it backward compatible with original termcolor.

Enjoy!

neotermcolor's People

Contributors

divi255 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.