curses and processing terminal escape characters

M

mix

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:

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

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

Tim Harig

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

Lawrence D'Oliveiro

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.

Does anyone still bother with anything other than VT1xx-type terminals?
 
T

Tim Harig

Does anyone still bother with anything other than VT1xx-type terminals?

1. Actually, most modern terminals and emulators have color which means
that they must by in the VT2xx range or above.

2. Yes there are still hardware terminals being used and not all of them
are VTxxxs.

3. Even assuming that the OP is only conserned about VTxxx terminals, it is
still a better coding practice to use a layer of abstraction so as
to keep the "magic numbers" out of his code.

4. Just because VTxxx style terminals and emulators currently dominate
doesn't mean that they will do so forever. It is quite possible
that in an age where most terminals are emulated, that somebody
will create a new style of terminal that fits in better to the
virtual/graphical world in which they inhabit.

5. Although it is offtopic for this question, using the curses abstraction
rather then hardcoded values, allows a visual text based program to
work in places that do not act like a traditional terminal.
Microsoft Windows console fits this description because formating
commands are not sent in-band with the text data. By using
something like PDCurses, it is possible to run curses programs on
non-terminal environments such as these.
 
E

Emile van Sebille

On 10/29/2010 9:48 PM Lawrence D'Oliveiro said...
Does anyone still bother with anything other than VT1xx-type terminals?

I'm still supporting about 150 green screen user seats (CHUIs), and no,
they're all running vt100 emulators.

Done-with-termcap-hacking-ly y'rs,

Emile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top