Sha256: cabf01d81398cd663f1139cd580014352bd293576867a4ff6219d7582b0884ed

Contents?: true

Size: 1.62 KB

Versions: 10

Compression:

Stored size: 1.62 KB

Contents

module Vedeu

  module Input

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

      # Instantiate Vedeu::Input::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::Input.
      #
      # @param reader [IO] An object that responds to `#read`.
      #   Typically, this is Vedeu::Terminal.
      # @return [Vedeu::Input::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).
      #
      # @return [String|Symbol]
      def keypress
        key = input

        Vedeu::Input::Translator.translate(key)
      end

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

    end # Input

  end # Input

end # Vedeu

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
vedeu-0.6.17 lib/vedeu/input/input.rb
vedeu-0.6.16 lib/vedeu/input/input.rb
vedeu-0.6.15 lib/vedeu/input/input.rb
vedeu-0.6.14 lib/vedeu/input/input.rb
vedeu-0.6.13 lib/vedeu/input/input.rb
vedeu-0.6.12 lib/vedeu/input/input.rb
vedeu-0.6.11 lib/vedeu/input/input.rb
vedeu-0.6.10 lib/vedeu/input/input.rb
vedeu-0.6.9 lib/vedeu/input/input.rb
vedeu-0.6.8 lib/vedeu/input/input.rb