Skip to content

Commit

Permalink
Revert "[ruby/reline] Reline::ANSI is general io. Reline::GeneralIO i…
Browse files Browse the repository at this point in the history
…s not."

This reverts commit ba01d15.

It seems to be failing test-bundler-parallel. Reverting it to normalize
the CI. We should revert this revert once we figure it out.
  • Loading branch information
k0kubun committed Jun 3, 2024
1 parent a8f5284 commit 6e84ac2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
16 changes: 15 additions & 1 deletion lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 254,7 @@ def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
end

Reline.update_iogate
io_gate.with_raw_input do
inner_readline(prompt, add_hist, true, &confirm_multiline_termination)
end
Expand All @@ -276,6 277,7 @@ def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)

def readline(prompt = '', add_hist = false)
@mutex.synchronize do
Reline.update_iogate
io_gate.with_raw_input do
inner_readline(prompt, add_hist, false)
end
Expand Down Expand Up @@ -459,7 461,7 @@ def ambiguous_width
end

private def may_req_ambiguous_char_width
@ambiguous_width = 2 if io_gate.dumb? || !STDIN.tty? || !STDOUT.tty?
@ambiguous_width = 2 if io_gate.dumb? or !STDOUT.tty?
return if defined? @ambiguous_width
io_gate.move_cursor_column(0)
begin
Expand Down Expand Up @@ -553,6 555,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' && core.io_gate.dumb? && $stdout.tty?
require 'reline/io/ansi'
remove_const(:IOGate)
const_set(:IOGate, Reline::ANSI.new)
end
end
end


Expand Down
6 changes: 5 additions & 1 deletion lib/reline/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 19,11 @@ def self.decide_io_gate
io
end
else
Reline::ANSI.new
if $stdout.tty?
Reline::ANSI.new
else
Reline::Dumb.new
end
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/reline/io/ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 174,12 @@ def inner_getc(timeout_second)
Reline.core.line_editor.handle_signal
end
c = @input.getbyte
(c == 0x16 && @input.tty? && @input.raw(min: 0, time: 0, &:getbyte)) || c
(c == 0x16 && @input.raw(min: 0, time: 0, &:getbyte)) || c
rescue Errno::EIO
# Maybe the I/O has been closed.
nil
rescue Errno::ENOTTY
nil
end

START_BRACKETED_PASTE = String.new("\e[200~", encoding: Encoding::ASCII_8BIT)
Expand Down Expand Up @@ -237,12 239,12 @@ def get_screen_size
def set_screen_size(rows, columns)
@input.winsize = [rows, columns]
self
rescue Errno::ENOTTY, Errno::ENODEV
rescue Errno::ENOTTY
self
end

def cursor_pos
if @input.tty? && @output.tty?
begin
res = ''
m = nil
@input.raw do |stdin|
Expand All @@ -261,7 263,7 @@ def cursor_pos
end
column = m[:column].to_i - 1
row = m[:row].to_i - 1
else
rescue Errno::ENOTTY
begin
buf = @output.pread(@output.pos, 0)
row = buf.count("\n")
Expand Down
12 changes: 0 additions & 12 deletions test/reline/yamatanooroti/test_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -969,18 969,6 @@ def test_force_enter
EOC
end

def test_nontty
omit if Reline.core.io_gate.win?
cmd = %Q{ruby -e 'puts(%Q{ello\C-ah\C-e})' | ruby -I#{@pwd}/lib -rreline -e 'p Reline.readline(%{> })' | ruby -e 'print STDIN.read'}
start_terminal(40, 50, ['bash', '-c', cmd])
sleep 1
close rescue nil
assert_screen(<<~'EOC')
> hello
"hello"
EOC
end

def test_eof_with_newline
omit if Reline.core.io_gate.win?
cmd = %Q{ruby -e 'print(%Q{abc def \\e\\r})' | ruby -I#{@pwd}/lib -rreline -e 'p Reline.readline(%{> })'}
Expand Down

0 comments on commit 6e84ac2

Please sign in to comment.