Skip to main content

Fork of termcolor library

Project description

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neotermcolor-2.0.11.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neotermcolor-2.0.11-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file neotermcolor-2.0.11.tar.gz.

File metadata

  • Download URL: neotermcolor-2.0.11.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for neotermcolor-2.0.11.tar.gz
Algorithm Hash digest
SHA256 9a5a1d4d6987278f459aed25d1d60ff811478a8fe1ae0692c66875f250e82e6e
MD5 2fdd6e69de748850e8101b5eb42bb87f
BLAKE2b-256 9d949a56224c4aeb8c528cd5b71349c6ff234f73aa3640f52fd0688060642bb7

See more details on using hashes here.

File details

Details for the file neotermcolor-2.0.11-py3-none-any.whl.

File metadata

  • Download URL: neotermcolor-2.0.11-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for neotermcolor-2.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 2ec7195505cc27e0c8ae36eda3523e2410b81f1e2ff018dc60e16a9a85572585
MD5 8a80aad0b4ce741a257f4e1f5d7444b1
BLAKE2b-256 92c30e36d165ec1c07b8bf840412ea8da8e224d001cbf86e6bdc710f7309774b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page