class TTY::Reader::Console

Constants

CSI
ESC
TIMEOUT

Attributes

escape_codes[R]

Escape codes

@return [Array]

@api public

input[R]
keys[R]

Key codes

@return [Hash]

@api public

mode[R]

Public Class Methods

new(input) click to toggle source
# File lib/tty/reader/console.rb, line 29
def initialize(input)
  @input = input
  @mode  = Mode.new(input)
  @keys  = Keys.ctrl_keys.merge(Keys.keys)
  @escape_codes = [[ESC.ord], CSI.bytes.to_a]
end

Public Instance Methods

get_char(echo: true, raw: false, nonblock: false) click to toggle source

Get a character from console with echo

@param [Boolean] echo

whether to echo input back or not, defaults to true

@param [Boolean] raw

whether to use raw mode or not, defaults to false

@param [Boolean] nonblock

whether to wait for input or not, defaults to false

@return [String]

@api private

# File lib/tty/reader/console.rb, line 48
def get_char(echo: true, raw: false, nonblock: false)
  mode.raw(raw) do
    mode.echo(echo) do
      if nonblock
        input.wait_readable(TIMEOUT) ? input.getc : nil
      else
        input.getc
      end
    end
  end
end