[erlang-questions] Reading (and ignoring) escape-sequences

Richard O'Keefe <>
Tue Mar 23 23:29:07 CET 2010

On Mar 23, 2010, at 10:13 PM, Mazen Harake wrote:

> Yeah I've already started digging into that.
> The problem is more fundamental then I thought and in the end it  
> turns out that all libraries (including ncurses) depend on the the  
> fact that a human can not type in the escape sequence as fast as it  
> is received by a computer. This makes it _extreamly_ hard (I would  
> say impossible) to do this from pure Erlang; it has to be done from  
> inside the driver.

There seems to be no fundamental reason why the Erlang _language_
would be unsuitable for the task, it does have time-outs, after all.

My own emacs-like screen editor handles this by not _caring_
whether the ESC was generated by some other key or by the user
pressing the ESC key.  If the sequence "\027[A" comes in, I just
don't care whether the user pressed one key (Up arrow) or three
(ESC, [, A).  (In emacs terms, Meta-[ has its own keymap.)

For me, processing fancy-key sequences got a _lot_ easier when I
decided to support the ECMA sequences only.  These days, when
practically all terminals are actually terminal emulators, and
they emulate the ECMA/ANSI interface more or less well, it's not
too hard.  In the old days, we _desperately_ needed termcap/
termlib, because you might have a computer with twenty terminals,
no two of them using the same codes.

Perhaps the place to start, then, is just how many kinds of terminal
you want to support.  On my system,
	toe | wc
lists 1562 different terminal descriptions.  (There aren't that
many ways to encode the fancy keys, but there _are_ lots.)  If you
want to support any fraction of those terminals, you have no
realistic option but to use termcap/termlib.  You could process
that using Erlang, but why bother?  You might as well use the method
ncurses uses, by using ncurses in an outboard program.

If you only want to support ECMA/ANSI escape sequences, the best way
is to redesign your protocol so that you don't care any more than I
do whether a sequence was typed by hand or the result of a single
keypress.  ECMA/ANSI escapes generally have the form
	ESC [ {parameter;...;parameter} letter
so simply ensuring that nobody ever has any other reason to send
ESC [ should get you a long way.

More information about the erlang-questions mailing list