fin.color

Colorful console output not only makes output look prettier, it can significantly help with readability.

This module provides a simple wrapper that makes outputting colored text really simple:

C.red("this is red") + C.blue.bold("this is blue and bold")

Code Docs

fin.color.C

An instance of Color that is most appropriate for sys.stdout. Typically this will be a VtColor object, but when stdout is redirected to a file, or other non-terminal output then it will revert to NoColor.

This constant allows code to simply refer to, for example:

>>> fin.color.C.blue('hi')
'\x1b[34mhi\x1b[0m'
class fin.color.Color(parts=())[source]

Bases: object

A simple class for producing pretty terminal output. While Color is abstract, VtColor provides common VT-100 (xterm) compatible output. This is a very light, small library, and doesn’t deal with curses or terminfo.

The module global C is created at import time. If standard out appears to support color output, then this will be an instance of VtColor, otherwise, NoColor.

Typical Usage:

c = fin.color.C
print c.blue + "I'm blue, da-ba-dee da-ba-dai" + c.reset
print c.red.bold("Color") + c.red.blue("Blind")
print c.green("In") + c.bg_green.black("verse") # Note assumes a white-on-black color scheme.
class fin.color.NoColor(parts=())[source]

Bases: fin.color.Color

A color class for when no color information should be output. For example, if output can be redirected to a terminal OR a log file, NoColor can be transparently swapped in for the VtColor class to ensure that the data in the log file does not include a large number of escape codes.

class fin.color.VtColor(parts=())[source]

Bases: fin.color.Color

The color class for VT-compatible terminals (basically all non-windows terminals).

The color attributes may be used in two ways. Getting a color is a matter of referencing the correct attribute:

>>> C.red
<fin.color.VtColor at 0x7f904012fb90>

This may then be converted to a string, for printing:

>>> str(C.red)
'\x1b[34m'
>>> C.red + "foo" + C.reset
'\x1b[31mfoo\x1b[0m'

The alternate syntax is to call the color, passing in a string, which implicitly add the color code before the string, and adds a reset afterwards (NOTE: the reset will reset all color information, including any inherited):

>>> C.red("hello") + C.blue("world")
'\x1b[31mhello\x1b[0m\x1b[34mworld\x1b[0m'

Printing any of the above in a standard (non-windows) terminal, will result in the correct colored output.

The available colors are listed below. All colors may be prefixed with ‘bg_‘, and colors may be combined by further attribute access:

>>> C.bg_blue.white.bold("Bold, white-on-blue text")
'\x1b[44;37;1mBold, white-on-blue text\x1b[0m'

Two special attributes: ‘bold’, and ‘reset’ respectively turn the text bold (or use bright colors, depending on console) and reset all color attributes.

COLORS = ['black', 'red', 'green', 'yellow', 'blue', 'purple', 'cyan', 'white']

All colors that may be referenced

EXTRA = {'reset': 0, 'bold': 1}

Non-color attributes

fin.color.auto_color(stream=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>)[source]

This does some simple tests to determine if the output stream supports colors, returning the corect Color class for the stream.

The lookup is intentionally kept simple, as this has proved to capture 99% of cases without adding the burden of more complicated capabilities databases.