Skip to content

Instantly share code, notes, and snippets.

@tompng
Last active January 22, 2025 06:23
Show Gist options
  • Save tompng/acecff6e918ee98e57fa4fb67a32b4ca to your computer and use it in GitHub Desktop.
Save tompng/acecff6e918ee98e57fa4fb67a32b4ca to your computer and use it in GitHub Desktop.
require 'irb'
require 'stringio'
def count_lines(ascii_text, width)
ascii_text.lines.sum{|l| [(l.chomp.size-1)/40+1,1].max }
end
10000.times do |i|
width = 40
height= 20
out = IRB::Pager::PageOverflowIO.new(width, height, ->*{})
chars = [*'a'..'f', "\n"]
escs = ["\e[1m", "\e[31;42m", ""]
buf = StringIO.new
rand(2..50).times do
s = (rand**3*100).floor.times.map{chars.sample+escs.sample}.join
if rand < 0.95
out.write(s)
buf.write(s)
else
out.puts(s)
buf.puts(s)
end
end
ascii_all = out.string.gsub(/\e\[[\d;]+m/,'')
ascii_firstpage = (out.first_page_lines&.join||out.string).gsub(/\e\[[\d;]+m/,'')
raise unless out.string == buf.string
if out.multipage?
p i => :multipage
raise unless count_lines(ascii_firstpage, width) == height
raise unless count_lines(ascii_all[0, ascii_firstpage.size + 1], width) != height
raise unless ascii_all.start_with?(ascii_firstpage)
else
p i => :single
raise unless count_lines(ascii_firstpage, width) <= height
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment