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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top