-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncursed.py
78 lines (62 loc) · 1.92 KB
/
uncursed.py
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
68
69
70
71
72
73
74
75
76
77
78
import time
import string
import curses
import curses.panel as panel
def max_line_len(s):
return max([len(i) for i in s.splitlines()])
def centered_xcoord(win, s):
nrows, ncols = win.getmaxyx()
if isinstance(s, int):
maxlen = s
else:
maxlen = max_line_len(s)
return (ncols // 2) - (maxlen // 2)
def centered_ycoord(win, s):
nrows, ncols = win.getmaxyx()
if isinstance(s, int):
nlines = s
else:
nlines = len(str(s).splitlines())
return (nrows // 2) - (nlines // 2)
def adjust_lines(s, func=str.ljust):
maxlen = max_line_len(s)
adj_lines = [func(i, maxlen) for i in s.splitlines()]
return "\n".join(adj_lines)
def add_multiline_str(win, y, x, s):
lines = s.splitlines()
for i, line in enumerate(lines):
win.addstr(y + i, x, line)
def set_status_bar(win, s, loc="bottom"):
nrows, ncols = win.getmaxyx()
y = 0 if (loc == "top") else (nrows - 1)
win.move(y, 0)
win.clrtoeol()
win.addstr(y, 0, s)
def new_text_win(s, padding = 1, just = None):
nlines = len(s.splitlines()) + 2 * padding
ncols = max_line_len(s) + 2 * padding
win = curses.newwin(nlines, ncols)
if just is not None:
s = adjust_lines(s, just)
add_multiline_str(win, padding, padding, s)
return win
def setup_screen(screen, nonblocking=True):
screen.clear()
screen.nodelay(int(nonblocking))
curses.curs_set(0)
def test(screen, s, just=None):
setup_screen(screen, nonblocking=True)
win = new_text_win(s, just = just)
h, w = win.getmaxyx()
y, x = centered_ycoord(screen, h), centered_xcoord(screen, w)
pnl = panel.new_panel(win)
pnl.move(y,x)
set_status_bar(screen, "Push 'q' to quit")
screen.noutrefresh()
panel.update_panels()
curses.doupdate()
# loop until receive 'q'
while True:
c = screen.getch()
if c in (ord('q'), ord('Q')):
break