Class: TTY::Reader::WinConsole
- Inherits:
-
Object
- Object
- TTY::Reader::WinConsole
- 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
-
#escape_codes ⇒ Array[Integer]
readonly
Escape codes.
-
#keys ⇒ Hash[Symbol]
readonly
Key codes.
Instance Method Summary collapse
-
#get_char(options) ⇒ String
private
Get a character from console blocking for input.
- #get_char_blocking ⇒ Object
- #get_char_echo_blocking ⇒ Object
- #get_char_echo_non_blocking ⇒ Object
-
#get_char_non_blocking ⇒ Object
private
Get the char for last key pressed, or if no keypress return nil.
-
#initialize(input) ⇒ WinConsole
constructor
A new instance of WinConsole.
-
#input_ready? ⇒ Boolean
private
Check if IO has user input.
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_codes ⇒ Array[Integer] (readonly)
Escape codes
24 25 26 |
# File 'lib/tty/reader/win_console.rb', line 24 def escape_codes @escape_codes end |
#keys ⇒ Hash[Symbol] (readonly)
Key codes
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
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() 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_blocking ⇒ Object
71 72 73 |
# File 'lib/tty/reader/win_console.rb', line 71 def get_char_blocking WinAPI.getch.chr end |
#get_char_echo_blocking ⇒ Object
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_blocking ⇒ Object
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_blocking ⇒ Object
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 |