File: on_resize.py

package info (click to toggle)
python-blessed 1.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,812 kB
  • sloc: python: 14,645; makefile: 13; sh: 7
file content (47 lines) | stat: -rwxr-xr-x 1,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
import threading
from blessed import Terminal

term = Terminal()

_resize_pending = threading.Event()


def on_resize(*args):
    _resize_pending.set()


def display_size(term):
    # conditionally refresh sixel size when enabled
    sixel_height, sixel_width = 0, 0
    if term.does_sixel():
        sixel_height, sixel_width = term.get_sixel_height_and_width(force=True)
    print()
    print(f'height={term.height}, width={term.width}, ' +
          f'pixel_height={term.pixel_height}, pixel_width={term.pixel_width}, ' +
          f'sixel_height={sixel_height}, sixel_width={sixel_width}',
          end='', flush=True)


if not term.does_inband_resize():
    print('In-band Window Resize not supported on this terminal')
    import sys
    if sys.platform != 'win32':
        import signal
        signal.signal(signal.SIGWINCH, on_resize)

with term.cbreak(), term.notify_on_resize():
    print("press 'q' to quit.")
    display_size(term)

    while True:
        inp = term.inkey(timeout=0.1)

        if inp == 'q':
            break

        if inp.name == 'RESIZE_EVENT':
            _resize_pending.set()
        elif _resize_pending.is_set():
            _resize_pending.clear()
            display_size(term)