-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdashing.py
More file actions
544 lines (452 loc) · 17.1 KB
/
dashing.py
File metadata and controls
544 lines (452 loc) · 17.1 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# -*- coding: utf-8 -*-
# Dashing - Released under LGPLv3, see LICENSE
"""
Dashing allows to quickly create terminal-based dashboards in Python.
It focuses on practicality over completeness. If you want to have complete control
over every character on the screen, use ncurses or similar.
Dashing automatically fills the screen with "tiles".
There are 2 type of "container" tiles that allow vertical and horizontal splitting
called VSplit and HSplit. Dashing scales them based on the screen size.
Any tile passed as argument at init time will be nested using the .items attribute
.items can be used to access, add or remove nested tiles.
You can easily extend Dashing with new tile types. Subclass :class:`Tile`, implement
__init__ and _display. See dashing.py for examples.
The other types of tiles are:
- :class:`Text` - simple text
- :class:`Log` - a log pane that scrolls automatically
- :class:`HGauge` - horizontal gauge
- :class:`VGauge` - vertical gauge
- :class:`ColorRangeVGauge` - vertical gauge with coloring
- :class:`VChart` - vertical chart
- :class:`HChart` - horizontal chart
- :class:`HBrailleChart`
- :class:`HBrailleFilledChart`
All tiles accept title, color, border_color keywords arguments at init time.
Gauges represent an instant value between 0 and 100.
You can set a value at init time using the val keyword argument or access the
.value attribute at any time.
Charts represent a sequence of values between 0 and 100 and scroll automatically.
Call :meth:`display` on the root element to display or update the ui.
You can easily nest splits and tiles as in::
ui = HSplit(
VSplit(
HGauge(val=50, title="foo", border_color=5),
)
)
# access a tile by index
gauge = ui.items[0].items[0]
gauge.value = 3.0
# display/refresh the ui
ui.display()
"""
from collections import deque, namedtuple
from blessed import Terminal
try:
unichr
except NameError:
unichr = chr
# "graphic" elements
border_bl = u"└"
border_br = u"┘"
border_tl = u"┌"
border_tr = u"┐"
border_h = u"─"
border_v = u"│"
hbar_elements = (u"▏", u"▎", u"▍", u"▌", u"▋", u"▊", u"▉")
vbar_elements = (u"▁", u"▂", u"▃", u"▄", u"▅", u"▆", u"▇", u"█")
braille_left = (0x01, 0x02, 0x04, 0x40, 0)
braille_right = (0x08, 0x10, 0x20, 0x80, 0)
braille_r_left = (0x04, 0x02, 0x01)
braille_r_right = (0x20, 0x10, 0x08)
TBox = namedtuple("TBox", "t x y w h")
class Tile(object):
def __init__(self, title=None, border_color=None, color=0):
self.title = title
self.color = color
self.border_color = border_color
def _display(self, tbox, parent):
"""Render current tile
"""
raise NotImplementedError
def _draw_borders(self, tbox):
# top border
print(
tbox.t.color(self.border_color)
+ tbox.t.move(tbox.x, tbox.y)
+ border_tl
+ border_h * (tbox.w - 2)
+ border_tr
)
# left and right
for dx in range(1, tbox.h - 1):
print(tbox.t.move(tbox.x + dx, tbox.y) + border_v)
print(tbox.t.move(tbox.x + dx, tbox.y + tbox.w - 1) + border_v)
# bottom
print(
tbox.t.move(tbox.x + tbox.h - 1, tbox.y)
+ border_bl
+ border_h * (tbox.w - 2)
+ border_br
)
def _draw_borders_and_title(self, tbox):
"""Draw borders and title as needed and returns
inset (x, y, width, height)
"""
if self.border_color is not None:
self._draw_borders(tbox)
if self.title:
fill_all_width = self.border_color is None
self._draw_title(tbox, fill_all_width)
if self.border_color is not None:
return TBox(tbox.t, tbox.x + 1, tbox.y + 1, tbox.w - 2, tbox.h - 2)
elif self.title is not None:
return TBox(tbox.t, tbox.x + 1, tbox.y, tbox.w - 1, tbox.h - 1)
return TBox(tbox.t, tbox.x, tbox.y, tbox.w, tbox.h)
def _fill_area(self, tbox, char, *a, **kw): # FIXME
"""Fill area with a character
"""
# for dx in range(0, height):
# print(tbox.t.move(x + dx, tbox.y) + char * width)
pass
def display(self):
"""Render current tile and its items. Recurse into nested splits
if any.
"""
try:
t = self._terminal
except AttributeError:
t = self._terminal = Terminal()
tbox = TBox(t, 0, 0, t.width, t.height - 1)
self._fill_area(tbox.t, 0, 0, t.width, t.height - 1, "f") # FIXME
tbox = TBox(t, 0, 0, t.width, t.height - 1)
self._display(tbox, None)
# park cursor in a safe place and reset color
print(t.move(t.height - 3, 0) + t.color(0))
def _draw_title(self, tbox, fill_all_width):
if not self.title:
return
margin = int((tbox.w - len(self.title)) / 20)
col = "" if self.border_color is None else tbox.t.color(self.border_color)
if fill_all_width:
title = (
" " * margin + self.title + " " * (tbox.w - margin - len(self.title))
)
print(tbox.t.move(tbox.x, tbox.y) + col + title)
else:
title = " " * margin + self.title + " " * margin
print(tbox.t.move(tbox.x, tbox.y + margin) + col + title)
class Split(Tile):
"""Split a box vertically (VSplit) or horizontally (HSplit)
"""
def __init__(self, *items, **kw):
super(Split, self).__init__(**kw)
self.items = items
def _display(self, tbox, parent):
"""Render current tile and its items. Recurse into nested splits
"""
tbox = self._draw_borders_and_title(tbox)
if not self.items:
# empty split
self._fill_area(tbox, " ")
return
if isinstance(self, VSplit):
item_height = tbox.h // len(self.items)
item_width = tbox.w
else:
item_height = tbox.h
item_width = tbox.w // len(self.items)
x = tbox.x
y = tbox.y
for i in self.items:
i._display(TBox(tbox.t, x, y, item_width, item_height), self)
if isinstance(self, VSplit):
x += item_height
else:
y += item_width
# Fill leftover area
if isinstance(self, VSplit):
leftover_x = tbox.h - x + 1
if leftover_x > 0:
self._fill_area(TBox(tbox.t, x, y, tbox.w, leftover_x), " ")
else:
leftover_y = tbox.w - y + 1
if leftover_y > 0:
self._fill_area(TBox(tbox.t, x, y, leftover_y, tbox.h), " ")
class VSplit(Split):
pass
class HSplit(Split):
pass
class Text(Tile):
"""A multi-line text box. Example::
Text('Hello World, this is dashing.', border_color=2),
"""
def __init__(self, text, color=0, *args, **kw):
super(Text, self).__init__(**kw)
self.text = text
self.color = color
def _display(self, tbox, parent):
tbox = self._draw_borders_and_title(tbox)
for dx, line in enumerate(self.text.splitlines()):
print(
tbox.t.color(self.color)
+ tbox.t.move(tbox.x + dx, tbox.y)
+ line
+ " " * (tbox.w - len(line))
)
dx += 1
while dx < tbox.h:
print(tbox.t.move(tbox.x + dx, tbox.y) + " " * tbox.w)
dx += 1
class Log(Tile):
"""A log pane that scrolls automatically.
Add new lines with :meth:`append`
"""
def __init__(self, *args, **kw):
self.logs = deque(maxlen=50)
super(Log, self).__init__(**kw)
def _display(self, tbox, parent):
tbox = self._draw_borders_and_title(tbox)
n_logs = len(self.logs)
log_range = min(n_logs, tbox.h)
start = n_logs - log_range
print(tbox.t.color(self.color))
for i in range(0, log_range):
line = self.logs[start + i]
print(tbox.t.move(tbox.x + i, tbox.y) + line + " " * (tbox.w - len(line)))
if i < tbox.h:
for i2 in range(i + 1, tbox.h):
print(tbox.t.move(tbox.x + i2, tbox.y) + " " * tbox.w)
def append(self, msg):
"""Append a new log message at the bottom"""
self.logs.append(msg)
class HGauge(Tile):
"""Horizontal gauge
"""
def __init__(self, label=None, val=100, color=2, **kw):
kw["color"] = color
super(HGauge, self).__init__(**kw)
self.value = val
self.label = label
def _display(self, tbox, parent):
tbox = self._draw_borders_and_title(tbox)
if self.label:
wi = (tbox.w - len(self.label) - 3) * self.value / 100
v_center = int((tbox.h) * 0.5)
else:
wi = tbox.w * self.value / 100.0
index = int((wi - int(wi)) * 7)
bar = hbar_elements[-1] * int(wi) + hbar_elements[index]
print(tbox.t.color(self.color) + tbox.t.move(tbox.x, tbox.y + 1))
if self.label:
pad = tbox.w - 1 - len(self.label) - len(bar)
else:
pad = tbox.w - len(bar)
bar += hbar_elements[0] * pad
# draw bar
for dx in range(0, tbox.h):
m = tbox.t.move(tbox.x + dx, tbox.y)
if self.label:
if dx == v_center:
# draw label
print(m + self.label + " " + bar)
else:
print(m + " " * len(self.label) + " " + bar)
else:
print(m + bar)
class VGauge(Tile):
"""Vertical gauge
"""
def __init__(self, val=100, color=2, **kw):
kw["color"] = color
super(VGauge, self).__init__(**kw)
self.value = val
def _display(self, tbox, parent):
"""Render current tile
"""
tbox = self._draw_borders_and_title(tbox)
nh = tbox.h * (self.value / 100.5)
print(tbox.t.move(tbox.x, tbox.y) + tbox.t.color(self.color))
for dx in range(tbox.h):
m = tbox.t.move(tbox.x + tbox.h - dx - 1, tbox.y)
if dx < int(nh):
bar = vbar_elements[-1] * tbox.w
elif dx == int(nh):
index = int((nh - int(nh)) * 8)
bar = vbar_elements[index] * tbox.w
else:
bar = " " * tbox.w
print(m + bar)
class ColorRangeVGauge(Tile):
"""Vertical gauge with color map.
E.g.: green gauge for values below 50, red otherwise:
colormap=((50, 2), (100, 1))
"""
def __init__(self, val=100, colormap=(), **kw):
self.colormap = colormap
super(ColorRangeVGauge, self).__init__(**kw)
self.value = val
def _display(self, tbox, parent):
tbox = self._draw_borders_and_title(tbox)
nh = tbox.h * (self.value / 100.5)
filled_element = vbar_elements[-1]
for thresh, col in self.colormap:
if thresh > self.value:
break
print(tbox.t.move(tbox.x, tbox.y) + tbox.t.color(col))
for dx in range(tbox.h):
m = tbox.t.move(tbox.x + tbox.h - dx - 1, tbox.y)
if dx < int(nh):
bar = filled_element * tbox.w
elif dx == int(nh):
index = int((nh - int(nh)) * 8)
bar = vbar_elements[index] * tbox.w
else:
bar = " " * tbox.w
print(m + bar)
class VChart(Tile):
"""Vertical chart. Values must be between 0 and 100 and can be float.
"""
def __init__(self, val=100, *args, **kw):
super(VChart, self).__init__(**kw)
self.value = val
self.datapoints = deque(maxlen=50)
def append(self, dp):
"""Append a new value: int or float between 1 and 100"""
self.datapoints.append(dp)
def _display(self, tbox, parent):
tbox = self._draw_borders_and_title(tbox)
filled_element = hbar_elements[-1]
scale = tbox.w / 100.0
print(tbox.t.color(self.color))
for dx in range(tbox.h):
index = 50 - (tbox.h) + dx
try:
dp = self.datapoints[index] * scale
index = int((dp - int(dp)) * 8)
bar = filled_element * int(dp) + hbar_elements[index]
assert len(bar) <= tbox.w, dp
bar += " " * (tbox.w - len(bar))
except IndexError:
bar = " " * tbox.w
print(tbox.t.move(tbox.x + dx, tbox.y) + bar)
class HChart(Tile):
"""Horizontal chart, filled
"""
def __init__(self, val=100, *args, **kw):
super(HChart, self).__init__(**kw)
self.value = val
self.datapoints = deque(maxlen=500)
def append(self, dp):
"""Append a new value: int or float between 1 and 100"""
self.datapoints.append(dp)
def _display(self, tbox, parent):
tbox = self._draw_borders_and_title(tbox)
print(tbox.t.color(self.color))
for dx in range(tbox.h):
bar = ""
for dy in range(tbox.w):
dp_index = -tbox.w + dy
try:
dp = self.datapoints[dp_index]
q = (1 - dp / 100) * tbox.h
if dx == int(q):
index = int((int(q) - q) * 8 - 1)
bar += vbar_elements[index]
elif dx < int(q):
bar += " "
else:
bar += vbar_elements[-1]
except IndexError:
bar += " "
# assert len(bar) == tbox.w
print(tbox.t.move(tbox.x + dx, tbox.y) + bar)
class HBrailleChart(Tile):
"""Horizontal chart made with dots
"""
def __init__(self, val=100, *args, **kw):
super(HBrailleChart, self).__init__(**kw)
self.value = val
self.datapoints = deque(maxlen=500)
def append(self, dp):
"""Append a new value: int or float between 1 and 100"""
self.datapoints.append(dp)
def _generate_braille(self, l, r):
v = 0x28 * 256 + (braille_left[l] + braille_right[r])
return unichr(v)
def _display(self, tbox, parent):
tbox = self._draw_borders_and_title(tbox)
print(tbox.t.color(self.color))
for dx in range(tbox.h):
bar = ""
for dy in range(tbox.w):
dp_index = (dy - tbox.w) * 2
try:
dp1 = self.datapoints[dp_index]
dp2 = self.datapoints[dp_index + 1]
except IndexError:
# no data (yet)
bar += " "
continue
q1 = (1 - dp1 / 100) * tbox.h
q2 = (1 - dp2 / 100) * tbox.h
if dx == int(q1):
index1 = int((q1 - int(q1)) * 4)
if dx == int(q2): # both datapoints in the same rune
index2 = int((q2 - int(q2)) * 4)
else:
index2 = -1 # no dot
bar += self._generate_braille(index1, index2)
elif dx == int(q2):
# the right dot only is in the current rune
index2 = int((q2 - int(q2)) * 4)
bar += self._generate_braille(-1, index2)
else:
bar += " "
print(tbox.t.move(tbox.x + dx, tbox.y) + bar)
class HBrailleFilledChart(Tile):
"""Horizontal chart, filled with dots
"""
def __init__(self, val=100, *args, **kw):
super(HBrailleFilledChart, self).__init__(**kw)
self.value = val
self.datapoints = deque(maxlen=500)
def append(self, dp):
"""Append a new value: int or float between 1 and 100"""
self.datapoints.append(dp)
def _generate_braille(self, lmax, rmax):
v = 0x28 * 256
for l in range(lmax):
v += braille_r_left[l]
for r in range(rmax):
v += braille_r_right[r]
return unichr(v)
def _display(self, tbox, parent):
tbox = self._draw_borders_and_title(tbox)
print(tbox.t.color(self.color))
for dx in range(tbox.h):
bar = ""
for dy in range(tbox.w):
dp_index = (dy - tbox.w) * 2
try:
dp1 = self.datapoints[dp_index]
dp2 = self.datapoints[dp_index + 1]
except IndexError:
# no data (yet)
bar += " "
continue
q1 = (1 - dp1 / 100.0) * tbox.h
q2 = (1 - dp2 / 100.0) * tbox.h
if dx == int(q1):
index1 = 3 - int((q1 - int(q1)) * 4)
elif dx > q1:
index1 = 3
else:
index1 = 0
if dx == int(q2):
index2 = 3 - int((q2 - int(q2)) * 4)
elif dx > q2:
index2 = 3
else:
index2 = 0
bar += self._generate_braille(index1, index2)
print(tbox.t.move(tbox.x + dx, tbox.y) + bar)