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

Search Discussions

  • Tim Harig at Oct 29, 2010 at 5:35 pm

    On 2010-10-29, mix 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.
  • Dan Stromberg at Oct 29, 2010 at 5:57 pm

    On Fri, Oct 29, 2010 at 10:35 AM, Tim Harig wrote:
    On 2010-10-29, mix 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.
    Actually, I believe it's become pretty rare for someone to use a terminal or
    terminal emulator that doesn't support vt100 escape sequences or a superset
    thereof. My condolences if you try to emulate something that does those
    old, nasty Wyse "magic cookies".

    --
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20101029/9e848f89/attachment-0001.html>
  • Lawrence D'Oliveiro at Oct 30, 2010 at 4:48 am

    In message <iaf0l9$3h0$1 at speranza.aioe.org>, Tim Harig wrote:

    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?
  • Tim Harig at Oct 30, 2010 at 3:57 pm

    On 2010-10-30, Lawrence D'Oliveiro wrote:
    In message <iaf0l9$3h0$1 at speranza.aioe.org>, Tim Harig wrote:
    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?
    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.
  • Emile van Sebille at Oct 30, 2010 at 5:40 pm
    On 10/29/2010 9:48 PM Lawrence D'Oliveiro said...
    In message<iaf0l9$3h0$1 at speranza.aioe.org>, Tim Harig wrote:
    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?
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 29, '10 at 3:15p
activeOct 30, '10 at 5:40p
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase