Class: TTY2::Reader::WinConsole

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

Constant Summary collapse

ESC =
"\e"
NUL_HEX =
"\x00"
EXT_HEX =
"\xE0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ WinConsole

Returns a new instance of WinConsole.



26
27
28
29
30
31
# File 'lib/tty2/reader/win_console.rb', line 26

def initialize(input)
  require_relative "win_api"
  @input = input
  @keys = Keys.ctrl_keys.merge(Keys.win_keys)
  @escape_codes = [[NUL_HEX.ord], [ESC.ord], EXT_HEX.bytes.to_a]
end

Instance Attribute Details

#escape_codesArray[Integer] (readonly)

Escape codes

Returns:

  • (Array[Integer])


24
25
26
# File 'lib/tty2/reader/win_console.rb', line 24

def escape_codes
  @escape_codes
end

#keysHash[Symbol] (readonly)

Key codes

Returns:

  • (Hash[Symbol])


17
18
19
# File 'lib/tty2/reader/win_console.rb', line 17

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 blocking for input

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)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tty2/reader/win_console.rb', line 45

def get_char(echo: true, raw: false, nonblock: false)
  if raw && echo
    if nonblock
      get_char_echo_non_blocking
    else
      get_char_echo_blocking
    end
  elsif raw && !echo
    nonblock ? get_char_non_blocking : get_char_blocking
  elsif !raw && !echo
    nonblock ? get_char_non_blocking : get_char_blocking
  else
    @input.getc
  end
end

#get_char_blockingObject



72
73
74
# File 'lib/tty2/reader/win_console.rb', line 72

def get_char_blocking
  WinAPI.getch.chr
end

#get_char_echo_blockingObject



76
77
78
# File 'lib/tty2/reader/win_console.rb', line 76

def get_char_echo_blocking
  WinAPI.getche.chr
end

#get_char_echo_non_blockingObject



68
69
70
# File 'lib/tty2/reader/win_console.rb', line 68

def get_char_echo_non_blocking
  input_ready? ? get_char_echo_blocking : nil
end

#get_char_non_blockingObject

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 the char for last key pressed, or if no keypress return nil



64
65
66
# File 'lib/tty2/reader/win_console.rb', line 64

def get_char_non_blocking
  input_ready? ? get_char_blocking : nil
end

#input_ready?Boolean

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.

Check if IO has user input

Returns:

  • (Boolean)


85
86
87
# File 'lib/tty2/reader/win_console.rb', line 85

def input_ready?
  !WinAPI.kbhit.zero?
end