Make the signal handler a little simpler

This commit is contained in:
Kovid Goyal 2018-01-10 13:37:43 +05:30
parent 03702772d3
commit 677e8a1ae9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -103,14 +103,18 @@ def options_spec():
Size = namedtuple('Size', 'rows cols width height')
def screen_size(refresh=False):
if refresh or getattr(screen_size, 'ans', None) is None:
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))
screen_size.changed = False
return screen_size.ans
screen_size.changed = True
def write_gr_cmd(cmd, payload=None):
cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items())
w = sys.stdout.buffer.write
@ -319,7 +323,7 @@ def main(args=sys.argv):
' Directories are scanned recursively for image files.')
args, items = parse_args(args[1:], options_spec, 'image-file ...', msg, '{} icat'.format(appname))
signal.signal(signal.SIGWINCH, lambda: screen_size(refresh=True))
signal.signal(signal.SIGWINCH, lambda: setattr(screen_size, 'changed', True))
if not sys.stdout.isatty() or not sys.stdin.isatty():
raise SystemExit(
'Must be run in a terminal, stdout and/or stdin is currently not a terminal'