3

Perl has the excellent module Term::ANSIScreen for doing all sorts of fancy cursor movement and terminal color control. I'd like to reimplement a program that's currently in Perl in Python instead, but the terminal ANSI colors are key to its function. Is anyone aware of an equivalent?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Kamil Kisiel
  • 19,723
  • 11
  • 46
  • 56

4 Answers4

8

If you only need colors You may want to borrow the implementation from pygments. IMO it's much cleaner than the one from ActiveState

http://dev.pocoo.org/hg/pygments-main/file/b2deea5b5030/pygments/console.py

Jorge Vargas
  • 6,712
  • 7
  • 32
  • 29
3

There is also the termcolor and the termstyle packages. The latter is capable of disabling colour output if stdout is not a terminal.

See also this question.

Community
  • 1
  • 1
Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
3

Here's a cookbook recipe on ActiveState to get you started. It covers colors and positioning.

[Edit: The pygments code submitted above by Jorge Vargas is a better approach. ]

Jeff Bauer
  • 13,890
  • 9
  • 51
  • 73
  • Perfect. This is exactly what I was looking for. Now I just have to bundle it up in to a nice module and clean up the code a bit. Thanks. – Kamil Kisiel Jan 23 '09 at 05:30
2

While I haven't used it myself, I believe the curses library is commonly used for this:

http://docs.python.org/library/curses.html

And the How-to:

http://docs.python.org/howto/curses.html#curses-howto

Unfortunatly, this module doesn't appear to be available in the standard library for windows. This site apparently has a windows solution:

http://adamv.com/dev/python/curses/

monkut
  • 42,176
  • 24
  • 124
  • 155
  • Not quite the same I think. You need to make a "window" with curses, whereas with Term::ANSIScreen I can print an arbitrary number of lines to the terminal using the standard print function. – Kamil Kisiel Jan 23 '09 at 00:56
  • Yeah, it doesn't look like it's meant to deal with the current terminal. It looks like Jeff may have what your looking for. – monkut Jan 23 '09 at 01:35