Module: Themigrator::UI::Getchar

Included in:
Themigrator::UI
Defined in:
lib/themigrator/ui/getchar.rb

Constant Summary collapse

KEY2SYM =
Hash.new { |h, k| h[k] = k }

Instance Method Summary collapse

Instance Method Details

#getcharObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/themigrator/ui/getchar.rb', line 18

def getchar
  begin
    c = STDIN.getc.chr
    # gather next two characters of special keys
    if c == "\e"
      extra_thread = Thread.new do
        c += STDIN.getc.chr
        c += STDIN.getc.chr
        c += STDIN.getc.chr if ["\e[6", "\e[5"].include?(c)
      end
      # wait just long enough for special keys to get swallowed
      extra_thread.join(0.00100)
      # kill thread so not-so-long special keys don't wait on getc
      extra_thread.kill
    end
  rescue => e
    ui_log_exception(e)
  ensure
  end
  KEY2SYM[c]
end