diff options
author | Stan Lo <[email protected]> | 2023-07-07 18:27:14 +0100 |
---|---|---|
committer | git <[email protected]> | 2023-07-07 17:27:25 +0000 |
commit | 24d9e21f84eced634b3e443ada2a57621b2b4b9b () | |
tree | 96ecaa8228d6b04c80b48047e8a77abc15b5cb61 /lib/reline.rb | |
parent | a642a94b68a31c0e7c092087114c269132cdb159 (diff) |
[ruby/reline] Reduce direct references to `Reline::IOGate`
(https://.com/ruby/reline/pull/566) * Avoid referencing IOGate from IOGate classes The only time those classes being used is when themselves being the IOGate. So when referencing to IOGate, it's better to use `self` instead. * Avoid referencing to IOGate from LineEditor directly * Avoid referencing to IOGate from Core directly * Reference to Reline.core directly * Replace Reline::IOGate with Reline.core.io_gate
-rw-r--r-- | lib/reline.rb | 60 |
1 files changed, 32 insertions, 28 deletions
@@ -83,8 +83,12 @@ module Reline @bracketed_paste_finished = false end def encoding - Reline::IOGate.encoding end def completion_append_character=(val) @@ -181,16 +185,16 @@ module Reline def input=(val) raise TypeError unless val.respond_to?(:getc) or val.nil? - if val.respond_to?(:getc) && Reline::IOGate.respond_to?(:input=) - Reline::IOGate.input = val end end def output=(val) raise TypeError unless val.respond_to?(:write) or val.nil? @output = val - if Reline::IOGate.respond_to?(:output=) - Reline::IOGate.output = val end end @@ -213,7 +217,7 @@ module Reline end def get_screen_size - Reline::IOGate.get_screen_size end Reline::DEFAULT_DIALOG_PROC_AUTOCOMPLETE = ->() { @@ -267,7 +271,7 @@ module Reline def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination) Reline.update_iogate - Reline::IOGate.with_raw_input do unless confirm_multiline_termination raise ArgumentError.new('#readmultiline needs block to confirm multiline termination') end @@ -300,7 +304,7 @@ module Reline private def inner_readline(prompt, add_hist, multiline, &confirm_multiline_termination) if ENV['RELINE_STDERR_TTY'] - if Reline::IOGate.win? $stderr = File.open(ENV['RELINE_STDERR_TTY'], 'a') else $stderr.reopen(ENV['RELINE_STDERR_TTY'], 'w') @@ -308,7 +312,7 @@ module Reline $stderr.sync = true $stderr.puts "Reline is used by #{Process.pid}" end - otio = Reline::IOGate.prep may_req_ambiguous_char_width line_editor.reset(prompt, encoding: encoding) @@ -335,7 +339,7 @@ module Reline unless config.test_mode config.read config.reset_default_key_bindings - Reline::IOGate.set_default_key_bindings(config) end line_editor.rerender @@ -344,9 +348,9 @@ module Reline line_editor.set_signal_handlers prev_pasting_state = false loop do - prev_pasting_state = Reline::IOGate.in_pasting? read_io(config.keyseq_timeout) { |inputs| - line_editor.set_pasting_state(Reline::IOGate.in_pasting?) inputs.each { |c| line_editor.input_key(c) line_editor.rerender @@ -356,29 +360,29 @@ module Reline @bracketed_paste_finished = false end } - if prev_pasting_state == true and not Reline::IOGate.in_pasting? and not line_editor.finished? line_editor.set_pasting_state(false) prev_pasting_state = false line_editor.rerender_all end break if line_editor.finished? end - Reline::IOGate.move_cursor_column(0) rescue Errno::EIO # Maybe the I/O has been closed. rescue StandardError => e line_editor.finalize - Reline::IOGate.deprep(otio) raise e rescue Exception # Including Interrupt line_editor.finalize - Reline::IOGate.deprep(otio) raise end line_editor.finalize - Reline::IOGate.deprep(otio) end # GNU Readline waits for "keyseq-timeout" milliseconds to see if the ESC @@ -393,7 +397,7 @@ module Reline private def read_io(keyseq_timeout, &block) buffer = [] loop do - c = Reline::IOGate.getc if c == -1 result = :unmatched @bracketed_paste_finished = true @@ -433,7 +437,7 @@ module Reline begin succ_c = nil Timeout.timeout(keyseq_timeout / 1000.0) { - succ_c = Reline::IOGate.getc } rescue Timeout::Error # cancel matching only when first byte block.([Reline::Key.new(c, c, false)]) @@ -448,7 +452,7 @@ module Reline end return :break when :matching - Reline::IOGate.ungetc(succ_c) return :next when :matched buffer << succ_c @@ -465,7 +469,7 @@ module Reline begin escaped_c = nil Timeout.timeout(keyseq_timeout / 1000.0) { - escaped_c = Reline::IOGate.getc } rescue Timeout::Error # independent ESC block.([Reline::Key.new(c, c, false)]) @@ -488,19 +492,19 @@ module Reline end private def may_req_ambiguous_char_width - @ambiguous_width = 2 if Reline::IOGate == Reline::GeneralIO or !STDOUT.tty? return if defined? @ambiguous_width - Reline::IOGate.move_cursor_column(0) begin output.write "\u{25bd}" rescue Encoding::UndefinedConversionError # LANG=C @ambiguous_width = 1 else - @ambiguous_width = Reline::IOGate.cursor_pos.x end - Reline::IOGate.move_cursor_column(0) - Reline::IOGate.erase_after_cursor end end @@ -576,7 +580,7 @@ module Reline end def self.ungetc(c) - Reline::IOGate.ungetc(c) end def self.line_editor @@ -588,7 +592,7 @@ module Reline # Need to change IOGate when `$stdout.tty?` change from false to true by `$stdout.reopen` # Example: rails/spring boot the application in non-tty, then run console in tty. - if ENV['TERM'] != 'dumb' && Reline::IOGate == Reline::GeneralIO && $stdout.tty? require 'reline/ansi' remove_const(:IOGate) const_set(:IOGate, Reline::ANSI) |