From 6f9d1b4b0f8938c42aa767577c7c6761ed9de64a Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Thu, 6 Jul 2023 05:21:09 +0900 Subject: [ruby/reline] Update Relin::IOGate dinamically when it is needed (https://github.com/ruby/reline/pull/560) https://github.com/ruby/reline/commit/4680d1c9e0 --- lib/reline.rb | 14 ++++++++++++++ test/reline/test_reline.rb | 1 + 2 files changed, 15 insertions(+) diff --git a/lib/reline.rb b/lib/reline.rb index 7776bf4726..8da614aa2b 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -266,6 +266,7 @@ module Reline 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') @@ -284,6 +285,7 @@ module Reline end def readline(prompt = '', add_hist = false) + Reline.update_iogate inner_readline(prompt, add_hist, false) line = line_editor.line.dup @@ -580,6 +582,18 @@ module Reline 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' diff --git a/test/reline/test_reline.rb b/test/reline/test_reline.rb index 460fb7872d..33387231b4 100644 --- a/test/reline/test_reline.rb +++ b/test/reline/test_reline.rb @@ -8,6 +8,7 @@ class Reline::Test < Reline::TestCase end def setup + Reline.send(:test_mode) Reline.output_modifier_proc = nil Reline.completion_proc = nil Reline.prompt_proc = nil -- cgit v1.2.3