class TTY::Reader::WinConsole

Constants

ESC
EXT_HEX
NUL_HEX

Attributes

escape_codes[R]

Escape codes

@return [Array]

@api public

keys[R]

Key codes

@return [Hash]

@api public

Public Class Methods

new(input) click to toggle source
# 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

Public Instance Methods

get_char(options) click to toggle source

Get a character from console blocking for input

@param [Hash] options @option options [Symbol] :echo

the echo mode toggle

@option options [Symbol] :raw

the raw mode toggle

@return [String]

@api private

# 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_blocking() click to toggle source
# File lib/tty/reader/win_console.rb, line 71
def get_char_blocking
  WinAPI.getch.chr
end
get_char_echo_blocking() click to toggle source
# File lib/tty/reader/win_console.rb, line 75
def get_char_echo_blocking
  WinAPI.getche.chr
end
get_char_echo_non_blocking() click to toggle source
# 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_blocking() click to toggle source

Get the char for last key pressed, or if no keypress return nil

@api private

# File lib/tty/reader/win_console.rb, line 63
def get_char_non_blocking
  input_ready? ? get_char_blocking : nil
end
input_ready?() click to toggle source

Check if IO has user input

@return [Boolean]

@api private

# File lib/tty/reader/win_console.rb, line 84
def input_ready?
  !WinAPI.kbhit.zero?
end