Sha256: 4977c9acecc2e51206e899d2960edf1f3d8604882fe1024fa4c75100bcfc3897

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 KB

Contents

module Vedeu

  # Captures input from the user via {Vedeu::Terminal#input} and translates
  # special characters into symbols.
  #
  class Input

    # Instantiate Input and capture keypress(es).
    #
    # @param (see #initialize)
    # @return [String|Symbol]
    def self.capture(reader)
      new(reader).capture
    end

    # Returns a new instance of Vedeu::Input.
    #
    # @param reader [IO] An object that responds to `#read`. Typically, this is
    #   Vedeu::Terminal.
    # @return [Vedeu::Input]
    def initialize(reader)
      @reader = reader
    end

    # Triggers either a ':_command_' event with the command when the reader is
    # in cooked mode, or when in raw mode, the keypress event with the key(s)
    # pressed.
    #
    # @return [Array|String|Symbol]
    def capture
      if reader.raw_mode?
        Vedeu.trigger(:_keypress_, keypress)

      elsif reader.fake_mode?
        Vedeu.trigger(:_editor_, keypress)

      else
        Vedeu.trigger(:_command_, command)

      end
    end

    protected

    # @!attribute [r] reader
    # @return [IO]
    attr_reader :reader

    private

    # Returns the translated (when possible) keypress(es) as either a String or
    # a Symbol.
    #
    # @return [String|Symbol]
    def keypress
      key = input

      Vedeu::InputTranslator.translate(key)
    end

    # Returns the input from the terminal.
    #
    # @return [String]
    def input
      @input ||= reader.read
    end
    alias_method :command, :input

  end # Input

end # Vedeu

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vedeu-0.6.7 lib/vedeu/input/input.rb
vedeu-0.6.6 lib/vedeu/input/input.rb
vedeu-0.6.5 lib/vedeu/input/input.rb
vedeu-0.6.4 lib/vedeu/input/input.rb
vedeu-0.6.3 lib/vedeu/input/input.rb
vedeu-0.6.2 lib/vedeu/input/input.rb
vedeu-0.6.1 lib/vedeu/input/input.rb
vedeu-0.6.0 lib/vedeu/input/input.rb