Class: IO
- Inherits:
-
Object
- Object
- IO
- Defined in:
- lib/tlspretense/ext_core/io_raw_input.rb
Overview
Extends IO to enable “raw” input on TTYs.
Instance Method Summary collapse
-
#disable_raw_chars ⇒ Object
Reverts the termios state to what it was before calling #enable_raw_chars.
-
#enable_raw_chars ⇒ Object
Enables raw character input for a TTY.
Instance Method Details
#disable_raw_chars ⇒ Object
Reverts the termios state to what it was before calling #enable_raw_chars.
25 26 27 28 |
# File 'lib/tlspretense/ext_core/io_raw_input.rb', line 25 def disable_raw_chars raise IOError, "#{self} is not a TTY." unless self.tty? Termios.tcsetattr(self, Termios::TCSANOW, @_oldtermios) end |
#enable_raw_chars ⇒ Object
Enables raw character input for a TTY. It uses the ruby-termios gem to disable ICANON and ECHO functionality. This means that characters will become immediately available to IO#gets, IO#getchar, etc. after the user presses a button, and that the characters will not be implicitly echoed to the screen.
If you call this on $stdin, you probably should ensure that you call #disable_raw_chars in order to restore the previous termios state after your program exits. Otherwise, you may screw up the user’s terminal, and they will have to call reset.
15 16 17 18 19 20 21 22 |
# File 'lib/tlspretense/ext_core/io_raw_input.rb', line 15 def enable_raw_chars raise IOError, "#{self} is not a TTY." unless self.tty? @_oldtermios = Termios.tcgetattr(self) newt = @_oldtermios.dup newt.c_lflag &= ~Termios::ICANON newt.c_lflag &= ~Termios::ECHO Termios.tcsetattr(self, Termios::TCSANOW, newt) end |