Skip to main content

ansicolortags brings a simple and powerful way to use colours in a terminal application with Python 2 and 3.

Project description

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.

The complete documentation can be found here : http://ansicolortags.readthedocs.io/ !

Colours

Foregrounds

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.

For example:

from ansicolortags import printc
printc('<reset>This is default. <red>This is red<yellow> and yellow in foreground now<reset>').

Backgrounds

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.

For example:

from ansicolortags import printc
printc('<Default>this is default. <Blue>this have a blue background<Black> and black in background now<reset>').

Other tags

The following tags are also available:

  • b, B : to turn on and off the bold mode,

  • u, U : to turn on and off the underline mode,

  • neg, Neg : to turn on and off the reverse video mode,

  • blink, Blink : to turn on and off the blink mode,

  • el : to erase the current line,

  • bell : to make the terminal ring.

Shortcuts

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.

Auto detection

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.

Other features

Other functions

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).

Script

ansicolortags.py is also a script. You can have his description (or use it) directly with:

python -m ansicolortags --help

For testing

ansicolortags.py can be used to run some tests (with the --test option).

With GNU/Bash

ansicolortags.py can be used to generate a GNU/Bash color profile (with the --generate --file color.sh options).

See here for this color.sh file

This sh file can be imported with $ . color.sh in any GNU/Bash scripts, or even in your ~/.bashrc file.

License ?

This module is licensed under the term of the MIT License, see the file LICENSE for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansicolortags-0.4.zip (19.0 kB view details)

Uploaded Source

File details

Details for the file ansicolortags-0.4.zip.

File metadata

  • Download URL: ansicolortags-0.4.zip
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ansicolortags-0.4.zip
Algorithm Hash digest
SHA256 76a507994ac26be993b468e27a1b7715bf9151d034c812538fed446a4c8ee990
MD5 6637a270242bfe0ac14b313ceb193ff0
BLAKE2b-256 e1226a383bebee83d76c00fb858caf1ead6dfde02f5dcea42e0d194050f0e8a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page