Class: TTY::Reader::WinConsole

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

Constant Summary collapse

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

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/tty/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/tty/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/tty/reader/win_console.rb', line 17

def keys
  @keys
end

Instance Method Details

#get_char(options) ⇒ 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:

  • options (Hash[Symbol])

Options Hash (options):

  • :echo (Symbol)

    the echo mode toggle

  • :raw (Symbol)

    the raw mode toggle

Returns:

  • (String)


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

def get_char(options)
  if options[:raw] && options[:echo]
    if options[:nonblock]
      get_char_echo_non_blocking
    else
      get_char_echo_blocking
    end
  elsif options[:raw] && !options[:echo]
    options[:nonblock] ? get_char_non_blocking : get_char_blocking
  elsif !options[:raw] && !options[:echo]
    options[:nonblock] ? get_char_non_blocking : get_char_blocking
  else
    @input.getc
  end
end

#get_char_blockingObject



71
72
73
# File 'lib/tty/reader/win_console.rb', line 71

def get_char_blocking
  WinAPI.getch.chr
end

#get_char_echo_blockingObject



75
76
77
# File 'lib/tty/reader/win_console.rb', line 75

def get_char_echo_blocking
  WinAPI.getche.chr
end

#get_char_echo_non_blockingObject



67
68
69
# File 'lib/tty/reader/win_console.rb', line 67

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



63
64
65
# File 'lib/tty/reader/win_console.rb', line 63

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)


84
85
86
# File 'lib/tty/reader/win_console.rb', line 84

def input_ready?
  !WinAPI.kbhit.zero?
end