An efficient and simple ANSI colors module (and also a powerfull script), with functions to print text using colors.
- the name in minuscule is for color with bold (example ‘yellow’),
- the name starting with ‘B’ is for color without bold (example ‘Byellow’),
- the name starting with a capital letter is for the backgroung color (example ‘Yellow’).
- the name in minuscule is for activate the effect,
- the name starting in capital letter is for desactivate the effect.
- sprint: give a string,
- printc: like __builtin__.print, but with interpreting balises to put colors,
- writec: like printc, but using any file object (and no new line added at the end of the string),
- erase: erase all ANSI colors balises in the string (like sprint, but erasing and not interpreting color balises)
- clearLine, clearScreen: to clear the current line or screen,
- Reset: to return to default foregroung and backgroung, and stopping all fancy effects (like blinking or reverse video).
- notify: try to display a system notification. Only on *linux*.
- xtitle: try to set the title of the terminal. Not always supported.
To store a string, use sprint (i.e. print to a string, sprint), like here
>>> example=sprint("France flag is <blue>blue<white>white<red>red<white>, Iran flag if <green>green only<white>.")The string example can then be printed, with colors, with
>>> print example # Sorry, but in the documentation it is hard to show colors :) France flag is bluewhitered, Iran flag if green only.To directly print a string colored by balises, use printc
>>> printc("Batman's costum is <black>black<white>, Aquaman's costum is <blue>blue<white> and <green>green<white>.")See also
This is the most usefull function. To do the same, but on any file, use writec
Moreover, the function erase can also be usefull to simply delete all valid color balises
>>> print erase("Batman's costum is <black>black<white>, Aquaman's costum is <blue>blue<white> and <green>green<white>, and this is a non-valid <balise>, so it is kept like this.") Batman's costum is black, Aquaman's costum is blue and green, and this is a non-valid <balise>, so it is kept like thisIn this last example, <el> balise is used to erase the current content of the line, usefull to make a dynamical print
>>> writec("<red>Computing <u>2**(2**(2**4))<reset>...."); tmp=2**(2**(2**4)); writec("<el><green> Done !<reset>") Done !The first ‘Computing 2**(2**(2**4))....’ have disappeared after the computation !
- To show the help ::
$ ANSIColors.py –help
- To run a test ::
$ ANSIColors.py –test
- To produce a GNU Bash color aliases file ::
$ ANSIColors.py –generate –file ~/.color_aliases.sh
- Now, this script can detect if ANSI codes are supported :
- $ ANSIColors.py –help : will print with colors if colors seems to be supported;
- $ ANSIColors.py –help –noANSI : will print without any colors, even if it is possible;
- $ ANSIColors.py –help –ANSI : will print without colors, even they seems to be not supported.
And, the module part behave identically.
This module is concluded. The reference page for ANSI code is : here on Wikipedia. The reference page for XTitle escape code is : here.
(c) October 2012 - February 2013 By Lilian BESSON, ENS de Cachan (M1 Mathematics & M1 Computer Science MPRI) mailto:lbesson@ens-cachan.fr
For Naereen Corp. mailto:naereen-corporation@laposte.net https:sites.google.com/site/naereencorp
If false, the module do almost NOTHING
Black and bold.
Red and bold.
Green and bold.
Yellow and bold.
Blue and bold.
Magenta and bold.
Cyan and bold.
White and bold.
Black and not bold.
Red and not bold.
Green and not bold.
Yellow and not bold.
Blue and not bold.
Magenta and not bold.
Cyan and not bold.
White and not bold.
Black backgroung
Red backgroung
Green backgroung
Yellow backgroung
Blue backgroung
Magenta backgroung
Cyan backgroung
White backgroung
Make the text blink. NOT SUPPORTED BY ALL TERMINAL. On Windows (with mintty) it’s ok. On Linux (with ttys, gnome-terminal or pyterminal, it’s not).
Make the text not blink (i.e. stop blinking).
default foregroung
default backgroung
italic
no italic
bold
no bold
underline
no underline
negative
no negative
Clear the screen.
Clear the current line.
Reset the current foregroung and backgroung values to default, and disable all effects.
BEL is the bell character (007). It might be interpreted and a sonor signal might be heard (but not with every terminals).
Use it like : writec(“<title>.: My title :.<bell>”), and only with ending the sequence with <bell>.
A well colored Warning symbol (/!\)
A well colored question symbol (/?\)
A well colored ERROR word.
A well colored WARNING word.
A well colored INFO word.
List of all authorized colors.
List of all simple colors
Convert a string to a color. [string] have to be in [colorList] to be recognized (and interpreted). Default value if [string] is not one of the color name is “” the empty string.
Parse a string containing color balises, when color is one of the previous define name, and then return it, with color balises changed to concrete ANSI color codes.
Balises are delimited by [left] and [right]. By default, it’s Pango style whit ‘<’ and ‘>’, but you can change them.
For example, a HTML style like : left=’<span color=’ and right=’>’ is also possible. (But, without closing ‘</span’, this is a stupid example. Sorry I didn’t find anything else...)
Warning
It is more prudent to put nothing else than ANSI Colors (i.e. values in colorList) between ‘<’ and ‘>’ in [chainWithBalises]. The comportement of the function in case of false balises is not perfect. Moreover, a good idea could be to don’t try to use ‘<’ or ‘>’ for anything else than balises.
I know, it’s not perfect. But, the syntax of color balises is so simple and se beautiful with this limitation that you will surely forgive me this, won’t you ;) ?
>>> print sprint("<blue>this is blue.<white>And <this> is white.<red>Now this is red because I am <angry> !<green><white>")
this is blue.And <this> is white.Now this is red because I am <angry> !
Parse a string containing color balises, when color is one of the previous define name, and then return it, with color balises erased.
>>> print erase("<blue>This is blue.<white>And <this> is white.<red>Now this is red because I am <angry> !<reset>")
This is blue.And <this> is white.Now this is red because I am <angry> !
A shortcut to print sprint(chainWithBalises) : analyse all balises, and print the result.
Also usefull to print colored text, but without an ending ‘n’ caracter.
Example:
- In this example, before the long calcul begin, it print ‘Computing 2**(2**(2**4)).....’,
- and when the computation is done, erase the current line (with <el> balise), and print ‘ Done !’ in green, and the result of the computation.
>>> writec("<red>Computing<reset> 2**(2**(2**4))....."); tmp=2**(2**(2**4)); writec("<el><green>Done !<reset>")
- This example show how to use ANSIColors module to put colors data in a file.
- Be aware that this file now contains ANSI escape sequences. For example, $ cat /tmp/colored-text.txt * will well print the colors, but editing the file will show *hard values of escape code (you know, the stuff that you typically don’t want to know anything, the **dirty stuff* !*).
>>> my_file = open('/tmp/colored-text.txt', mode='w') # Open an adhoc file. >>> write("<blue>this is blue.<white>And <this> is white.<red>Now this is red because I am <angry> !<green><white>", file=my_file)
>>> writec("<reset>")
Warning
The file file will be flushed by this function if flush is set to True (this is default comportement). If you prefer no to, use flush=False option
>>> writec(chainWithBalises_1), file=my_file, flush=False)
>>> # many things.
>>> writec(chainWithBalises_n), file=my_file, flush=False)
>>> my_file.flush() # only flush *here*.
notify(msg=””, obj=”.: Notification sent by ANSIColors.notify :.”, icon=None, verb=False): -> True|False Notification using subprocess and notify-send.
Also print the informations directly to the screen (only if verb=True).
Warning
This doesn’t use any ANSI escape codes, but the common notify-send linux program. It shall fails (but not durty) on Windows or Mac OS X.
Return True iff the title have been correctly changed. Fails simply if notify-send is not found.
An experimental try is with ANSI escape code, if the simple way by invoking the xtitle program doesn’t work (or if it is not installed).
Note
The second solution used the two ANSI Balises <title> and <bell>. So, you can also do it with :
>>> ANSIColors.writec("<title>.: This is the new title of the terminal :.<bell>")
But this function xtitle is better : it tries two ways, and returns a signal to inform about his success.
The file is a list of ‘export NAME=”VALUE”’, to be used with GNU Bash.