Last active
July 22, 2025 16:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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