Skip to content

Instantly share code, notes, and snippets.

@mattn
Created February 18, 2026 12:58
Show Gist options
  • Select an option

  • Save mattn/1eadbaf27ef12d55280b52827e2c0039 to your computer and use it in GitHub Desktop.

Select an option

Save mattn/1eadbaf27ef12d55280b52827e2c0039 to your computer and use it in GitHub Desktop.
import fcntl
import termios
import struct
import string
import random
import time
l = list(string.ascii_lowercase + string.ascii_uppercase)
t = 100
def get_terminal_size() -> tuple[int, int] | tuple[None, None]:
# Use fileno(0) for stdin
try:
# TIOCGWINSZ is defined as 0x5413 in many systems
# struct.unpack('HHHH', ...) packs rows, cols, xpix, ypix
size = fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))
rows, cols, xpix, ypix = struct.unpack('HHHH', size)
return rows, cols
except IOError:
return None, None
def main():
rows, cols = get_terminal_size()
if rows == None:
print("error")
return
print("\033[2J", end="")
print("\033[1;{}r".format(rows-1), end="")
for v in range(t):
c = random.choice(l)
# スクロール領域の最下行に移動し、改行でスクロールさせてから内容を表示
print("\033[{};1H\n{}".format(rows-1, c * cols), end="")
# 最下行(スクロール領域外)にプログレスバーを描画
print("\033[{};1H\033[2K{}".format(rows, "#" * int(cols*(v+1)//t)), end="", flush=True)
time.sleep(0.3)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment