Is there a Term::ANSIScreen equivalent for Python?

Question:

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?

Asked By: Kamil Kisiel

||

Answers:

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/

Answered By: monkut

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

Answered By: Jeff Bauer

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

Answered By: Jorge Vargas

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.

Answered By: Olivier Verdier