module SSLTest::InputHandler

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

Public Class Methods

new(stdin=$stdin) click to toggle source
# File lib/ssl_test/input_handler.rb, line 7
def initialize(stdin=$stdin)
  @stdin = stdin
  @actions = {}

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

Public Instance Methods

on(char, blk=nil, &block) click to toggle source
# File lib/ssl_test/input_handler.rb, line 28
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) click to toggle source

Receives one character at a time.

# File lib/ssl_test/input_handler.rb, line 21
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
unbind() click to toggle source
# File lib/ssl_test/input_handler.rb, line 15
def unbind
  # Clean up by resotring the old termios
  @stdin.disable_raw_chars
end