Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions invoke/terminals.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,18 @@ def _pty_size() -> Tuple[Optional[int], Optional[int]]:
# Sentinel values to be replaced w/ defaults by caller
size = (None, None)
# We want two short unsigned integers (rows, cols)
fmt = "HH"
# Note: TIOCGWINSZ struct contains 4 unsigned shorts, 2 unused
fmt = "HHHH"
# Create an empty (zeroed) buffer for ioctl to map onto. Yay for C!
buf = struct.pack(fmt, 0, 0)
buf = struct.pack(fmt, 0, 0, 0, 0)
# Call TIOCGWINSZ to get window size of stdout, returns our filled
# buffer
try:
result = fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf)
# Unpack buffer back into Python data types
# NOTE: this unpack gives us rows x cols, but we return the
# inverse.
rows, cols = struct.unpack(fmt, result)
rows, cols, *_ = struct.unpack(fmt, result)
return (cols, rows)
# Fallback to emptyish return value in various failure cases:
# * sys.stdout being monkeypatched, such as in testing, and lacking
Expand Down