Sha256: f210a1db1cf02a862d352e2a35829e3b7d51545f805310cb1abd4e1240ec6b08

Contents?: true

Size: 1.29 KB

Versions: 8

Compression:

Stored size: 1.29 KB

Contents

require 'remedy/console'
require 'remedy/key'

module Remedy
  module Keyboard
    module_function

    def get
      parse raw_get
    end

    def raw_get
      Console.raw do
        input = STDIN.getc.chr

        if input == "\e" then
          input << STDIN.read_nonblock(3) rescue nil
          input << STDIN.read_nonblock(2) rescue nil
        end

        input
      end
    end

    def parse sequence
      key = Key.new sequence

      if raise_on_control_c? && key.control_c? then
        raise ControlC, "User pressed Control-C"
      elsif key.recognized? then
        key
      elsif raise_on_unrecognized_key? then
        raise UnrecognizedInput, %Q{Unknown key or byte sequence: "#{sequence}" : #{key.inspect}}
      else
        key
      end
    end

    def raise_on_unrecognized_key?
      @raise_on_unrecognized_key
    end

    def raise_on_unrecognized_key!
      @raise_on_unrecognized_key = true
    end

    def dont_raise_on_unrecognized_key!
      @raise_on_unrecognized_key = false
    end

    def raise_on_control_c?
      @raise_on_control_c
    end

    def raise_on_control_c!
      @raise_on_control_c = true
    end

    def dont_raise_on_control_c!
      @raise_on_control_c = false
    end

    class ControlC < Interrupt; end
    class UnrecognizedInput < IOError; end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
remedy-0.4.0 lib/remedy/keyboard.rb
remedy-0.3.1 lib/remedy/keyboard.rb
remedy-0.3.0 lib/remedy/keyboard.rb
remedy-0.2.0 lib/remedy/keyboard.rb
remedy-0.1.2 lib/remedy/keyboard.rb
remedy-0.1.1 lib/remedy/keyboard.rb
remedy-0.1.0 lib/remedy/keyboard.rb
remedy-0.0.5 lib/remedy/keyboard.rb