Skip to content

Instantly share code, notes, and snippets.

@eqvinox
Created December 23, 2025 18:21
Show Gist options
  • Select an option

  • Save eqvinox/979ea1bc439874a069c35b1507b5f814 to your computer and use it in GitHub Desktop.

Select an option

Save eqvinox/979ea1bc439874a069c35b1507b5f814 to your computer and use it in GitHub Desktop.
set terminal size via CSI query
#!/usr/bin/python3
import sys
import os
import tty
import termios
import re
import struct
import fcntl
stdin = sys.stdin.fileno()
stdout = sys.stdout.fileno()
prevattr = termios.tcgetattr(stdin)
try:
tty.setcbreak(stdin)
os.write(stdout, b'\033[18;t')
reply = os.read(stdin, 256)
finally:
termios.tcsetattr(stdin, termios.TCSANOW, prevattr)
m = re.match(rb'^\033\[8;(?P<rows>\d+);(?P<cols>\d+)t$', reply)
if m:
rows, cols = int(m.group("rows").decode("ASCII")), int(m.group("cols").decode("ASCII"))
args = struct.pack('HHHH', rows, cols, 0, 0)
fcntl.ioctl(stdin, termios.TIOCSWINSZ, args)
else:
sys.stderr.write("terminal size query failed\n")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment