curses and processing terminal escape characters

Tim Harig usernet at ilthio.net
Fri Oct 29 19:35:37 CEST 2010


On 2010-10-29, mix <tmmikolajczyk at gmail.com> wrote:
> Hi,
>
> I'm wondering if there is a way in python to process a string
> containing terminal escape characters. Example: Please consider the
> following string:

Python could easily process the escape codes for any given terminal; but,
in general, you would want something that works for more then a single
terminal type.  This means that you need something that can do a reverse
translation of the termcap/terminfo database.

What you might be able to do, is build a dictionary by querying all of the
possible termcap/terminfo functionalities using tgetstr().  Then when you
encounter an escape sequence, you can use the escape sequence from the
dictionary as a template to parse the sequence.

> str = ''aaaa\x1B[K\x1B[D\x1B[D\x1B[D\x1B[D\x1B[C\x1B[C\x1B[C\x1B[C
> \x1B[D\x1B[D\x1B[@q\x1B[@q\x1B[@q''
>
> as a result of printing it (print str), the console's output is as
> follows:
>
> aaqqqaa

Your example is rather simplified.  Terminal escape coding can get rather
complex.  Paul Williams of vt100.net has some information on writing
terminal emulators at: http://www.vt100.net/emu/

> Having such string with the escape codes I would like to call a
> function that would process the input and return the "aaqqqaa" string.
> Of course I'm aware that any information about colors will be missed.
> I'm wondering if the curses module has such functionality.

Not that I am aware of.

There are a couple of terminal emulation libraries around that you might
want to take a look at.  I don't know of any that currently provide Python
interfaces; but, they shouldn't be too difficult to interface using
something like SWIG.



More information about the Python-list mailing list