ANSI Color formatting for terminal output and log coloring
Project description
termcolor_dg
๐ฆ pypi/termcolor_dg | ๐ฆ pypi/termcolor | ๐ Color codes cheatsheet | ๐ ANSI/VT100 Terminal Control Escape Sequences
ANSI Color formatting for terminal output and log coloring. Supports 16 color, 256 color and 24-bit color modes.
Python 2 support is present for legacy projects and because it is not too much work and I have to use it for now.
Example
>>> from termcolor_dg import colored, cprint
>>>
>>> print(colored('Hello, World!', 'light_red', 'on_blue'))
>>> cprint('Hello, World!', 'blue', 'on_light_red', ['reverse', 'blink'])
>>>
>>> print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
>>> print_red_on_cyan('Hello, World!')
>>>
>>> for i in range(10):
>>> cprint(str(i), 'magenta', end=' ' if i != 9 else '\n')
>>>
>>> import sys
>>> cprint("Attention!", 'red', attrs=('bold'), file=sys.stderr)
>>>
>>> import logging
>>> from termcolor_dg import logging_basic_color_config
>>> logging_basic_color_config()
>>> logging.log(logging.INFO, 'test')
Colors demo screenshot (python -m termcolor_dg
):
Colored logs demo screenshot (python -m termcolor_dg logs
):
Environment variables
ANSI_COLORS_FORCE if set to anything, even empty string, color escape sequences will be added.
ANSI_COLORS_DISABLED if set to anything no coloring will be performed, overrides ANSI_COLORS_FORCE.
If none of the environment variables is set the colors are used
only if the stdout
is attached to a terminal: sys.stdout.isatty()
.
Text properties
Text colors | Text highlights | Alt Text highlights | Attributes |
---|---|---|---|
black | black | on_black | bold |
red | red | on_red | dark |
green | green | on_green | underline |
yellow | yellow | on_yellow | blink |
blue | blue | on_blue | reverse |
magenta | magenta | on_magenta | concealed |
cyan | cyan | on_cyan | |
light_grey | light_grey | on_light_grey | |
dark_grey | dark_grey | on_dark_grey | |
light_red | light_red | on_light_red | |
light_green | light_green | on_light_green | |
light_yellow | light_yellow | on_light_yellow | |
light_blue | light_blue | on_light_blue | |
light_magenta | light_magenta | on_light_magenta | |
light_cyan | light_cyan | on_light_cyan | |
white | white | on_white |
Terminal properties support
Terminal | bold | dark | underline | blink | reverse | concealed | 256 colors | 24-bit color |
---|---|---|---|---|---|---|---|---|
linux | โ[^3] | โ[^3] | โ [^3] | โ [^3] | โ | โ | โ | โ |
konsole | โ | โ | โ | โ | โ | โ | โ | โ |
terminator | โ | โ | โ | โ | โ | โ | โ | โ |
kitty | โ | โ | โ | โ | โ | โ | โ | โ |
xterm | โ | โ | โ | โ | โ | โ | โ | โ |
rxvt | โ | โ | โ | โ | โ | โ | โ | โ |
dtterm | โ | โ | โ | reverse | โ | โ | โ | โ |
teraterm | reverse | โ | โ | rev/red | โ | โ | โ | โ |
aixterm | normal | โ | โ | โ | โ | โ | โ | โ |
Windows | โ | โ | โ | โ | โ | โ [^4] | โ | โ |
PuTTY | โ [^2] | โ | โ | โ [^1] | โ | โ | โ | โ |
Cygwin SSH | โ | โ | color | color | color | โ | โ | โ |
Mac Terminal | โ | โ | โ | โ | โ | โ | โ | โ |
iTerm2 | โ | โ | โ | โ | โ | โ | โ | โ |
[^1]: Disabled by default
[^2]: Supports color change, bold text or both.
[^3]: See VGA text mode
[^4]: See Add support for the "concealed" graphic rendition attribute #6876
Similar projects
You may want to check out:
- ๐ฆ pypi/termcolor - the original project termcolor_dg extends
- ๐ฆ pypi/colorama for more advanced options.
Interesting links
- Terminal Colors discussion with examples
termcolor_dg changes
0.9.0 (2022-02-05)
- fork to termcolor_dg
- add 24-bit colors support
- add logging and color demos
- add ANSI_COLORS_DISABLED and ANSI_COLORS_FORCE environment variables
- add always_colored, rainbow_color
- add monkey_patch_logging and logging_basic_color_config utility functions
- better cprint and print compatibility (no arguments = new line)
- drop 'gray' and 'on_gray' (it was an alias for black, bad idea?)
- make trailing reset sequence optional for colored
python -m build
to build the project- remove regex usage, the following would have not been stripped but works:
echo -e '(\e[01;32mx\e[1;0033mx\e[m)'
- remove PKG-INFO
- README.md instead of README.rst
- ... unit tests, INSTALL ...
termcolor changes
1.9.1 (2021-12-15)
- python 2 and 3 compatibility, avoid KeyError
- make pylint happy
- fix character escapes
- better .gitignore
- fix ALL to all
1.1.0 (2011-01-13)
- Added cprint function.
1.0.1 (2011-01-13)
- Updated README.rst.
1.0.0 (2011-01-13)
- Changed license to MIT.
- Updated copyright.
- Refactored source code.
0.2 (2010-09-07)
- Added support of Python 3.x.
0.1.2 (2009-06-04)
- Fixed bold characters. (Thanks Tibor Fekete)
0.1.1 (2009-03-05)
- Some refactoring.
- Updated copyright.
- Fixed reset colors.
- Updated documentation.
0.1 (2008-06-09)
- Initial release.
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
Built Distribution
File details
Details for the file termcolor_dg-0.9.1.tar.gz
.
File metadata
- Download URL: termcolor_dg-0.9.1.tar.gz
- Upload date:
- Size: 213.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 567898af12026fa8c57d787f051ece1da5c6ceb39d2db4840cb1b18faf655f7e |
|
MD5 | 5bde9e05d54c416b5ca078c56989369a |
|
BLAKE2b-256 | b67ebb4f354a4bc1d307267f2f0b509f7403a07f6186db898d0ba75fb9a7b532 |
File details
Details for the file termcolor_dg-0.9.1-py2.py3-none-any.whl
.
File metadata
- Download URL: termcolor_dg-0.9.1-py2.py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 138d7c536734671f526e5b894a250effb3fb79801204c3bd1523a9052f214a95 |
|
MD5 | 7e5ff272d83def7669bb53b253537523 |
|
BLAKE2b-256 | 98cdc4783bdb32e7151142d17853bbc924ebc392dbe3e5ed77c681ff77bfc1f5 |