-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
150 lines (118 loc) · 3.69 KB
/
example.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
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
from pyConsole import VerticalSplitScreen
from pyConsole import HorizontalSplitScreen
from pyConsole import VerticalScrollableScreen
from pyConsole import before_enter
from pyConsole import before_exit
from pyConsole import resize
from pyConsole import start
from pyConsole import publish
from pyConsole import subscribe
class Menu(VerticalScrollableScreen):
def onKeyDown(self, key):
if self.focus:
if key == '\n':
publish('build_screen', self.selected)
class Header(object):
def __init__(self):
self.width = 0
self.height = 0
self.title = 'static header'
def onScroll(self, direction):
pass
def onKeyDown(self, key):
pass
def onResize(self, x, y, w, h):
self.width = w
self.height = h
self.x = x
self.y = y
def render(self, canvas, x, y):
filler = ' ' * self.width
title = ' ' + self.title
title = '\033[5;30;47m' + (title + filler[:-len(title)]) + '\033[0m' # noqa
canvas.append('\033[{1};{0}H{2}'.format(x, y, title))
class Footer(object):
def __init__(self):
self.width = 0
self.height = 0
self.title = 'debug '
self.events = []
subscribe('event', self.onMessage)
def onMessage(self, event):
self.events.append(event)
if len(self.events) > 10:
self.events.pop(0)
publish('repaint')
def onScroll(self, direction):
pass
def onKeyDown(self, key):
pass
def onResize(self, x, y, w, h):
self.width = w
self.height = h
self.x = x
self.y = y
def render(self, canvas, x, y):
filler = ' ' * self.width
title = '\033[7;32m ' + self.title + '\033[0;31m ' + ', '.join(reversed(self.events)) # noqa
title = title + filler[:-len(title)]
title = title[:min(self.width, len(title))] + '\033[0m'
canvas.append('\033[{1};{0}H{2}'.format(x, self.height - 1 + self.y, title)) # noqa
if __name__ == '__main__':
# screen bleeding and multiline strings test
# big_data = [' B-{0}'.format('{0}'.format(i % 10) * ((i + 1) % 200)) for i in range(0, 1000, 1)] # noqa
# screen buffer seek test
big_data = [
{
'id': 'line_{0}'.format(i + 1),
'label': 'Line number {0}'.format(i + 1)
} for i in range(0, 2000, 1)
]
menu_items = [
{
'id': 'item_1',
'label': 'Item 1'
},
{
'id': 'item_2',
'label': 'Item 2'
},
{
'id': 'item_3',
'label': 'Item 3'
},
{
'id': 'item_4',
'label': 'Item 4'
}
]
header = Header()
header.height = 2
footer = Footer()
footer.height = 2
left_list = Menu(menu_items)
left_list.width = 10
def build_screen(item):
selected_item = menu_items[next(index for (index, d) in enumerate(menu_items) if d['id'] == item)] # noqa
big_data = [
{
'id': '{0}_line_{1}'.format(selected_item['id'], i + 1),
'label': '{0} -> {1}'.format(selected_item['label'], i + 1)
} for i in range(0, 2000, 1)
]
publish('set_screen', VerticalSplitScreen(
top=header,
bottom=VerticalSplitScreen(
top=HorizontalSplitScreen(
left=left_list,
right=VerticalScrollableScreen(big_data),
),
bottom=footer
)
))
resize()
before_enter()
subscribe('build_screen', build_screen)
publish('build_screen', 'item_1')
start()
before_exit()