Skip to content

Commit 2d68284

Browse files
authored
Reline::ANSI is general io. Reline::GeneralIO is not. (#659)
Reline::ANSI has a partial non-tty supporting code. It should be a general io. Reline::Dumb should be only used in testing.
1 parent 95ee80b commit 2d68284

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

lib/reline.rb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
254254
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
255255
end
256256

257-
Reline.update_iogate
258257
io_gate.with_raw_input do
259258
inner_readline(prompt, add_hist, true, &confirm_multiline_termination)
260259
end
@@ -277,7 +276,6 @@ def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
277276

278277
def readline(prompt = '', add_hist = false)
279278
@mutex.synchronize do
280-
Reline.update_iogate
281279
io_gate.with_raw_input do
282280
inner_readline(prompt, add_hist, false)
283281
end
@@ -461,7 +459,7 @@ def ambiguous_width
461459
end
462460

463461
private def may_req_ambiguous_char_width
464-
@ambiguous_width = 2 if io_gate.dumb? or !STDOUT.tty?
462+
@ambiguous_width = 2 if io_gate.dumb? || !STDIN.tty? || !STDOUT.tty?
465463
return if defined? @ambiguous_width
466464
io_gate.move_cursor_column(0)
467465
begin
@@ -555,18 +553,6 @@ def self.ungetc(c)
555553
def self.line_editor
556554
core.line_editor
557555
end
558-
559-
def self.update_iogate
560-
return if core.config.test_mode
561-
562-
# Need to change IOGate when `$stdout.tty?` change from false to true by `$stdout.reopen`
563-
# Example: rails/spring boot the application in non-tty, then run console in tty.
564-
if ENV['TERM'] != 'dumb' && core.io_gate.dumb? && $stdout.tty?
565-
require 'reline/io/ansi'
566-
remove_const(:IOGate)
567-
const_set(:IOGate, Reline::ANSI.new)
568-
end
569-
end
570556
end
571557

572558

lib/reline/io.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ def self.decide_io_gate
1919
io
2020
end
2121
else
22-
if $stdout.tty?
23-
Reline::ANSI.new
24-
else
25-
Reline::Dumb.new
26-
end
22+
Reline::ANSI.new
2723
end
2824
end
2925
end

lib/reline/io/ansi.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,10 @@ def inner_getc(timeout_second)
174174
Reline.core.line_editor.handle_signal
175175
end
176176
c = @input.getbyte
177-
(c == 0x16 && @input.raw(min: 0, time: 0, &:getbyte)) || c
177+
(c == 0x16 && @input.tty? && @input.raw(min: 0, time: 0, &:getbyte)) || c
178178
rescue Errno::EIO
179179
# Maybe the I/O has been closed.
180180
nil
181-
rescue Errno::ENOTTY
182-
nil
183181
end
184182

185183
START_BRACKETED_PASTE = String.new("\e[200~", encoding: Encoding::ASCII_8BIT)
@@ -239,12 +237,12 @@ def get_screen_size
239237
def set_screen_size(rows, columns)
240238
@input.winsize = [rows, columns]
241239
self
242-
rescue Errno::ENOTTY
240+
rescue Errno::ENOTTY, Errno::ENODEV
243241
self
244242
end
245243

246244
def cursor_pos
247-
begin
245+
if @input.tty? && @output.tty?
248246
res = +''
249247
m = nil
250248
@input.raw do |stdin|
@@ -263,7 +261,7 @@ def cursor_pos
263261
end
264262
column = m[:column].to_i - 1
265263
row = m[:row].to_i - 1
266-
rescue Errno::ENOTTY
264+
else
267265
begin
268266
buf = @output.pread(@output.pos, 0)
269267
row = buf.count("\n")

test/reline/yamatanooroti/test_rendering.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,18 @@ def test_force_enter
969969
EOC
970970
end
971971

972+
def test_nontty
973+
omit if Reline.core.io_gate.win?
974+
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'}
975+
start_terminal(40, 50, ['bash', '-c', cmd])
976+
sleep 1
977+
close rescue nil
978+
assert_screen(<<~'EOC')
979+
> hello
980+
"hello"
981+
EOC
982+
end
983+
972984
def test_eof_with_newline
973985
omit if Reline.core.io_gate.win?
974986
cmd = %Q{ruby -e 'print(%Q{abc def \\e\\r})' | ruby -I#{@pwd}/lib -rreline -e 'p Reline.readline(%{> })'}

0 commit comments

Comments
 (0)