Created
April 16, 2024 17:15
-
-
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
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
#! /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