Thread View: comp.lang.perl.misc
3 messages
3 total messages
Started by Eli the Bearded
Sun, 29 Nov 2026 05:51
a question about TUIs and raw input from ttys
Author: Eli the Bearded
Date: Sun, 29 Nov 2026 05:51
Date: Sun, 29 Nov 2026 05:51
86 lines
3423 bytes
3423 bytes
What's the trick to reading raw input from a tty (xterm in my case) to
make Perl not miss things?
I've written a rough draft of a text mode (text user interface; TUI)
game yesterday and today, and it works, but the controls feel janky,
like it might take two key presses to get something done.
The full source is here:
https://codeberg.org/elithebearded/flow-thy-pipes
You could `git clone` it to see the whole thing, but the input boils
down to three functions.
sub set_raw_tty() {
qx( stty raw -echo -isig -icanon -ixon -icrnl );
}
sub restore_tty() {
qx( stty echo icanon isig opost imaxbel ixon icrnl brkint );
}
sub process_keyboard() {
my $rin = '';
my $rout;
my $nfound;
vec($rin, fileno(STDIN), 1) = 1;
$nfound = select($rout = $rin, undef, undef, 0.05);
if($nfound) {
my $c;
if ( read STDIN, $c, 1 ) {
if ($c eq 'Q') { end_play }
if (! $win) {
if ($c eq 'h') { $cursor_col --; if ($cursor_col < 1) { $cursor_col = 1; }}
if ($c eq 'l') { $cursor_col ++; if ($cursor_col > ($wide-1)) { $cursor_col = $wide-1; }}
if ($c eq 'k') { $cursor_row --; if ($cursor_row < 1) { $cursor_row = 1; }}
if ($c eq 'j') { $cursor_row ++; if ($cursor_row > ($tall-1)) { $cursor_row = $tall-1; }}
if ($c eq 'a') {
my $cell = $board[$cursor_row][$cursor_col];
if (defined($rotations{$cell})) {
$board[$cursor_row][$cursor_col] = rot_ccw( $cell );
}
}
if ($c eq 'z') {
my $cell = $board[$cursor_row][$cursor_col];
if (defined($rotations{$cell})) {
$board[$cursor_row][$cursor_col] = rot_cw( $cell );
}
}
} else {
if ($c eq ' ') {
$level ++;
$remaining = -1;
}
}
}
}
}
The main loop looks like:
while (1) {
draw_board;
process_keyboard;
if ($win) {
# ...
return;
}
}
Moving around the board, or rotating things under the cursor, can take
multiple keystrokes where one is needed.
I would rather not use a curses library for this, as I want to
understand what a library like that is doing for me (besides reading
the termcap / terminfo files to generalize the control sequences).
I suspect there's something better to do with select() and read(),
but what?
Elijah
------
wants to get input solved before moving on to maze generation
Re: a question about TUIs and raw input from ttys
Author: Ivan Shmakov
Date: Sun, 29 Nov 2026 13:27
Date: Sun, 29 Nov 2026 13:27
90 lines
4397 bytes
4397 bytes
>>>>> On 2026-11-29, Eli the Bearded wrote:
> What's the trick to reading raw input from a tty (xterm in my case)
> to make Perl not miss things?
> I've written a rough draft of a text mode (text user interface; TUI)
> game yesterday and today, and it works, but the controls feel janky,
> like it might take two key presses to get something done.
Sounds like a buffering issue. Let's see.
> sub set_raw_tty() {
> qx( stty raw -echo -isig -icanon -ixon -icrnl );
> }
Here you disable in-kernel tty buffering, alright...
> $nfound = select($rout = $rin, undef, undef, 0.05);
> if($nfound) {
> my $c;
> if ( read STDIN, $c, 1 ) {
... yet here you request in-Perl filehandle buffering instead.
I suspect the Perl function you'd want to use here is sysread,
not read.
What happens here is this. Suppose user types something like
^[[31~ on their terminal. This makes "select" return 1, then
the following Perl "read" call results in a read(2) syscall
requesting a pageful (4096 bytes) or so of input, of which
"read" puts a single "^[" byte (I'm assuming you don't configure
STDIN for UTF-8 reads here) into $c, and keeps the rest in its
internal buffer.
The next read /would/ return the rest of the input, but it never
gets called, because the next "select" does not return 1 - the
kernel has already given your process all the data it had!
I was able to reproduce the issue with the following code: it
indeed takes a second keypress after a "multibyte" key, and using
"sysread" in place of "read" resolves the issue.
use common::sense;
require IO::Select;
system (qw { stty raw isig }); ## NB: isig so I can ^C it when done
my $in = \*STDIN;
my $sel = IO::Select->new ();
$sel->add ($in);
while (1) {
my @rd = $sel->can_read (0.05);
next
unless (@rd);
my $s;
$rd = $in->read ($s, 1)
or die ($!);
print ("Got ", $s, "\n");
}
FWIW, I don't recall ever trying to implement reading
DEC VT/ANSI/ECMA 48 ESC-sequences from a terminal in Perl,
but it turns out that in networking code, mixing buffered
and unbuffered IO is also a recipe for trouble. I've
learned to use syscall tracers, such as "$ strace -ft " and
ktruss(1), to troubleshoot such issues.
> I would rather not use a curses library for this, as I want to
> understand what a library like that is doing for me (besides reading
> the termcap / terminfo files to generalize the control sequences).
Unless I be mistaken, the Curses' main feature is that it
maintains a screen buffer. Which is handy for fullscreen
applications, but less applicable for things like Readline
- where using Termcap / Terminfo support alone, rather than
the entire Curses library, makes more sense.
And it annoys me to no end when an application refuses to consult
the databases and just uses hardcoded ESC sequences instead.
Like, "why, everyone now supports VT100 colors!" (VT100, VT220,
VT320 had no support for colors, if XTerm FAQ is to be believed.)
^[[34mNever mind that I might be running the command under
^[[1mscript(1)^[0m (or using "log" function built into GNU Screen.)
> wants to get input solved before moving on to maze generation
Reminds me of [1], somehow.
[1] http://linusakesson.net/games/autosokoban/
Re: a question about TUIs and raw input from ttys
Author: Eric Pozharski
Date: Sun, 29 Nov 2026 23:37
Date: Sun, 29 Nov 2026 23:37
12 lines
1476 bytes
1476 bytes
with <eli$2611290036@qaz.wtf> Eli the Bearded wrote: > What's the trick to reading raw input from a tty (xterm in my case) to > make Perl not miss things? Have you considered Term::ReadKey instead? It sure has hairs (read: old and cranky) but might do what you need (before going curses). *CUT* [ 81 lines 1 level deep] -- Torvalds' goal for Linux is very simple: World Domination Stallman's goal for GNU is even simpler: Freedom
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads