Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Reline::IOGate dynamically when it is needed #560

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,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 @@ -288,6 +289,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 @@ -584,6 +586,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