Ticket #111 (new defect)

Opened 2 months ago

Last modified 2 months ago

EM::open_keyboard is line-buffered

Reported by: tmm1 Assigned to:
Priority: major Milestone:
Keywords: Cc:

Description

KEYBOARD suggests that it should not be line-buffered by default, and that you should use EM::P::LineText?2 w/ receive_line for line-buffered access

Change History

03/17/09 02:07:41 changed by careo

This isn't strictly true. The problem is with the terminal settings on stdin. You can work around it one of two ways.

The first is shell out to stty like this:

  # save current terminal state and set terminal to "raw" mode
  $terminal_state = `stty -g`
  system "stty raw -echo"
  
  # use $stdin

  # restore terminal settings.
  system "stty #{$terminal_state}"

That works, but feels like cheating (to me at least). You can do the same using the Termios library:

  require 'termios'
  
  # save current terminal state and set terminal to "raw" mode
  $terminal_state = Termios.getattr $stdin
  new_settings = $terminal_state.dup
  new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON)
  new_settings.c_cc[Termios::VMIN] = 1
  Termios.setattr $stdin, Termios::TCSANOW, new_settings
  
  # use $stdin
  
  # restore terminal settings.
  Termios.setattr($stdin, Termios::TCSANOW, $terminal_state)

Note that either way will give you every character, including C and friends, and that it will really hose the user's terminal if you exit without restoring the terminal settings.