Skip to content

Commit c52b17b

Browse files
committed
Query ioctl first for terminal dimensions in pixels, fallback to ANSI escape code
1 parent 7b25fdd commit c52b17b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

matplotlib-backend-kitty/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# SPDX-License-Identifier: CC0-1.0
22

3+
import array
4+
import fcntl
35
import os
46
import re
57
import sys
@@ -21,6 +23,14 @@
2123
interactive(True)
2224

2325
def term_size_px():
26+
# try to get terminal size from ioctl
27+
buf = array.array("H", [0, 0, 0, 0])
28+
fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf)
29+
_, _, width_px, height_px = buf
30+
if width_px != 0 and height_px != 0:
31+
return height_px, width_px
32+
33+
# fallback to ANSI escape code if ioctl fails
2434
buf = ''
2535
stdin = sys.stdin.fileno()
2636
tattr = termios.tcgetattr(stdin)

0 commit comments

Comments
 (0)