class GitWaybackMachine::Controls

Public Class Methods

new() click to toggle source
# File lib/git_wayback_machine/controls.rb, line 7
def initialize
end

Public Instance Methods

on_event(&callback) click to toggle source
# File lib/git_wayback_machine/controls.rb, line 10
def on_event(&callback)
  while true
    case read_key
    when "\e[A"      then callback.call(:up)    # UP
    when "\e[B"      then callback.call(:down)  # DOWN
    when "\r", "\n"  then callback.call(:enter) # ENTER
    when "\u0003", "\e" # Ctrl+C, ESC
      exit(0)
    end
  end
end

Private Instance Methods

read_key() click to toggle source
# File lib/git_wayback_machine/controls.rb, line 24
def read_key
  STDIN.raw do |io|
    key = io.getc.chr

    if key == "\e"
      extra_thread = Thread.new do
        key += io.getc.chr + io.getc.chr
      end
      extra_thread.join(0.001)
      extra_thread.kill
    end

    key
  end
end