🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.perl.misc
Article #3

a question about TUIs and raw input from ttys

#3
From: Eli the Bearded
Date: Sun, 29 Nov 2026 05:51
86 lines
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
Technical Details
Message-ID: <eli$2611290036@qaz.wtf>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!panix!.POSTED.panix5.panix.com!qz!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!panix!.POSTED.panix5.panix.com!qz!not-for-mail
From: Eli the Bearded <*@eli.users.panix.com>
Newsgroups: comp.lang.perl.misc
Subject: a question about TUIs and raw input from ttys
Date: Sun, 29 Nov 2026 05:51:05 -0000 (UTC)
Organization: Some absurd concept
Message-ID: <eli$2611290036@qaz.wtf>
Injection-Date: Sun, 29 Nov 2026 05:51:05 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="panix5.panix.com:166.84.1.5";
	logging-data="20446"; mail-complaints-to="abuse@panix.com"
User-Agent: Vectrex rn 2.1 (beta)
X-Liz: It's actually happened, the entire Internet is a massive game of Redcode
X-Motto: "Erosion of rights never seems to reverse itself." -- kenny@panix
X-US-Congress: Moronic Fucks.
X-Attribution: EtB
XFrom: is a real address
Encrypted: double rot-13