-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidgets.py
273 lines (226 loc) · 8.54 KB
/
widgets.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from collections.abc import Sequence
from time import sleep
from math import floor, ceil
import curses
class Table:
def __init__(self, win, x, y, w, h, col_defs, properties={}):
self.window = win
self.column_definitions = col_defs
self.x = x
self.y = y
self.width = w
self.height = h
self.properties = properties
def calculateColDefs(self):
ret = []
col_offset = 0
index = 0
for col_def in self.column_definitions:
calculated = dict()
if "offset" in col_def:
offset_def = col_def["offset"]
col_offset = (self.width + offset_def) if offset_def < 0 else offset_def
calculated["offset"] = col_offset
if "width" in col_def:
calculated["width"] = col_def["width"]
elif "default_width" in self.properties:
calculated["width"] = self.properties["default_width"]
else:
calculated["width"] = self.width - col_offset
if "force_callback" in col_def:
calculated["force_callback"] = col_def["force_callback"]
elif "force_callback" in self.properties:
calculated["force_callback"] = self.properties["default_force_callback"]
else:
calculated["force_callback"] = False
if "padding_left" in col_def:
calculated["padding_left"] = col_def["padding_left"]
elif "padding_left" in self.properties:
calculated["padding_left"] = self.properties["default_padding_left"]
else:
calculated["padding_left"] = False
if "padding_right" in col_def:
calculated["padding_right"] = col_def["padding_right"]
elif "padding_right" in self.properties:
calculated["padding_left"] = self.properties["default_padding_right"]
else:
calculated["padding_right"] = False
if "text" in col_def:
calculated["text"] = col_def["text"]
if "alignment" in col_def:
calculated["alignment"] = col_def["alignment"]
if "attributes" in col_def:
calculated["attributes"] = col_def["attributes"]
if "force_callback" in col_def:
calculated["force_callback"] = col_def["force_callback"]
ret.append(calculated)
col_offset += (
calculated["padding_left"]
+ calculated["width"]
+ calculated["padding_right"]
)
if "spacing" in self.properties:
col_offset += self.properties["spacing"]
return ret
def apply_data(self, data):
calculatedColDefs = self.calculateColDefs()
force_callback = (
True
if "force_callback" in self.properties and self.properties["force_callback"]
else False
)
for row in range(0, self.height):
record = data[row] if row < len(data) else None
for col in range(0, len(calculatedColDefs)):
text = (
self.properties["default_text"]
if "default_text" in self.properties
else ""
)
color = (
self.properties["default_color"]
if "default_color" in self.properties
else None
)
alignment = (
self.properties["default_alignment"]
if "default_alignment" in self.properties
else 0
)
attributes = (
self.properties["default_attributes"]
if "default_attributes" in self.properties
else 0
)
col_def = calculatedColDefs[col]
if "text" in col_def:
text_def = col_def["text"]
if callable(text_def):
if col_def["force_callback"] or record is not None:
text = text_def(col, row, record, data)
elif isinstance(text_def, Sequence):
text = text_def[row % len(text_def)]
else:
text = text_def
elif record is not None:
text = record[col]
if "alignment" in col_def:
alignment_def = col_def["alignment"]
if callable(alignment_def):
if col_def["force_callback"] or record is not None:
alignment = alignment_def(col, row, record, data)
elif isinstance(alignment_def, Sequence):
alignment = alignment_def[row % len(alignment_def)]
else:
alignment = alignment_def
if alignment is 0:
text = text.ljust(col_def["width"])
elif alignment is 1:
text = text.center(col_def["width"])
elif alignment is 2:
text = text.rjust(col_def["width"])
text = formatText(
text,
col_def["width"],
alignment,
col_def["padding_left"],
col_def["padding_right"],
)
if "attributes" in col_def:
attributes_def = col_def["attributes"]
if callable(attributes_def):
if col_def["force_callback"] or record is not None:
attributes = attributes_def(col, row, record, data)
elif isinstance(attributes_def, Sequence):
attributes = attributes_def[row % len(attributes_def)]
else:
attributes = attributes_def
self.window.addstr(
self.y + row, self.x + col_def["offset"], text, attributes
)
self.window.refresh()
if record is not None and "line_delay" in self.properties:
sleep(self.properties["line_delay"])
class Label:
def __init__(
self,
window,
x,
y,
w,
text="",
attributes=0,
alignment=0,
padding_left=0,
padding_right=0,
padding_is_width=True,
):
self.window = window
self.x = x
self.y = y
self.width = (w - padding_left - padding_right) if padding_is_width else w
self.text = text
self.attributes = attributes
self.alignment = alignment
self.padding_left = padding_left
self.padding_right = padding_right
def update_text(self, text):
self.text = text
return self
def clear_text(self):
self.text = ""
return self
def update_attributes(self, attributes):
self.attributes = attributes
return self
def draw(self, refresh=True):
text = formatText(
self.text, self.width, self.alignment, self.padding_left, self.padding_right
)
self.window.addstr(self.y, self.x, text, self.attributes)
if refresh:
self.window.refresh()
# print(text)
return self
def formatText(text, width, alignment=1, padding_left=0, padding_right=0):
# alignment
if alignment == 0:
text = text.ljust(width)
elif alignment == 1:
text = text.center(width)
elif alignment == 2:
text = text.rjust(width)
# crop width
text = text[:width]
# add padding
text = " " * padding_left + text
text += " " * padding_right
return text
def rectangle(window, x, y, w, h):
t = "\u2500"
b = "\u2500"
l = "\u2502"
r = "\u2502"
ul = "\u250c"
ur = "\u2510"
ll = "\u2514"
lr = "\u2518"
# Borders
hline(window, x + 1, y, t, w - 2) # top
vline(window, x + w - 1, y + 1, l, h - 2) # right
hline(window, x + 1, y + h - 1, b, w - 2) # bottom
vline(window, x, y + 1, r, h - 2) # left
# Corners
window.addch(y, x, ul)
window.addch(y, x + w - 1, ur)
window.addch(y + h - 1, x, ll)
try:
window.addch(y + h - 1, x + w - 1, lr)
except Exception as e:
pass
def hline(window, x, y, ch, w):
for X in range(x, x + w):
window.addch(y, X, ch)
def vline(window, x, y, ch, h):
for Y in range(y, y + h):
window.addch(Y, x, ch)