curses and processing terminal escape characters

Discussion in 'Python' started by mix, Oct 29, 2010.

  1. mix

    mix Guest

    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.
    mix, Oct 29, 2010
    #1
    1. Advertising

  2. mix

    Tim Harig Guest

    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.
    Tim Harig, Oct 29, 2010
    #2
    1. Advertising

  3. In message <iaf0l9$3h0$>, 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?
    Lawrence D'Oliveiro, Oct 30, 2010
    #3
  4. mix

    Tim Harig Guest

    On 2010-10-30, Lawrence D'Oliveiro <_zealand> wrote:
    > In message <iaf0l9$3h0$>, 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.
    Tim Harig, Oct 30, 2010
    #4
  5. On 10/29/2010 9:48 PM Lawrence D'Oliveiro said...
    > In message<iaf0l9$3h0$>, 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
    Emile van Sebille, Oct 30, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Turner

    Curses, Terminal Settings

    J Turner, Aug 28, 2003, in forum: Python
    Replies:
    0
    Views:
    380
    J Turner
    Aug 28, 2003
  2. Leif B. Kristensen

    Curses and Terminal DEC

    Leif B. Kristensen, Feb 17, 2005, in forum: Python
    Replies:
    0
    Views:
    301
    Leif B. Kristensen
    Feb 17, 2005
  3. Jean-Paul Calderone
    Replies:
    2
    Views:
    422
    Joel Hedlund
    Feb 9, 2006
  4. Kenneth McDonald
    Replies:
    8
    Views:
    253
    James Gray
    Sep 26, 2008
  5. Vlad Tepes
    Replies:
    4
    Views:
    495
    Vlad Tepes
    Oct 13, 2003
Loading...

Share This Page