Skip to content

Commit

Permalink
Zero winsize bugfix (#1073)
Browse files Browse the repository at this point in the history
* Page overflow calculation should work even with winsize=[0,0]

* InputMethod#winsize fallback to [24, 80] if width or height is zero
  • Loading branch information
tompng authored Jan 27, 2025
1 parent 095a1f7 commit b37c1c2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/irb/pager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/irb/test_pager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b37c1c2

Please sign in to comment.