Sha256: e1b0ba5ae5bf65605c0ff406d0b808344983b24736fbbeb3fc6f83f538cdb2d0

Contents?: true

Size: 1.61 KB

Versions: 17

Compression:

Stored size: 1.61 KB

Contents

module Vedeu

  module Input

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

      # Instantiate Vedeu::Input::Input and capture keypress(es).
      #
      # @param (see #initialize)
      # @return (see #read)
      def self.read(reader)
        new(reader).read
      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 read
        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

17 entries across 17 versions & 1 rubygems

Version Path
vedeu-0.6.37 lib/vedeu/input/capture.rb
vedeu-0.6.36 lib/vedeu/input/capture.rb
vedeu-0.6.35 lib/vedeu/input/capture.rb
vedeu-0.6.34 lib/vedeu/input/capture.rb
vedeu-0.6.33 lib/vedeu/input/capture.rb
vedeu-0.6.32 lib/vedeu/input/capture.rb
vedeu-0.6.31 lib/vedeu/input/capture.rb
vedeu-0.6.30 lib/vedeu/input/capture.rb
vedeu-0.6.29 lib/vedeu/input/capture.rb
vedeu-0.6.28 lib/vedeu/input/capture.rb
vedeu-0.6.27 lib/vedeu/input/capture.rb
vedeu-0.6.26 lib/vedeu/input/capture.rb
vedeu-0.6.25 lib/vedeu/input/capture.rb
vedeu-0.6.24 lib/vedeu/input/capture.rb
vedeu-0.6.23 lib/vedeu/input/capture.rb
vedeu-0.6.22 lib/vedeu/input/capture.rb
vedeu-0.6.21 lib/vedeu/input/capture.rb