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: In Python:
```py ```py
import struct, fcntl, termios import array, fcntl, termios
s = struct.pack('HHHH', 0, 0, 0, 0) buf = array.array('H', [0, 0, 0, 0])
x = struct.unpack('HHHH', fcntl.ioctl(1, termios.TIOCGWINSZ, s)) fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf)
print(f'number of columns: {x[0]}, number of rows: {x[1]}, screen width: {x[2]}, screen height: {x[3]}') 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. 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 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import array
import codecs import codecs
import fcntl import fcntl
import mimetypes import mimetypes
import os import os
import re import re
import signal import signal
import struct
import subprocess import subprocess
import sys import sys
import termios import termios
@ -18,8 +18,8 @@ from collections import namedtuple
from math import ceil, floor from math import ceil, floor
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from kitty.constants import appname
from kitty.cli import parse_args from kitty.cli import parse_args
from kitty.constants import appname
from kitty.utils import read_with_timeout from kitty.utils import read_with_timeout
try: try:
@ -105,9 +105,9 @@ Size = namedtuple('Size', 'rows cols width height')
def screen_size(): def screen_size():
if screen_size.changed: if screen_size.changed:
s = struct.pack('HHHH', 0, 0, 0, 0) buf = array.array('H', [0, 0, 0, 0])
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s) fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf)
screen_size.ans = Size(*struct.unpack('HHHH', x)) screen_size.ans = Size(*buf)
screen_size.changed = False screen_size.changed = False
return screen_size.ans return screen_size.ans