ansicolortags brings a simple and powerful way to use colours in a terminal application with Python 2 and 3.
The ansicolortags module provides efficient and useful functions to use colours in a terminal application with Python 2 and 3, with a HTML-tag like style : <red>text<white> will print text in red.
All ANSI colors code are defined with this tag-like style. This point is the main interest of this module, because all others modules define function to print with some colours.
You can choose one of the 8 basic ANSI colours: black, red, green, yellow, blue, magenta, cyan, white. The names beginning with a lower-script design foreground colours.
from ansicolortags import printc printc('<reset>This is default. <red>This is red<yellow> and yellow in foreground now<reset>').
You can choose one of the 8 basic ANSI colours: Black, Red, Green, Yellow, Blue, Magenta, Cyan, White. The names beginning with an upper-script design background colors.
from ansicolortags import printc printc('<Default>this is default. <Blue>this have a blue background<Black> and black in background now<reset>').
Some macros are also provided, like the tags <ERROR>, <INFO> or <WARNING>.
And also <warning> and <question>, which respectively give a colored ! and ?.
The reset tag is a special tag to reinitialize all previously changed parameters.
Writing to a file ?
This is possible with the writec function. For example:
import sys from ansicolortags import writec writec('<ERROR><u><red>The computer is going to explode!<reset>', fn=sys.stderr) # sys.stderr.flush() # this is useless : writec flush itself.
Of course, the colors are disabled if the output does not support them.
It works perfectly on any GNU/Linux (tested with Ubuntu 10+, Debian, Arch Linux) and Windows (with or without Cygwin), and should work fine one MAC OS X or on other UNIX-like.
There is also the xtitle() function, to change the title of the terminal. This try to use the command-line tool xtitle, and if it fails it tries to use an ANSI code to change the title.
There is also a notify() function to display a system notification (using the command-line tool notify-send).
ansicolortags.py is also a script. You can have his description (or use it) directly with:
python -m ansicolortags --help
ansicolortags.py can be used to run some tests (with the --test option).
ansicolortags.py can be used to generate a GNU/Bash color profile (with the --generate --file color.sh options).
This sh file can be imported with $ . color.sh in any GNU/Bash scripts, or even in your ~/.bashrc file.
This module is licensed under the term of the MIT License, see the file LICENSE for more details.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.