Skip to content

Instantly share code, notes, and snippets.

@letsbreelhere
Created April 16, 2024 17:15
Show Gist options
  • Save letsbreelhere/2b164fc0f8928d535090c2f4ca6d805a to your computer and use it in GitHub Desktop.
Save letsbreelhere/2b164fc0f8928d535090c2f4ca6d805a to your computer and use it in GitHub Desktop.
Print stdin, but only scroll the most recent n lines to avoid clutter
#! /usr/bin/env python3
import sys
import time
n = 5
if len(sys.argv) > 1:
n = int(sys.argv[1])
# Save the cursor position
print("\033[s", end='')
lines = []
while True:
try:
line = input()
except EOFError:
break
# Clear the screen below the cursor
print("\033[0K", end='')
# Clear the screen above the cursor until saved position
print("\033[3J", end='')
lines.append(line)
lines = lines[-n:]
# Restore the cursor position
print("\033[u", end='')
for l in lines:
print(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment