From 95384d437a65e4c8862e449db542cdf8a574f281 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 20 Feb 2018 17:01:14 +0530 Subject: [PATCH] Use the nice array based interface for fcntl.ioctl --- graphics-protocol.asciidoc | 8 ++++---- kitty/icat.py | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/graphics-protocol.asciidoc b/graphics-protocol.asciidoc index dc44f1122..2a422ebee 100644 --- a/graphics-protocol.asciidoc +++ b/graphics-protocol.asciidoc @@ -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. diff --git a/kitty/icat.py b/kitty/icat.py index 581edef7e..4350f4b3a 100755 --- a/kitty/icat.py +++ b/kitty/icat.py @@ -2,13 +2,13 @@ # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2017, Kovid Goyal +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