Sha256: b0653960600bf47b413d1645fb408152d7434e6b9020e4848606c25c309ef972

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

module Vedeu

  module Editor

    # Capture input from the terminal via 'getch'.
    #
    class Capture

      ESCAPE_KEY_CODE = 27 # \e

      # @param console [IO]
      # @return [String|Symbol]
      def self.read(console)
        new(console).read
      end

      # Returns a new instance of Vedeu::Editor::Capture.
      #
      # @param console [IO]
      # @return [Vedeu::Editor::Capture]
      def initialize(console)
        @console = console
      end

      # @return [String|Symbol]
      def read
        Vedeu::InputTranslator.translate(keys)
      end

      private

      # @return [String]
      def keys
        keys = console.getch

        if keys.ord == ESCAPE_KEY_CODE
          @chars = 3

          begin
            keys << console.read_nonblock(@chars)

          rescue IO::WaitReadable
            IO.select([console])
            @chars -= 1
            retry

          rescue IO::WaitWritable
            IO.select(nil, [console])
            @chars -= 1
            retry

          end
        end

        keys
      end

      # @return [IO]
      def console
        @console || Vedeu::Terminal.console
      end

    end # Capture

  end # Editor

end # Vedeu

Version data entries

7 entries across 7 versions & 1 rubygems

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