Package: termcolor-dg
termcolor_dg📦pypi/termcolor_dg|
📑ANSI escape code|
📑Color codes cheatsheetANSI Color formatting for terminal output and log coloring. Supports 16 colors, 256 colors 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.Examplefromtermcolor_dgimportcolored,cprintprint(colored('Hello, World!','light_red','on_blue'))cprint('Hello, World!','blue','on_light_red',['reverse','blink'])print_red_on_cyan=lambdax:cprint(x,'red','on_cyan')print_red_on_cyan('Hello, World!')foriinrange(10):cprint(str(i),'magenta',end=' 'ifi!=9else'\n')importsyscprint("Attention!",196,attrs='bold',file=sys.stderr)# 256 color modecprint("Attention!",(255,0,0),attrs='bold',file=sys.stderr)# 24-bit color modeimportloggingfromtermcolor_dgimportlogging_basic_color_configlogging_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 variablesANSI_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, overridesANSI_COLORS_FORCE.NO_COLOR:
If set to anything no coloring will be performed, overridesANSI_COLORS_FORCE,
seehttps://no-color.org/.If none of the environment variables is set the colors are used
only if thestdoutis attached to a terminal:sys.stdout.isatty().Text propertiesText colorsText highlightsAlt Text highlightsAttributesblackblackon_blackboldredredon_reddarkgreengreenon_greenunderlineyellowyellowon_yellowblinkblueblueon_bluereversemagentamagentaon_magentaconcealedcyancyanon_cyanlight_greylight_greyon_light_greydark_greydark_greyon_dark_greylight_redlight_redon_light_redlight_greenlight_greenon_light_greenlight_yellowlight_yellowon_light_yellowlight_bluelight_blueon_light_bluelight_magentalight_magentaon_light_magentalight_cyanlight_cyanon_light_cyanwhitewhiteon_whiteFunctionsAdding ANSI colorsalways_colored(text, color=None, on_color=None, attrs=None, reset=True):   Returns the text with ANSI color code in front and ANSI color reset after. Arguments:
:   -textis the text to add color to
:   -colorandon_colordefine the character and background color. Each can be a name for 16 color mode, number for the 256 color variant and list/tuple of R, G and B for the 24 bit color support. The following all define light red:'light_red',9,196and(255, 0, 0).
:   -attrsattributes, single attribute name, list or tuple of attributes.
:   -resetif set toFalsewill suppress adding of the reset ANSI sequence at the end. Useful if you are joining another colored string, only the last reset is really needed.colored(text, color=None, on_color=None, attrs=None, reset=True):   same as always_colored but checks if the app is running on a terminal and if the colors have been forced or disabled. The booleantermcolor_dg.DISABLEDis the variable checked.Utility functionscprint(text='', color=None, on_color=None, attrs=None, **kwargs):   same as colored, but prints the resulting string instead of returning it.rainbow_color(n, steps, nmax=255):   calculates the color at stepnneeded to produce a rainbow instepssteps with intensity up tonmax. Ex:rainbow_color(0, 120)returns(255, 0, 0),rainbow_color(30, 120)returns(128, 255, 0).monkey_patch_logging:   Monkeypatchlogging.Formatterto add colors to the logs on a terminal, not customizable for now. Monkeypatchlogging.LogRecord.getMessageto log formatting errors in the log format instead of throwing useless exception.logging_basic_color_config(level='DEBUG', fmt='%(asctime)s %(message)s  # %(filename)s:%(lineno)d %(name)s', color_on_terminal=True):   Basic logging configuration, coloring and report formatting errors (monkey_patch_logging). Coloring code relies on '  # ' to determine the section to print in pale dark blue (comment).monkey_unpatch_logging:   Undo the monkeypatching done bymonkey_patch_loggingand remove coloring and error handling.COLOR_RESET_STR:   The color and attribute ANSI reset code.Similar projectsYou may want to check out:📦pypi/termcolor- the original project📦delameter/termcolor- fork of the original termcolor_dg extends📦pypi/coloramafor more advanced options.Interesting links📑Terminal Colorsdiscussion with examples📑ANSI/VT100 Terminal Control Escape Sequences📑ANSI escape code📑Color codes cheatsheet📑MarkdownGuide - Basic SyntaxTerminal properties supportAssume this information is outdated.Terminalbolddarkunderlineblinkreverseconcealed256 colors24-bit colorlinux❌[^3]❌[^3]✅[^3]✅[^3]✅❌❌❌konsole✅✅✅✅✅✅✅✅terminator✅✅✅✅✅✅✅✅kitty✅✅✅✅✅❌✅✅xterm✅❌✅✅✅✅✅✅rxvt✅❌✅✅✅❌✅❌dtterm✅✅✅reverse✅✅❓❓teratermreverse❌✅rev/red✅❌❓❓aixtermnormal❌✅❌✅✅❓❓Windows❌❌❌❌✅✅[^4]❓✅PuTTY✅[^2]✅✅✅[^1]✅❌✅✅Cygwin SSH✅❌colorcolorcolor✅❓❓Mac Terminal✅❓✅✅✅✅✅❓iTerm2✅❓✅✅✅✅✅✅[^1]: Disabled by default[^2]: Supports color change, bold text or both.[^3]: SeeVGA text mode[^4]: SeeAdd support for the "concealed" graphic rendition attribute #6876termcolor_dg changes1.0.0 (2023-05-25)fix DISABLED detectionlogging.basicConfig has no disable_existing_loggers argument.move back to GitHub, workflows does work and is free.0.9.3.1 (2022-02-14)Adjust the log color scheme.pylint fixes in the color demo.0.9.3 (2022-02-14)Adjust the log color scheme.pylint fixes in the color demo.0.9.2 (2022-02-07)Combine color, background and attributes in a single escape sequence.Minor enhancements.0.9.1 (2022-02-05)Rename to termcolor_dg (termcolor2 is taken).0.9.0 (2022-02-05)Fork to termcolor2.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 the trailing reset sequence optional for colored.Usepython -m buildto 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 changes1.9.1 (2021-12-15)python 2 and 3 compatibility, avoid KeyErrormake pylint happyfix character escapesbetter .gitignorefixALLtoall1.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 for 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.