Class: Konamio::Sequence::Listener

Inherits:
Base
  • Object
show all
Includes:
KeyMap
Defined in:
lib/konamio/sequence/listener.rb

Instance Method Summary collapse

Methods included from KeyMap

included, #sequence_for

Constructor Details

#initialize(options) ⇒ Listener

Returns a new instance of Listener.



6
7
8
9
# File 'lib/konamio/sequence/listener.rb', line 6

def initialize(options)
  options = { input: $stdin, debounce: 0.0001 }.merge(options)
  load_options(:sequence, options)
end

Instance Method Details

#execute!Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/konamio/sequence/listener.rb', line 11

def execute!
  case listen
  when true
    return Konamio::Result.new(success: true, data: { sequence: @sequence[1..-1]})
  when false
    return Konamio::Result.new(success: false, data: { sequence: @sequence })
  when :negative
    return Konamio::Result.new(success: false, data: { sequence: :negative })
  end
end

#listenObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/konamio/sequence/listener.rb', line 22

def listen
  input = @input.getch
  if(input == "\e")
    extra_thread = Thread.new{
      input << @input.getch
      input << @input.getch
    }

    # wait just long enough for special keys to get swallowed
    extra_thread.join(@debounce)
    # kill thread so not-so-long special keys don't wait on getc
    extra_thread.kill
  end

  case input
  when sequence_for(@sequence[0])
    true
  when "\e"
    :negative
  else
    false
  end
end