From b37c1c2db92c0c9277de8ece09ece5a76cee505e Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Tue, 28 Jan 2025 03:03:27 +0900 Subject: [PATCH] Zero winsize bugfix (#1073) * Page overflow calculation should work even with winsize=[0,0] * InputMethod#winsize fallback to [24, 80] if width or height is zero --- lib/irb/input-method.rb | 7 ++++--- lib/irb/pager.rb | 2 +- test/irb/test_pager.rb | 12 ++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index f7f0c80ae..b9bbdeb1e 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -26,10 +26,11 @@ def gets def winsize if instance_variable_defined?(:@stdout) && @stdout.tty? - @stdout.winsize - else - [24, 80] + winsize = @stdout.winsize + # If width or height is 0, something is wrong. + return winsize unless winsize.include? 0 end + [24, 80] end # Whether this input method is still readable when there is no more data to diff --git a/lib/irb/pager.rb b/lib/irb/pager.rb index 16ff30cf8..89e1e7100 100644 --- a/lib/irb/pager.rb +++ b/lib/irb/pager.rb @@ -189,7 +189,7 @@ def write(text) end end @buffer.clear - @buffer << @lines.pop unless @lines.last.end_with?("\n") + @buffer << @lines.pop if !@lines.empty? && !@lines.last.end_with?("\n") @col = Reline::Unicode.calculate_width(@buffer, true) end if overflow || @lines.size > @height || (@lines.size == @height && @col > 0) diff --git a/test/irb/test_pager.rb b/test/irb/test_pager.rb index 0fad94da3..5a61b0e91 100644 --- a/test/irb/test_pager.rb +++ b/test/irb/test_pager.rb @@ -70,5 +70,17 @@ def test_callback_delay assert_equal 'a' * 1000 + 'bcd', out.string assert_equal [:before_delay, [:callback_called, ['a' * 10] * 4], :after_delay], actual_events end + + def test_zero_width + out = IRB::Pager::PageOverflowIO.new(0, 0, ->*{}) + 100.times { out.write 'a' } + assert_equal [true, [], 'a' * 100], [out.multipage?, out.first_page_lines, out.string] + out = IRB::Pager::PageOverflowIO.new(10, 0, ->*{}) + 100.times { out.write 'a' } + assert_equal [true, [], 'a' * 100], [out.multipage?, out.first_page_lines, out.string] + out = IRB::Pager::PageOverflowIO.new(0, 10, ->*{}) + 100.times { out.write 'a' } + assert_equal [true, [], 'a' * 100], [out.multipage?, out.first_page_lines, out.string] + end end end