Package: ansicolors
ANSI colors for PythonAdd ANSI colors and decorations to your strings.Example Usagefrom __future__ import print_function  # accomodate Python 2
from colors import *

print(color('my string', fg='blue'))
print(color('some text', fg='red', bg='yellow', style='underline'))The strings returned bycolorwill have embeddedANSI code sequencesstipulating text colors and styles. For example, the above
code will print the strings:'\x1b[34mmy string\x1b[0m'
'\x1b[31;43;4msome text\x1b[0m'You can choose the foreground (text) color with thefgparameter,
the background color withbg, and the style withstyle.You can choose one of the 8 basic ANSI colors:black,red,green,yellow,blue,magenta,cyan, andwhite, plus a specialdefaultwhich is display-specific, but usually a rational “no special
color” setting.There are other ways to specify colors. Many devices support
an idiosyncratic 256-color scheme developed as an extension to
the original ANSI codes for thexterm terminal emulator.
Colors (or grays) from this larger palette can be specified viaintvalue (0-255).To see them all:from __future__ import print_function
from colors import color

for i in range(256):
    print(color('Color #%d' % i, fg=i))The includedshow_colors.pyprogram is a much-expanded version of this idea
that can be used to explore available color and style combinations on your
terminal or output device.24-bit Color and CSS CompatibilityModern terminals go even further than thexterm256, often supporting a
full 24-bit RGB color scheme. You can provide a full RGB value several ways:with a 3-elementtupleorlistofint, each valued 0 to 255 (e.g.(255, 218, 185)),a string containing a CSS-compatible color name (e.g.'peachpuff'),a string containing a CSS-style hex value (e.g.'#aaa'or'#8a2be2')a string containing a CSS-style RGB notation (e.g.'rgb(102,51,153)')These forms can be mixed and matched at will:print(color('orange on gray', 'orange', 'gray'))
print(color('nice color', 'white', '#8a2be2'))Note that any color name defined in the basic ANSI color set takes
primacy over the CSS color names. Combined with the fact that
terminals do not always agree which precise tone of blue should
qualify as ANSIblue, there can be some ambiguity regarding
the named colors. If you need full precision, specify the RGB
color exactly. Theparse_rgbfunction can be used to identify
the correct definition according to the CSS standards.CaveatsUnfortunately there is no guarantee that every terminal will support all the
colors and styles ANSI ostensibly defines. In fact, most implement a rather
small subset. Colors are better supported than styles, for which youmightget
one or two of the most popular such asboldorunderline.Might.Whatever colors and styles are supported, there is no guarantee they will be
accurately rendered. Even at this late date, overfifty yearsafter the codes
began to be standardized, support from terminals and output devices is limited,
fragemented, and piecemeal.ANSI codes evolved in an entirely different historical context from today’s.
Both the Web and the idea of broad standardization were decades in the future.
Display technology was low-resolution, colors were limited on the rare occasions
they were present, and color/style fidelity was not a major consideration.
Vendors thought little or nothing of creating their own proprietary codes,
implementing functions differently from other vendors, and/or co-opting codes
previously in use for something else. Practical ANSI reference materials
includemanyphrases such as ‘hardly ever supported’ and ‘non-standard.’We still use ANSI codes today not because they’re especially good, but because
they’re the best, most-standard approach that pre-Web displays even remotely
agreed upon. Even deep into the Web era, text output endures as an important
means of human-computer interaction. The good news, such is it is: ANSI’s color
and style specifications (“SGR” or “Select Graphic Rendition” in ANSI
terminology) are the most-used and best-adhered-to portion of the whole ANSI
show.More Examples# use some partial functions

from __future__ import print_function # so works on Python 2 and 3 alike
from colors import red, green, blue

print(red('This is red'))
print(green('This is green'))
print(blue('This is blue'))Optionally you can add a background color and/or styles.:print(red('red on blue', bg='blue'))
print(green('green on black', bg='black', style='underline'))You can use multiple styles at once. Separate them with
a+.:print(red('very important', style='bold+underline'))You can additionally specify one of the supported styles:none,bold,faint,italic,underline,blink,blink2,negative,concealed,crossed. While most devices support only a few styles,
unsupported styles are generally ignored, so the only harm done is your text is
less pretty and/or formatted than you might like. A good general rule is
to enjoy the formatting if you get it, but don’t depend on it–especially
don’t depend on styles likeblink(e.g. to highlight critical data) orconcealed(e.g. to hide data). Most likely, they won’t.If you use a style often, you may want to create your own
named style:from functools import partial
from colors import color

important = partial(color, fg='red', style='bold+underline'))

print(important('this is very important!'))Utility FunctionsIn deailing with ANSI-styled text, it can be necessary to determine the
“equivalent” text minus the styling. The functionstrip_color(s)does that,
removing ANSI codes froms, returning its “plain text equivalent.”You may also wish to determine the effective length of a string. If it contains
ANSI codes, the builtinlen()function will return the length including
those codes, even though they are logically 0-length. So plainlen(s)is
probably not what you need.ansilen(s)in contrast returns the “effective”
length of the string, including only the non-ANSI characters.ansilen(s)is
equivalent tolen(strip_color(s)),Licensecolorsis licensed under theISC license.