Skip to content

Commit

Permalink
[ruby/reline] Update Relin::IOGate dinamically when it is needed
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng authored and matzbot committed Jul 5, 2023
1 parent 5f07f78 commit 6f9d1b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def get_screen_size
Reline::DEFAULT_DIALOG_CONTEXT = Array.new

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')
Expand All @@ -284,6 +285,7 @@ def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
end

def readline(prompt = '', add_hist = false)
Reline.update_iogate
inner_readline(prompt, add_hist, false)

line = line_editor.line.dup
Expand Down Expand Up @@ -580,6 +582,18 @@ def self.ungetc(c)
def self.line_editor
core.line_editor
end

def self.update_iogate
return if core.config.test_mode

# 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)
end
end
end

require 'reline/general_io'
Expand Down
1 change: 1 addition & 0 deletions test/reline/test_reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def call; end
end

def setup
Reline.send(:test_mode)
Reline.output_modifier_proc = nil
Reline.completion_proc = nil
Reline.prompt_proc = nil
Expand Down

0 comments on commit 6f9d1b4

Please sign in to comment.