Module: TLSPretense::TestHarness::InputHandler

Defined in:
lib/tlspretense/test_harness/input_handler.rb

Overview

EM handler to handle keyboard input while a test is running.

Instance Method Summary collapse

Instance Method Details

#initialize(stdin = $stdin) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/tlspretense/test_harness/input_handler.rb', line 6

def initialize(stdin=$stdin)
  @stdin = stdin
  @actions = {}

  # Set the term to accept keystrokes immediately.
  if @stdin.tty?
    @stdin.enable_raw_chars
  end
end

#on(char, blk = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
# File 'lib/tlspretense/test_harness/input_handler.rb', line 31

def on(char, blk=nil, &block)
  puts "Warning: setting a keyboard handler for a keystroke that is longer than one char: #{char.inspect}" if char.length != 1
  raise ArgumentError, "No block passed in" if blk == nil and block == nil
  @actions[char] = ( blk ? blk : block)
end

#receive_data(data) ⇒ Object

Receives one character at a time.



24
25
26
27
28
29
# File 'lib/tlspretense/test_harness/input_handler.rb', line 24

def receive_data(data)
  raise "data was longer than 1 char: #{data.inspect}" if data.length != 1
  if @actions.has_key? data
    @actions[data].call
  end
end

#unbindObject



16
17
18
19
20
21
# File 'lib/tlspretense/test_harness/input_handler.rb', line 16

def unbind
  # Clean up by resotring the old termios
  if @stdin.tty?
    @stdin.disable_raw_chars
  end
end