-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui.py
More file actions
67 lines (52 loc) · 1.48 KB
/
tui.py
File metadata and controls
67 lines (52 loc) · 1.48 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import shutil
import signal
import sys
# for reading pressed key
# import termios
# import tty
# STDIN_FD = sys.stdin.fileno()
# old_term = termios.tcgetattr(STDIN_FD)
resized = True
width = 0
height = 0
def handle_resize(signum, frame): # noqa
"""Handle terminal resize event"""
global resized
resized = True
signal.signal(signal.SIGWINCH, handle_resize)
def enter_raw():
"""Enter raw terminal mode"""
# tty.setraw(STDIN_FD)
sys.stdout.write(
"\x1b[?1049h" # alternate screen
"\x1b[?25l" # hide cursor
"\x1b[2J" # clear screen
"\x1b[H" # cursor home
"\x1b[?7l", # disable line wrap
)
sys.stdout.flush()
def leave_raw():
"""Leave raw terminal mode"""
sys.stdout.write(
"\x1b[?25h" # show cursor
"\x1b[?7h" # enable line wrap
"\x1b[?1049l" # leave alternate screen
"\x1b[0m", # reset attrs
)
sys.stdout.flush()
# termios.tcsetattr(STDIN_FD, termios.TCSADRAIN, old_term)
def get_size():
"""Get size of terminal in characters (h, w)"""
size = shutil.get_terminal_size()
return size.lines, size.columns
# def read_key():
# """Read pressed key"""
# r, _, _ = select.select([sys.stdin], [], [], 0)
# if r:
# return sys.stdin.read(1)
# return None
def draw(lines):
"""Draw lines on screen"""
sys.stdout.write("\x1b[H") # cursor home
sys.stdout.write("\n".join(lines))
sys.stdout.flush()