Use the nice array based interface for fcntl.ioctl

This commit is contained in:
Kovid Goyal 2018-02-20 17:01:14 +05:30
parent e44910212c
commit 95384d437a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 9 additions and 9 deletions

View File

@ -46,10 +46,10 @@ printf("number of columns: %i, number of rows: %i, screen width: %i, screen heig
In Python:
```py
import struct, fcntl, termios
s = struct.pack('HHHH', 0, 0, 0, 0)
x = struct.unpack('HHHH', fcntl.ioctl(1, termios.TIOCGWINSZ, s))
print(f'number of columns: {x[0]}, number of rows: {x[1]}, screen width: {x[2]}, screen height: {x[3]}')
import array, fcntl, termios
buf = array.array('H', [0, 0, 0, 0])
fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf)
print('number of columns: {}, number of rows: {}, screen width: {}, screen height: {}'.format(*buf))
```
Note that some terminals return `0` for the width and height values. Such terminals should be modified to return the correct values.

View File

@ -2,13 +2,13 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import array
import codecs
import fcntl
import mimetypes
import os
import re
import signal
import struct
import subprocess
import sys
import termios
@ -18,8 +18,8 @@
from math import ceil, floor
from tempfile import NamedTemporaryFile
from kitty.constants import appname
from kitty.cli import parse_args
from kitty.constants import appname
from kitty.utils import read_with_timeout
try:
@ -105,9 +105,9 @@ def options_spec():
def screen_size():
if screen_size.changed:
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
screen_size.ans = Size(*struct.unpack('HHHH', x))
buf = array.array('H', [0, 0, 0, 0])
fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf)
screen_size.ans = Size(*buf)
screen_size.changed = False
return screen_size.ans