ansicolor

Add ANSI escape codes to a given string to print styled text in consoles and terminal emulators.

The main function with which about anything can be done is colored(). Almost any combination of font color, background color and different styles (highlighted, underlined, etc.) can be defined using keyword arguments.

For convenience, there exist some functions with an even shorter syntax: red(), green(), etc. to just color the text, hred(), hgreen(), etc. to get highlighted colored text, ured(), ugreen(), etc. to get underlined colored text. To just change the style of the text and not its color, the functions highlight(), underline(), invert(), conceal(), blink(), and normal() are provided.

List of colors: gray, red, green, yellow, blue, purple, cyan, lightgray

List of styles: normal, highlight, underline, blink, invert, conceal

Courtesy goes to: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

Notes

  1. The blinking style probably won’t work in most terminal emulators.
  2. As far as I know, ANSI escape codes are not interpreted under the Windows operating system.

Examples

>>> highlight('Welcome to the ansicolor module')
Welcome to the ansicolor module
>>> red('This is some red text.')
This is some red text.
>>> ublue('And this is blue and underlined. Awesome!')
And this is blue and underlined. Awesome!
>>> hgray('Finished.')
Finished.

To see the results as rendered in your terminal emulator, call the test program by executing this module as a script:

python ansicolor/__init__.py

Functions

ansicolor.colored(string, color=None, style=None, bg=None, revert=True)

Add ANSI escape sequences to a given string to display it in a certain color and style in a console or terminal emulator. Also the background color bg can be set separately. If revert is True, return to normal style at the end of the string (default). In style, multiple comma-separated styles may be specified.

List of supported colors: gray, red, green, yellow, blue, purple, cyan, lightgray

List of supported styles: normal, highlight, underline, blink, invert, conceal

ansicolor.test()

Test program to demonstrate the features of the ansicolor module.

Styles

ansicolor.normal(string)
ansicolor.highlight(string)
ansicolor.underline(string)
ansicolor.invert(string)
ansicolor.conceal(string)

Colors

ansicolor.gray(string)
ansicolor.red(string)
ansicolor.green(string)
ansicolor.yellow(string)
ansicolor.blue(string)
ansicolor.purple(string)
ansicolor.cyan(string)
ansicolor.lightgray(string)

Highlighted colors

ansicolor.hgray(string)
ansicolor.hred(string)
ansicolor.hgreen(string)
ansicolor.hyellow(string)
ansicolor.hblue(string)
ansicolor.hpurple(string)
ansicolor.hcyan(string)
ansicolor.hlightgray(string)

Underlined colors

ansicolor.ugray(string)
ansicolor.ured(string)
ansicolor.ugreen(string)
ansicolor.uyellow(string)
ansicolor.ublue(string)
ansicolor.upurple(string)
ansicolor.ucyan(string)
ansicolor.ulightgray(string)

Indices and tables

Table Of Contents

This Page