in reply to Translating Bash to Perl
2 ideas:
a) the bad one: continue translating the rest into perl and worry later by temporarily using $key=`read -n3 key; echo \$key`;. Advantage: you stick to the task at hand.
b) the trivial one would be something like $c=""; do{$c.=ReadKey(0); last if $c=~/[\x0a]/} for (1..<small number for sequence length max>);. (0x0a/LF would also resynchronize the user's notion of 'char sequence starts' with the terminal's notion - 'the may need to press enter' bit).
both quick&dirty approaches should be only used for a very small number of expected keys. At least the user can resynch the tty by pressing enter, so it's not too bad.
The trick with b is (AFAIS not 100% properly done by read -n3) to start only outside a multi-char sequence. And to stop only when you've a full valid key, be it 'c' or CSI-LONG-LONG-STRING...LAST-CHAR-of-CONTROL-SEQUENCE. Also consider multibyte-characters (or add a warning comment like #ignoring multibyte chars for now; utf-8 characters can be quite long in bytes and you should probably test what ReadKey does see in an unicode xterm for e.g. an Umlaut. Then there's stuff like BIG5 and other older encodings).
If there's some sanity to CSI-style esc code sequence length I've yet to read about it (excluding the trivial enumeration approach: a table for all sequences for the current terminal settings; to be available for possible terminalsettings and termcap entries; weighing in at maybe a dozen MB or so ;> ).
As for curses, a quick grep shows that e.g. Curses::UI::Common::get_key has e.g. code to collect the individual ReadKey's or getch's into a variable, with a cautious comment of "I have no idea of the portability of this stuff but it works for me" :/.
Maybe there's a simple module in between ReadKey and full Curses to get the whole key sequence into a variable. Anyone has seen possible candidates?
Might be quest-level stuff for the general case:)
update:
please also include non-curses / partial terminal handling modules, if they offer abstractions beyond ReadKey.
Thx to bichonfrise for his pointer Curses::Simple.
My own hoard of never-used just-in-case-examples lists perlmenu, Curses/Perl::UI, and Curses::Widgets, all of which are rather complete user interfaces on a level far higher than ReadKey. Lower than ReadKey and of historic interest is the approach in Larry Wall's process-killer zap of camelbook fame. Might have appeared already in the 1st Ed as it was pure perl4 and eminently hackable.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Translating Terminal Input Code from Bash to Perl
by Anonymous Monk on Sep 30, 2009 at 20:53 UTC | |
by jakobi (Pilgrim) on Sep 30, 2009 at 20:57 UTC | |
|
Re^2: Translating Terminal Input Code from Bash to Perl
by Anonymous Monk on Sep 30, 2009 at 20:58 UTC | |
by jakobi (Pilgrim) on Sep 30, 2009 at 22:18 UTC |