Skip to content

Instantly share code, notes, and snippets.

@sbz
Last active September 1, 2024 20:05
Show Gist options
  • Save sbz/bca2d1327910e0d8cadff1835625cbf2 to your computer and use it in GitHub Desktop.
Save sbz/bca2d1327910e0d8cadff1835625cbf2 to your computer and use it in GitHub Desktop.
get terminal size using ioctl get win size (TIOCGWINSZ) in Python
import termios
import fcntl
import os
import struct
with open(os.ctermid(), 'r') as fd:
packed = fcntl.ioctl(fd, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))
rows, cols, h_pixels, v_pixels = struct.unpack('HHHH', packed)
print rows, cols, h_pixels, v_pixels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment