Painter is a library for colorizing the output text with style modifiers. You could change the foreground and the background colors as well as the text styles, such as bold or italic.
The following example shows how it is used:
from painter import paint # Simple printing of colors print('Welcome to Painter!', paint.red('I can paint things red'), paint.blue('and blue')) # Chaining colors and styles print(paint.blue.on_red.bold.underline('and far more complex combos too')) print() # Nested painting print(paint.on_red('I can also use a background color across', paint.blue('multiple'), paint.yellow('foreground colors'))) # Custom separator print(paint('and', 'allow', 'you to use', paint.red('custom separators'), sep='-')) print() # Creating themes cool_theme = paint.green.on_red.underline.bold print('Creating', cool_theme('your own theme'), 'is easy') # Easily disable painting of colors paint.enabled = False print('and I allow you to easily', paint.red('disable'), paint.blue('me')) print() paint.enabled = True print('Hope you have a', paint.blue('lovely day!'), paint.green(':)'))
The rendered text in terminal emulator looks like, in rxvt-unicode, white-on-black, with standard colors:
The usage is kind of interesting, sort of jQuery-like:
paint.blue.on_red.bold.underline('text')
You can chain colors and styles, or create themes for reuse, which essentially is just a reference of the chained functions:
theme_bg = paint.on_white theme_text_a = paint.red.bold theme_text_b = paint.blue.underline print(theme_bg(theme_text_a('Hello'), theme_text_b('world!')))
You can also disable or enable the coloring at any time you like:
paint.enabled = False
It also provide a stripping function and command-line tool for color code stripping, for example:
$ cat <in-file> | python -m painter.strip_color $ python -m painter.strip_color <in-file> <out-file>
This strips all ANSI color escape codes.
Painter is written by Fotis Gimian, under the MIT License, currently version 0.2 (2014-04-26), for both Python 2 and 3 plus PyPI implementation.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.