ANSIColors brings a simple and powerful way to use colours in a terminal.
Provide efficient and useful functions to use colors, in a HTML-tag style.
Almost all ANSI Colors are defined with this tags-like style. This point is the main interest of this module, because many others define functions to print with some colors.
The reset tag is a special one to reinitialize all previously changed parameters.
You can choose one of the 8 basic ANSI colors: black, red, green, yellow, blue, magenta, cyan, white. The names beginning with a lower-script design foreground colors.
ANSIColors.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 a upper-script design background colors.
ANSIColors.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 gives a colored ! and ?.
Writing to a file
This is possible with the writec function. For example
import sys ANSIColors.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 doesn’t support them (e.g. a file or a piped command).
It works perfectly on GNU/Linux (tested on Ubuntu, Arch-Linux, Debian, Fedora and Angstrom) and Windows (with OR without Cygwin), and have not be tested on MAC OS X (but it should be alright).
This module provides also the xtitle function, to change the title of the terminal. It tries to use the command xtitle, and if not available, tries to use one ANSI Code to change the title.
This module provides a notify function to display a system notification (using notify-send on Debian-like, not working on Windows).
ANSIColors.py is also a script. You can have his description (or use it) directly with
python -m ANSIColors --help
It can be used to run some tests (with the –test option).
It 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.
An up-to-date version of this complete color profile can be found on my webpage.
This module is licensed under the term of the GNU Public License, version 3 (GPLv3). See the file LICENSE for more details.