Class: TTY2::Reader::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/tty2/reader/console.rb

Constant Summary collapse

ESC =
"\e"
CSI =
"\e["
TIMEOUT =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Console

Returns a new instance of Console.



30
31
32
33
34
35
# File 'lib/tty2/reader/console.rb', line 30

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

Instance Attribute Details

#escape_codesArray[Integer] (readonly)

Escape codes

Returns:

  • (Array[Integer])


28
29
30
# File 'lib/tty2/reader/console.rb', line 28

def escape_codes
  @escape_codes
end

#keysHash[Symbol] (readonly)

Key codes

Returns:

  • (Hash[Symbol])


21
22
23
# File 'lib/tty2/reader/console.rb', line 21

def keys
  @keys
end

Instance Method Details

#get_char(echo: true, raw: false, nonblock: false) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a character from console with echo

Parameters:

  • echo (Boolean) (defaults to: true)

    whether to echo input back or not, defaults to true

  • raw (Boolean) (defaults to: false)

    whether to use raw mode or not, defaults to false

  • nonblock (Boolean) (defaults to: false)

    whether to wait for input or not, defaults to false

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tty2/reader/console.rb', line 49

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