class Reline::Windows
Constants
- KEY_MAP
Attributes
Public Class Methods
Source
# File lib/reline/io/windows.rb, line 10 def initialize @input_buf = [] @output_buf = [] @input = STDIN @output = STDOUT @console_output = STDOUT @hsg = nil @legacy_console = legacy_console? end
Public Instance Methods
# File lib/reline/io/windows.rb, line 194 def buffered_output yield end
Source
# File lib/reline/io/windows.rb, line 158 def check_input_event while @output_buf.empty? Reline.core.line_editor.handle_signal events = @input.console_input_events(80, timeout: 0.1) if events.empty? # prevent for background consolemode change @legacy_console = legacy_console? next end events.each do |event| case event[:type] when :window_buffer_size @winch_handler.() when :key if event[:key_down] process_key_event( event[:repeat_count], event[:virtual_key_code], event[:virtual_scan_code], event[:unicode_char], event[:control_key_state] ) end end end end end
Source
# File lib/reline/io/windows.rb, line 267 def clear_screen if @legacy_console @console_output.clear_screen else @output.write "\e[2J" "\e[H" end rescue SystemCallError end
Source
# File lib/reline/io/windows.rb, line 221 def cursor_pos row, column = @console_output.cursor Reline::CursorPos.new(column, row) rescue SystemCallError Reline::CursorPos.new(0, 0) end
# File lib/reline/io/windows.rb, line 303 def disable_auto_linewrap(setting = true, &block) mode = console_mode if legacy_console_mode?(mode) if block wrap_at_eol = mode.wrap_at_eol_output? mode.wrap_at_eol_output = false return block.call unless set_console_mode(mode) begin block.call ensure mode.wrap_at_eol_output = wrap_at_eol set_console_mode(mode) end else mode.wrap_at_eol_output = !setting set_console_mode(mode) end else block.call if block end end
Source
# File lib/reline/io/windows.rb, line 211 def empty_buffer? @output_buf.empty? && !@input.input_pending? end
Source
# File lib/reline/io/windows.rb, line 254 def erase_after_cursor @console_output.erase_line(0) rescue SystemCallError end
Source
# File lib/reline/io/windows.rb, line 215 def get_screen_size @console_output.winsize rescue SystemCallError [24, 80] end
Source
# File lib/reline/io/windows.rb, line 198 def getc(_timeout_second) check_input_event @output_buf.shift end
Source
# File lib/reline/io/windows.rb, line 280 def hide_cursor @console_output.hide_cursor rescue SystemCallError end
Source
# File lib/reline/io/windows.rb, line 207 def in_pasting? not empty_buffer? end
Source
# File lib/reline/io/windows.rb, line 228 def move_cursor_column(val) @console_output.goto_column(val) rescue SystemCallError end
Source
# File lib/reline/io/windows.rb, line 243 def move_cursor_down(val) if val > 0 row, column = @console_output.cursor rows, = @console_output.winsize @console_output.goto([row + val, rows - 1].min, column) elsif val < 0 move_cursor_up(-val) end rescue SystemCallError end
Source
# File lib/reline/io/windows.rb, line 233 def move_cursor_up(val) if val > 0 row, column = @console_output.cursor @console_output.goto([row - val, 0].max, column) elsif val < 0 move_cursor_down(-val) end rescue SystemCallError end
Source
# File lib/reline/io/windows.rb, line 96 def msys_tty? @input.tty?(:msys, :cygwin) end
Source
# File lib/reline/io/windows.rb, line 121 def process_key_event(repeat_count, virtual_key_code, virtual_scan_code, char_code, control_key_state) # high-surrogate if 0xD800 <= char_code and char_code <= 0xDBFF @hsg = char_code return end # low-surrogate if 0xDC00 <= char_code and char_code <= 0xDFFF if @hsg char_code = 0x10000 + (@hsg - 0xD800) * 0x400 + char_code - 0xDC00 @hsg = nil else # no high-surrogate. ignored. return end else # ignore high-surrogate without low-surrogate if there @hsg = nil end key = KeyEventRecord.new(virtual_key_code, char_code, control_key_state) match = KEY_MAP.find { |args,| key.match?(**args) } unless match.nil? @output_buf.concat(match.last) return end # no char, only control keys return if key.char_code == 0 and key.control_keys.any? @output_buf.push("\e".ord) if key.control_keys.include?(:ALT) and !key.control_keys.include?(:CTRL) @output_buf.concat(key.char.bytes) end
Source
# File lib/reline/io/windows.rb, line 261 def scroll_down(x) return if x.zero? # We use `\n` instead of CSI + S because CSI + S would cause https://github.com/ruby/reline/issues/576 @output.write "\n" * x end
This only works when the cursor is at the bottom of the scroll range For more details, see github.com/ruby/reline/pull/577#issuecomment-1646679623
# File lib/reline/io/windows.rb, line 33 def set_default_key_bindings(config) { [224, 72] => :ed_prev_history, # ↑ [224, 80] => :ed_next_history, # ↓ [224, 77] => :ed_next_char, # → [224, 75] => :ed_prev_char, # ← [224, 83] => :key_delete, # Del [224, 71] => :ed_move_to_beg, # Home [224, 79] => :ed_move_to_end, # End [ 0, 72] => :ed_prev_history, # ↑ [ 0, 80] => :ed_next_history, # ↓ [ 0, 77] => :ed_next_char, # → [ 0, 75] => :ed_prev_char, # ← [ 0, 83] => :key_delete, # Del [ 0, 71] => :ed_move_to_beg, # Home [ 0, 79] => :ed_move_to_end # End }.each_pair do |key, func| config.add_default_key_binding_by_keymap(:emacs, key, func) config.add_default_key_binding_by_keymap(:vi_insert, key, func) config.add_default_key_binding_by_keymap(:vi_command, key, func) end { [27, 32] => :em_set_mark, # M-<space> [24, 24] => :em_exchange_mark, # C-x C-x }.each_pair do |key, func| config.add_default_key_binding_by_keymap(:emacs, key, func) end # Emulate ANSI key sequence. { [27, 91, 90] => :completion_journey_up, # S-Tab }.each_pair do |key, func| config.add_default_key_binding_by_keymap(:emacs, key, func) config.add_default_key_binding_by_keymap(:vi_insert, key, func) end end
# File lib/reline/io/windows.rb, line 276 def set_screen_size(rows, columns) raise NotImplementedError end
Source
# File lib/reline/io/windows.rb, line 290 def set_winch_handler(&handler) @winch_handler = handler end
Source
# File lib/reline/io/windows.rb, line 285 def show_cursor @console_output.show_cursor rescue SystemCallError end
Source
# File lib/reline/io/windows.rb, line 203 def ungetc(c) @output_buf.unshift(c) end
Source
# File lib/reline/io/windows.rb, line 29 def win_legacy_console? @legacy_console end
Source
# File lib/reline/io/windows.rb, line 186 def with_raw_input yield end
Source
# File lib/reline/io/windows.rb, line 190 def write(string) @output.write(string) end