tags:

views:

398

answers:

4

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?

+2  A: 

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
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
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
+3  A: 

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

Jeff Bauer
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
+3  A: 

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
Very nice. Much nicer than the code on ActiveState.
Kamil Kisiel
great, glad you liked it. Thanks for the best answer!
Jorge Vargas
+1  A: 

There is also the termcolor package.

See also this question.

Olivier