Make the signal handler a little simpler
This commit is contained in:
parent
03702772d3
commit
677e8a1ae9
@ -103,14 +103,18 @@ def options_spec():
|
|||||||
Size = namedtuple('Size', 'rows cols width height')
|
Size = namedtuple('Size', 'rows cols width height')
|
||||||
|
|
||||||
|
|
||||||
def screen_size(refresh=False):
|
def screen_size():
|
||||||
if refresh or getattr(screen_size, 'ans', None) is None:
|
if screen_size.changed:
|
||||||
s = struct.pack('HHHH', 0, 0, 0, 0)
|
s = struct.pack('HHHH', 0, 0, 0, 0)
|
||||||
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
|
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
|
||||||
screen_size.ans = Size(*struct.unpack('HHHH', x))
|
screen_size.ans = Size(*struct.unpack('HHHH', x))
|
||||||
|
screen_size.changed = False
|
||||||
return screen_size.ans
|
return screen_size.ans
|
||||||
|
|
||||||
|
|
||||||
|
screen_size.changed = True
|
||||||
|
|
||||||
|
|
||||||
def write_gr_cmd(cmd, payload=None):
|
def write_gr_cmd(cmd, payload=None):
|
||||||
cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items())
|
cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items())
|
||||||
w = sys.stdout.buffer.write
|
w = sys.stdout.buffer.write
|
||||||
@ -319,7 +323,7 @@ def main(args=sys.argv):
|
|||||||
' Directories are scanned recursively for image files.')
|
' Directories are scanned recursively for image files.')
|
||||||
args, items = parse_args(args[1:], options_spec, 'image-file ...', msg, '{} icat'.format(appname))
|
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():
|
if not sys.stdout.isatty() or not sys.stdin.isatty():
|
||||||
raise SystemExit(
|
raise SystemExit(
|
||||||
'Must be run in a terminal, stdout and/or stdin is currently not a terminal'
|
'Must be run in a terminal, stdout and/or stdin is currently not a terminal'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user