-
Notifications
You must be signed in to change notification settings - Fork 9
/
widgets.py
executable file
·748 lines (656 loc) · 23.9 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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import click
"""
This library is intended to generate XML suitable for being served by a wopr server.
# Error: no such widget: undefined --> forgot to specify item colSpan etc
"""
class Document:
"""
A Document is a series of pages. Documents are structured as follows:
<document>
<page>
<item col="n", row="n", colSpan="n", rowSpan="n">
<WIDGET_TYPE>
</WIDGET_TYPE>
</item>
</page>
</document>
"""
def __init__(self, pages):
self.pages = pages
@property
def content(self):
ret = []
ret.append("<document>")
for page in self.pages:
# print(self.pages[page].grid)
ret.append(self.pages[page].content)
ret.append("</document>")
return "\n".join(ret)
class Page:
"""
A page is a 12x12 grid in which you can position different widgets (within <item> tags).
"""
def __init__(self):
self.widgets = []
def add_widget(self, widget):
self.widgets.append(widget)
@property
def grid(self):
ret = []
for widget in self.widgets:
ret.append(((widget.col, widget.row), (widget.colSpan, widget.rowSpan)))
return ret
@property
def content(self):
ret = []
ret.append("<page>")
for widget in self.widgets:
ret.append(widget.content)
ret.append("</page>")
ret.append("")
return "\n".join(ret)
class Widget(object):
"""A wopr widget, the content of which is rendered in XML as follows:
<item col="5" row="9" colSpan="1" rowSpan="1">
WIDGET_CONTENT
</item>
In theory, the available widgets are the ones that exist in the blessed and
blessed-contrib projects; not all have been implemented here:
[x] Line Chart
[x] Markdown
[x] Bar Chart
[ ] Table
[x] Donut
[?] Stacked Bar Chart
[ ] Map
[ ] Gauge
[ ] Stacked Gauge
[ ] LCD Display
[ ] Picture
[ ] Sparkline
[ ] Tree
Not applicable because animated:
[ ] Rolling Log
"""
def __init__(self, col=0, row=0, colSpan=0, rowSpan=0, data=None, label=None):
self.data = data or []
self.col = col
self.row = row
self.colSpan = colSpan
self.rowSpan = rowSpan
self.label = label
def make_markdown(self):
return self.markdown.content()
@property
def content(self):
ret = []
ret.append(
' <item col="{col}" row="{row}" colSpan="{colSpan}" rowSpan="{rowSpan}" label="{label}" >'.format(
col=self.col,
row=self.row,
colSpan=self.colSpan,
rowSpan=self.rowSpan,
label=self.label,
)
)
if self.data:
ret.append(self.data)
# ret.append(self.make_markdown())
ret.append(" </item>\n")
return "\n".join(ret).encode()
class Line:
def __init__(self, title=None, style_line="red", x=[], y=[]):
self.title = title
self.style_line = style_line
self.x = x
self.y = y
def get_x(self):
return ",".join(self.x)
def get_y(self):
return ",".join(self.y)
@property
def content(self):
ret = '<m title="{title}" style-line="{style_line}" x="{x}" y="{y}" />'.format(
title=self.title,
style_line=self.style_line,
x=self.x,
y=self.y,
)
return ret
class DonutChart(Widget):
"""A widget consisting of a collection of donuts."""
def __init__(
self,
arcWidth=3, # higher number => thicker line
border_type="line",
border_fg="gray",
data=None,
fill="white",
label=None,
radius=20, # higher = larger circle
remainColor="cyan",
spacing=1.5, # higher number => closer to each other, for some reason
stroke="magenta",
yPadding=5, # does nothing?
col=12,
row=None,
colSpan=None, # does nothing?
rowSpan=None, # does nothing?
):
Widget.__init__(self, col, row, colSpan, rowSpan, label=label)
self.arcWidth = arcWidth
self.border_type = border_type
self.border_fg = border_fg
self.data = data
self.radius = radius
self.remainColor = remainColor
self.spacing = spacing
self.yPadding = yPadding
@property
def content(self):
ret = []
ret.append(
' <item col="{col}" row="{row}" colSpan="{colSpan}" rowSpan="{rowSpan}" label="{label}">'.format(
col=self.col,
row=self.row,
colSpan=self.colSpan,
rowSpan=self.rowSpan,
label=self.label,
)
)
# These are the attributes of the whole donut block
donut = ["<donut "]
donut.append('arcWidth="{}"'.format(self.arcWidth))
donut.append('border-type="{}"'.format(self.border_type))
donut.append('border-fg="{}"'.format(self.border_fg))
donut.append('label="{}"'.format(self.label))
donut.append('radius="{}"'.format(self.radius))
donut.append('yPadding="{}"'.format(self.yPadding))
donut.append('remainColor="{}"'.format(self.remainColor)) # doesn't work?
donut.append('spacing="{}"'.format(self.spacing))
donut.append(">")
ret.append(" ".join(donut))
# These are the attributes of the individual donuts
ret.append("<data>")
for donut in self.data:
line = ["<m"]
line.append('label="{}"'.format(donut))
line.append('percent="{}"'.format(self.data[donut][0]))
line.append('color="{}"'.format(self.data[donut][1]))
# line.append('remainColor="{}"'.format("cyan"))
# line.append('stroke="{}"'.format("cyan"))
# line.append('fill="{}"'.format("cyan"))
# line.append('radius="{}"'.format("cyan"))
# line.append('spacing="{}"'.format("cyan"))
line.append("/> ")
line = " ".join(line)
ret.append(line)
ret.append("</data>")
ret.append("</donut>")
ret.append("</item>")
return "\n".join(ret)
class BarChart(Widget):
"""A bar chart."""
def __init__(
self,
barWidth=9,
barSpacing=1,
xOffset=10,
maxHeight=9,
height="100%",
border_type="line",
border_fg="gray",
data=None,
label=None,
col=None,
row=None,
colSpan=None,
rowSpan=None,
):
Widget.__init__(self, col, row, colSpan, rowSpan, label=label)
self.barWidth = barWidth
self.barSpacing = barSpacing
self.xOffset = xOffset
self.maxHeight = maxHeight
self.height = height
self.border_type = border_type
self.border_fg = border_fg
self.data = data
@property
def content(self):
ret = []
ret.append(
'<item col="{col}" row="{row}" colSpan="{colSpan}" rowSpan="{rowSpan}">'.format(
col=self.col,
row=self.row,
colSpan=self.colSpan,
rowSpan=self.rowSpan,
)
)
ret.append(
"".join(
[
'<bar barWidth="{}" '.format(self.barWidth),
'label="{}" '.format(self.label),
'barSpacing="{}" '.format(self.barSpacing),
'xOffset="{}" '.format(self.xOffset),
'maxHeight="{}" '.format(self.maxHeight),
'height="{}" '.format(self.height),
'border-type="{}" '.format(self.border_type),
'border-fg="{}" '.format(self.border_fg),
'data-titles="{}" '.format(",".join(self.data.keys())),
'data-data="{}" />'.format(
",".join([str(x) for x in self.data.values()])
),
]
)
)
ret.append("\n\n")
ret.append(" </item>")
return "\n".join(ret)
class LineChart(Widget):
"""A line chart widget."""
def __init__(
self,
xPadding=5,
showLegend="true",
legend_width=12,
border_type="line",
border_fg="gray",
label=None,
col=None,
row=None,
colSpan=None,
rowSpan=None,
):
Widget.__init__(self, col, row, colSpan, rowSpan, label=label)
self.xPadding = xPadding
self.showLegend = showLegend
self.legend_width = legend_width
self.border_type = border_type
self.border_fg = border_fg
self.lines = []
def add_row(self, *args):
new_row = Row(*args)
self.rows.append(new_row)
self.rowSpan = max(self.rowSpan, len(self.rows))
self.colSpan = max(self.colSpan, len(self.y))
def add_line(self, *args, **kwargs):
new_line = Line(*args, **kwargs)
self.lines.append(new_line)
@property
def content(self):
ret = []
ret.append(
' <item col="{col}" row="{row}" colSpan="{colSpan}" rowSpan="{rowSpan}">'.format(
col=self.col,
row=self.row,
colSpan=self.colSpan,
rowSpan=self.rowSpan,
)
)
ret.append(
' <line xPadding="{xPadding}" showLegend="{showLegend}" legend-width="{legend_width}" border-type="{border_type}" border-fg="{border_fg}">'.format(
xPadding=self.xPadding,
showLegend=self.showLegend,
legend_width=self.legend_width,
border_type=self.border_type,
border_fg=self.border_fg,
)
)
ret.append(" <data>")
# Add lines
for line in self.lines:
ret.append(" " + line.content)
ret.append(" </data>")
ret.append(" </line>")
ret.append(" </item>")
return "\n".join(ret)
class Markdown(Widget):
"""A Markdown object."""
def __init__(
self,
data=None,
style_paragraph="chalk.white",
style_strong="chalk.cyan.underline",
style_em="chalk.green",
border_type="line",
border_fg="gray",
label=None,
col=None,
row=None,
colSpan=None,
rowSpan=None,
):
Widget.__init__(self, col, row, colSpan, rowSpan, label=label)
# markdown styles
self.style_paragraph = style_paragraph
self.style_strong = style_strong
self.style_em = style_em
self.border_type = border_type
self.border_fg = border_fg
self.data = self.escape(data)
self.label = label
def escape(self, data):
if "&" in data:
data = data.replace("&", "&")
if "<" in data:
data = data.replace("<", "&")
if ">" in data:
data = data.replace(">", "&")
return data
def stylize(self, line):
"""Apply some colors to Markdown text."""
if line.startswith("##"):
line = click.style(line, bold=True)
if line.startswith("# "):
line = click.style(line, bold=True)
if line.count("**") == 2:
line = line.split("**")
line = line[0] + click.style(line[1], bold=True) + line[2]
if line.count("_") == 2:
line = line.split("_")
line = line[0] + click.style(line[1], underline=True) + line[2]
return line
@property
def content(self):
if isinstance(self.data, list):
self.data = [self.stylize(line) for line in self.data]
self.data = "\n".join(self.data)
if self.data:
self.data = self.data.replace("\n", u" ")
ret = []
ret.append(
'<item col="{col}" row="{row}" colSpan="{colSpan}" rowSpan="{rowSpan}">'.format(
col=self.col,
row=self.row,
colSpan=self.colSpan,
rowSpan=self.rowSpan,
)
)
ret.append(
"".join(
[
' <markdown style-paragraph="{style_paragraph}" '.format(
style_paragraph=self.style_paragraph
),
'style-strong="{style_strong}" '.format(
style_strong=self.style_strong
),
'style-em="{style_em}" '.format(style_em=self.style_em),
'border-type="{border_type}" '.format(border_type=self.border_type),
'label="{label}" '.format(label=self.label),
'border-fg="{border_fg}"> '.format(border_fg=self.border_fg),
]
)
)
ret.append("\n")
ret.append("\t<markdown>\n")
ret.append("\t\t" + self.data)
ret.append("\n")
ret.append(" </markdown>\n")
ret.append("</markdown>\n")
ret.append(" </item>")
return "\n".join(ret)
class Row:
"""A row in a table object."""
def __init__(self, items):
self.items = items
@property
def content(self):
return ",".join([str(x) for x in self.items])
class Gauge(Widget):
"""A progress gauge."""
# var gauge = contrib.gauge({label: 'Progress', stroke: 'green', fill: 'white'})
# gauge.setPercent(25)
def __init__(
self,
label=None,
stroke="green",
fill="white",
percent=50,
border={"type": "line", "fg": "cyan"},
border_type="line",
border_fg="gray",
col=None,
row=None,
colSpan=None,
rowSpan=None,
):
Widget.__init__(self, col, row, colSpan, rowSpan, label=label)
self.stroke = stroke
self.fill = fill
self.percent = percent
self.border = border
self.border_type = border_type
self.border_fg = border_fg
@property
def content(self):
ret = []
ret.append(
'<item col="{col}" row="{row}" colSpan="{colSpan}" rowSpan="{rowSpan}" >'.format(
col=self.col,
row=self.row,
colSpan=self.colSpan,
rowSpan=self.rowSpan,
)
)
ret.append(
"".join(
[
'<gauge percent="{}" ',
'label="{}" ',
'stroke="{}" ',
#'border="{}" ',
'border-type="{}" ',
'border-fg="{}" ',
">",
]
).format(
self.percent,
self.label,
self.stroke,
# self.border,
self.border_type,
self.border_fg,
)
)
ret.append("foo")
ret.append("</gauge>")
ret.append("</item>")
return "\n".join(ret)
class Table(Widget):
"""A table object."""
def __init__(
self,
fg="white",
width="30%",
height="30%",
border={"type": "line", "fg": "cyan"},
border_type="line",
border_fg="gray",
columnSpacing=8,
columnWidth=None,
interactive="false",
label=None,
selectedFg=None,
selectedBg=None,
keys=False,
data={},
col=None,
row=None,
colSpan=None,
rowSpan=None,
):
Widget.__init__(self, col, row, colSpan, rowSpan, label=label)
self.fg = fg
self.keys = keys
self.width = width
self.height = height
self.border = border
self.border_type = border_type
self.border_fg = border_fg
self.columnSpacing = columnSpacing
self.interactive = interactive
self.selectedFg = selectedFg
self.selectedBg = selectedBg
self.data = data
self.rows = data["rows"]
self.headers = data["headers"]
@property
def data_headers(self):
"""Return the table headers."""
headers = self.data["headers"]
return ",".join(headers)
def fill_rows(self):
"""Fill rows with empty strings to avoid annoying "RangeError: Invalid array length" error."""
# FIXME: this doesn't work...
maxlen = max([len(row) for row in self.rows])
maxlen = max(maxlen, len(self.headers))
for row in self.rows:
while len(row) < maxlen:
row.append(" ")
# print(row, file=sys.stderr)
while len(self.headers) < maxlen:
self.headers.append(" ")
# print(self.data_headers, file=sys.stderr)
# print(self.headers, file=sys.stderr)
# print(maxlen, file=sys.stderr)
assert len(self.headers) == maxlen
@property
def columnWidth(self):
"""Return the width of each column."""
widths = [len(header) for header in self.headers]
for row in self.rows:
row_widths = [len(str(x)) for x in row]
widths = [max(x, y) for (x, y) in zip(widths, row_widths)]
return ",".join([str(x) for x in widths])
@property
def content(self):
ret = []
ret.append(
'<item col="{col}" row="{row}" colSpan="{colSpan}" rowSpan="{rowSpan}" >'.format(
col=self.col,
row=self.row,
colSpan=self.colSpan,
rowSpan=self.rowSpan,
)
)
ret.append(
(
" <table "
'border-type="{border_type}" border-fg="{border_fg}" '
'columnSpacing="{columnSpacing}" columnWidth="{columnWidth}" '
'data-headers="{data_headers}" '
'fg="{fg}" '
'height="{height}" '
'interactive="{interactive}" '
'keys="{keys}" '
'label="{label}" '
'width="{width}" '
'selectedBg="{selectedBg}" '
'selectedFg="{selectedFg}" '
"> ".format(
border_type=self.border_type,
border_fg=self.border_fg,
columnSpacing=self.columnSpacing,
columnWidth=self.columnWidth,
data_headers=self.data_headers,
fg=self.fg,
height=self.height,
interactive=self.interactive,
keys=self.keys,
label=self.label,
selectedBg=self.selectedBg,
selectedFg=self.selectedFg,
width=self.width,
)
)
)
ret.append(" <data-data>")
self.fill_rows()
assert len(self.headers) == len(self.rows[0])
for row in self.rows:
ret.append(" " + ",".join([str(x) for x in row]))
ret.append(" </data-data>")
ret.append(" </table>")
ret.append(" </item>")
return "\n".join(ret)
class Spedometer(Widget):
"""
This is a made-up class but I like it.
"""
fancy_grid = [
"╒",
"═",
"╤",
"═",
"╕",
"│",
" ",
"╪",
" ",
"│",
"╞",
" ",
"╪",
" ",
"╡",
"╘",
"═",
"╧",
"═",
"╛",
]
toc = [
"╒══════════════ Welcome to the professional resume of A.J. Bowen ══════════════════════╕",
"│ │",
"│ 1. This screen │",
"│ 2. Skills │",
"│ 3. Experience │",
"│ 4. Languages │",
"│ │",
"│ To view all slides (press enter to move to next slide; press Ctrl+C to exit) │",
"│ │",
"│ $ p=0; while true; do curl localhost/$((p++))\?cols=$((COLUMNS)); read; done │",
"│ │",
"│ To view a specific slide: │",
"│ │",
"│ $ curl -N cv.soulshake.net/3 │",
"│ │",
"│ Options (URL paramters): │",
"│ │",
"│ \?auto Advance through slides automatically (5 seconds each) │",
"│ \&cols=$((COLUMNS)) Specify number of rows │",
"│ \&rows=$((LINES)) Specify number of columns │",
"│ \&terminal=xterm Specify your terminal │",
"│ │",
"│ You can infer them automatically from your environment: │",
"│ │",
"│ $ curl -N cv.soulshake.net\?\&cols=$((COLUMNS))\&rows=$((LINES-5))\&terminal=${TERM} │",
"│ │",
"│ │",
"╘══════════════════════════════════════════════════════════════════════════════════════════╛",
]
@property
def content(self):
ret = [
" .",
" 90 100 90 100 ",
" 80 \ 110 80 110",
" \ ",
" 70 \ 120 70 120",
" \ ",
" 60 \ 130 60 130",
" O O",
" 50 140 50 \ 140",
" \ ",
" 40 150 40 \ 150",
" mph mph \ ",
" 30 160 30 \ 160",
" This is your CV on LinkedIn This is your CV on the command line",
]
return "\n".join(ret)