Skip to content

Instantly share code, notes, and snippets.

@letsbreelhere
Last active July 22, 2025 16:44
Show Gist options
  • Save letsbreelhere/2f8408381aefe5c32c290e15a4150fe1 to your computer and use it in GitHub Desktop.
Save letsbreelhere/2f8408381aefe5c32c290e15a4150fe1 to your computer and use it in GitHub Desktop.
Rolling output window in a terminal - a little snippet for command line tools
class RollingOutputBuffer
def initialize(max_lines = 10)
@max_lines = max_lines
@lines = []
@last_printed = 0
end
def <<(line)
@lines << line
shift if @lines.size > @max_lines
end
def shift
@lines.shift
end
def unshift(line)
@lines.unshift(line)
end
def print
if @last_printed > 0
$stdout.print "\e[#{@last_printed}A"
end
$stdout.print "\e[0J#{@lines.join("\n")}\n"
@last_printed = @lines.size
end
def clear
$stdout.print "\e[#{@last_printed}A" if @last_printed > 0
$stdout.print "\e[0J"
@last_printed = 0
@lines = []
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment