[erlang-questions] Read only 1 character from STDin without requiring Return

Richard A. O'Keefe ok@REDACTED
Tue Mar 14 06:16:49 CET 2017


> On 13/03/2017, at 7:19 AM, qp <quantumpotato@REDACTED> wrote:
> 
> How do read 1 character at a time without requiring the enter press?

The obvious question is "why do you want to do this"?

It's actually nowhere as simple an issue as you might think.
Seriously, it's not clear what "read 1 character at a time"
MEANS.  Do you mean
 - read a keycode?
 - read a byte?
 - read a possibly multibyte character?
and
 - if the user pushes a an error or function key,
   sending an ESC [ .... <char> sequence, do you
   want just the ESC (and if so what happens to everything
   after it) or the whole thing
 - how are you going to tell the difference between ESC [ ...
   sent from a special key and an ESC sent from the ESC key
   (I've seen two approaches: don't try to distinguish, and
   use a timeout)
and
 + do you want the character to be echoed
 - or not
 + do you want some interrupt characters to be
   heeded still
 - or not
and
 - if there are multiple characters already buffered
   (from a time when the terminal was in a "cooked" mode, perhaps)
   and you switch to whatever character-at-a-time mode you want,
   what is to happen to those characters?
and above all,
 * given that Erlang uses GNU readline or something very like
   it, even when calling io:get_chars/2, how are you NOT going
   to mess that up?

Having said that,
http://erlang.org/pipermail/erlang-questions/2009-November/047298.html
tells us that Joe Armstrong wrote an emacs-like editor in Erlang.
<quote>
I wrote a simple emacs editor years ago -
it's in the widgets subdirectory of 
http://www.sics.se/~joe/ex11/download/release-2.5.tgz

The only tricky part was not the emacs logic,
but the screen display
and catching the keystrokes and mouse events.
</quote>

That link is dead, but https://github.com/baryluk/ex11
should work.

It's not *much* of an emacs: ^A ^E ^D ^B ^F PgUp PgDn
arrow keys and inserting plain characters.  Call it
proof-of-concept.

An alternative would be to use a separate program to do this.




More information about the erlang-questions mailing list