Module: Clack::Core::KeyReader
- Defined in:
- lib/clack/core/key_reader.rb
Overview
Reads single keystrokes from the terminal in raw mode. Handles escape sequences for arrow keys and other special keys.
Constant Summary collapse
- ESCAPE_TIMEOUT =
Timeout for detecting if Escape is part of a sequence (50ms). If no follow-up character arrives, treat Escape as a standalone key.
0.05- SEQUENCE_TIMEOUT =
Timeout for reading additional characters in a CSI sequence (10ms). Short because subsequent bytes in a sequence arrive almost instantly.
0.01
Class Method Summary collapse
-
.read(input = nil) ⇒ String?
Read a single keystroke in raw mode.
Class Method Details
.read(input = nil) ⇒ String?
Read a single keystroke in raw mode. When input is an IO backed by a console, uses raw mode. When input is a StringIO or test double, reads directly.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/clack/core/key_reader.rb', line 25 def read(input = nil) io = input || IO.console raise IOError, "No console available (not a TTY?)" unless io # StringIO / test doubles don't support raw mode return read_from(io) unless io.respond_to?(:raw) io.raw { |raw_io| read_from(raw_io) } rescue Errno::EIO, Errno::EBADF, IOError # Terminal disconnected or closed - treat as cancel "\u0003" # Ctrl+C end |