bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to interpret ansi escape code like "\033[; 1; 31mHello World\033


From: Bryan Christ
Subject: Re: How to interpret ansi escape code like "\033[; 1; 31mHello World\033[0m"?
Date: Wed, 19 Nov 2008 10:31:42 -0600

John,

Most console applications rely on some implementation of curses--in this
case ncurses.  For the sake of illustration, i'll use my favorite
console game--myman.

At some point during startup, myman calls initscr().  initscr() tries to
determine what kind of terminal is being used.  The best clue is the
TERM environment variable.  Using the clue from TERM, ncurses consults
the terminfo facility (or legacy termcap facility) which tells ncurses
what the capabilities of the terminal are.  These capabilities
correspond to things like clearing the screen, moving the cursor,
changing colors, etc.

Now back to the illustration.  Suppose myman wants to advance the cursor
to the right.  Likely some form of the ncurses move() function will be
called.  The capability of a terminal to do this operation is cuf1 (see
man pages for terminfo).

Let's assume that myman is running on xterm (as indicated by the TERM
var).  Since every terminal implementation (xterm, vt100, ansi, etc) has
its own unique set of capabilities and escape sequence, ncurses consults
the terminfo facility to determine what escape sequence xterm has
defined for the capability cuf1.  In the case of xterm, cuf1 is \E[C
(see man infocmp).  This escape sequence is written to the terminal
which, is in turn, recognized by xterm to trigger moving the cursor
right.

It would be next to impossible for you to interpret ever possible
terminal definition (/usr/share/terminfo/*/*).  Instead, you need to
create your own terminfo definition or find one you think suits your
needs.  Then, call setenv() to set the TERM var before you call exec().
Once you have done this, your parser can deal strictly with predictable
escape sequences.

Bryan

On Wed, 2008-11-19 at 15:35 +0000, John Daker wrote:
> Hi, again
> 
> I've read some source you guys mentioned, and got more confused...
> 
> What's relationship of vt100,xterm,escape code, how vim produce/explain ?
> 
> What I'm doing is a program that fork a pty to exec programs like
> 'vim', and I use ncurses to display what I read from pty.
> So I have to deal with escape code.
> I've tried some google, and got more confused when 'vt100' 'vt520',
> 'xterm' ....comes up.
> 
> My question is where should I start?
> Where can I get the whole escape code list?
> Which part of the list should I interpret in my program to make sure
> 'vim','top' and so on can work properly?
> 
> Thanks
> 
> On Tue, Nov 18, 2008 at 2:55 AM, Bryan Christ <address@hidden> wrote:
> > Nelson,
> >
> > "Mutt's code is in pager.c, the grok_ansi() function, and only parses a
> > subset"
> >
> > I should have mentioned in my previous email, the other part of the
> > strategy in creating your own interpreter is to also create your own
> > terminfo entry.  This will allow you to restrict the kinds of escape
> > sequences which get transmitted to your input reader.
> >
> > Bryan
> >
> > On Mon, 2008-11-17 at 18:45 +0000, Dan Nelson wrote:
> >> In the last episode (Nov 17), Thomas Dickey said:
> >> > On Mon, 17 Nov 2008, John Daker wrote:
> >> >
> >> > > I'm writing a telnet client with ncurses, and I cannot handle ansi
> >> > > escape code like "\033[;1;31mHello World\033[0m" read through
> >> > > socket from a telnet server,
> >> > >
> >> > > Can ncurses parse ansi escape code to color system of himself?
> >> >
> >> > no - I've considered making a demo to show this, but it wouldn't be
> >> > done in the library (long explanation...)
> >> >
> >> > > Or is there any third party lib to do this?
> >> >
> >> > I don't recall any, offhand -
> >>
> >> Both Mutt and Screen have code that parses ANSI color escape sequences.
> >> Mutt's code is in pager.c, the grok_ansi() function, and only parses a
> >> subset of the "\033[...m" escape sequence (just enough to display
> >> colors).  Screen's is in ansi.c, and is a full VT100/ANSI emulator.
> >>
> >> http://www.mutt.org/
> >> http://www.gnu.org/software/screen/
> >>
> >> --
> >>         Dan Nelson
> >>         address@hidden
> >>
> >>
> >> _______________________________________________
> >> Bug-ncurses mailing list
> >> address@hidden
> >> http://lists.gnu.org/mailman/listinfo/bug-ncurses
> >
> >
> >
> > _______________________________________________
> > Bug-ncurses mailing list
> > address@hidden
> > http://lists.gnu.org/mailman/listinfo/bug-ncurses
> >





reply via email to

[Prev in Thread] Current Thread [Next in Thread]