Class: Tomo::Console::KeyReader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tomo/console/key_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin) ⇒ KeyReader

Returns a new instance of KeyReader.



12
13
14
15
# File 'lib/tomo/console/key_reader.rb', line 12

def initialize(input=$stdin)
  @buffer = +""
  @input = input
end

Instance Method Details

#nextObject

Raises:

  • (Interrupt)


17
18
19
20
21
22
23
24
# File 'lib/tomo/console/key_reader.rb', line 17

def next
  pressed = raw { getc }
  pressed << read_chars_nonblock if pressed == "\e"
  raise Interrupt if pressed == ?\C-c

  clear if !pressed.match?(/\A\w+\z/) || seconds_since_last_press > 0.75
  buffer << pressed
end