[Q] Printing color strings on stdout without using escape sequences ?

Michael Hudson mwh at python.net
Fri Nov 16 12:39:30 CET 2001


Ivan DUTKA-MALEN <dutka at edf.fr> writes:

> Hello,
> 
> for my application to be a bit more friendly, I want to show some
> colored strings on the standard output but I don't want to use ugly
> escape sequence like '\033[1m;' or so...
> 
> I'd prefer use a module (if one exists) that encapsulate all those
> things and that allows me to be portable on various Unix-like systems
> (especialy Linux, Solaris, HP-UX, IRIX).
> Does anyone know such a module ?
> 
> For information, I tried to use curses but I think it doesn't make
> what I want in the sense that it uses the full screen and I just want
> to use the standard output stream. I may be wrong and I surely misuse
> it. Some help will be much appreciated :-)).

You can use bits of the curses module -- setupterm, tigetstr, tparm,
putp, specifically.

Try this:

>>> import curses
>>> curses.setupterm()
>>> curses.putp(curses.tparm(curses.tigetstr("setaf"), 1)); \
... print "this is in red"
this is in red

You'll need Python 2.1 or newer, and you'll probably be wanting to
wrap this up in something nicer.

HTH,
M.

-- 
  The meaning of "brunch" is as yet undefined.
                                             -- Simon Booth, ucam.chat



More information about the Python-list mailing list