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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#!/usr/bin/env python3
import curses
import threading
from .globals import CURSES_LOCK
class Pager:
def set_dim(self, height, width):
if height < 4 or width < 10:
raise ValueError("Size too small")
with self._lock:
if (height, width) != (self._height, self._width):
self.win.erase()
self.win.noutrefresh()
self.win.resize(height, width)
self.win.redrawwin()
self._height = height
self._width = width
self._regen_actual()
self._redraw()
def set_pos(self, y, x):
with self._lock:
if (y, x) != (self._y, self._x):
self.win.mvwin(y, x)
self._y = y
self._x = x
self.win.noutrefresh()
def _redraw(self):
with self._lock, CURSES_LOCK:
start = max(0, self._bottom_a - self._height + 2)
end = self._bottom_a
todisp = self._actual[start:end]
if len(todisp) < self._height-2 and len(todisp) < len(self._actual):
end = self._bottom_a+self._height-2-len(todisp)
todisp += self._actual[self._bottom_a:end]
self.win.erase()
self.win.border()
if start > 0:
self.win.addch(0, self._width//2, '⯅')
for n, l in enumerate(todisp, 1):
self.win.addstr(n, 1, l)
if end < len(self._actual):
self.win.addch(self._height-1, self._width//2, '⯆')
self.win.refresh()
def _split_line(self, l):
with self._lock:
lines = []
while len(l) >= self._width-2:
lines.append(l[:self._width-3] + '…')
l = "⮡ "+l[self._width-3:]
lines.append(l)
return lines
def _regen_actual(self):
with self._lock:
self._actual = []
for n, l in enumerate(self._lines):
self._actual += self._split_line(l)
if n == self._bottom:
self._bottom_a = len(self._actual)
self._bottom_a = max(self._bottom_a, self._height-2)
def display_many(self, s, split=False):
with self._lock:
if split and self._lines:
self._lines.append("")
self._actual.append("")
for l in s:
self._lines.append(l)
self._actual += self._split_line(l)
self._bottom = len(self._lines) - 1
self._bottom_a = len(self._actual)
self._redraw()
def display(self, s):
self.display_many((s, ))
def user_page(self, refresh=None):
curses.curs_set(0)
while True:
l = self.win.getch()
with self._lock:
if l == curses.KEY_RESIZE:
if refresh is not None:
refresh()
elif l == ord('q'):
break
elif l in (ord('k'), curses.KEY_UP):
if self._bottom_a > self._height-2:
self._bottom_a -= 1
self._redraw()
elif l in (ord('j'), curses.KEY_DOWN):
if self._bottom_a < len(self._actual):
self._bottom_a += 1
self._redraw()
elif l == curses.KEY_NPAGE:
if self._bottom_a < len(self._actual):
self._bottom_a = min(len(self._actual), self._bottom_a + self._height - 2)
self._redraw()
elif l == curses.KEY_PPAGE:
if self._bottom_a > 0:
self._bottom_a = max(self._height - 2, self._bottom_a - self._height + 2)
self._redraw()
elif l == curses.KEY_HOME:
if self._bottom_a > self._height - 2:
self._bottom_a = self._height - 2
self._redraw()
elif l == curses.KEY_END:
if self._bottom_a < len(self._actual):
self._bottom_a = len(self._actual)
self._redraw()
curses.curs_set(1)
def clear(self):
with self._lock:
self._lines = []
self._actual = []
self._bottom_a = 0
self._bottom = -1
self._redraw()
def __init__(self, y, x, height, width):
with CURSES_LOCK:
self.win = curses.newwin(height, width, y, x)
self.win.leaveok(True)
self.win.keypad(True)
self._lock = threading.RLock()
self._height = height
self._width = width
self._y = -1
self._x = -1
self._lines = []
self._actual = []
self._bottom = -1
self._bottom_a = 0
self.set_pos(y, x)
self.set_dim(height, width)
|