Class: TTY::Reader::WinConsole
- Inherits:
-
Object
- Object
- TTY::Reader::WinConsole
- Defined in:
- lib/tty/reader/win_console.rb
Constant Summary collapse
- ESC =
"\e"
- NUL_HEX =
"\x00"
- EXT_HEX =
"\xE0"
Instance Attribute Summary collapse
-
#escape_codes ⇒ Array[Integer]
readonly
Escape codes.
-
#keys ⇒ Hash[Symbol]
readonly
Key codes.
Instance Method Summary collapse
-
#get_char(echo: true, raw: false, nonblock: false) ⇒ 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(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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tty/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_blocking ⇒ Object
72 73 74 |
# File 'lib/tty/reader/win_console.rb', line 72 def get_char_blocking WinAPI.getch.chr end |
#get_char_echo_blocking ⇒ Object
76 77 78 |
# File 'lib/tty/reader/win_console.rb', line 76 def get_char_echo_blocking WinAPI.getche.chr end |
#get_char_echo_non_blocking ⇒ Object
68 69 70 |
# File 'lib/tty/reader/win_console.rb', line 68 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
64 65 66 |
# File 'lib/tty/reader/win_console.rb', line 64 def get_char_non_blocking input_ready? ? get_char_blocking : nil end |