-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal_bars.py
71 lines (57 loc) · 1.73 KB
/
terminal_bars.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
node = "█"
remainder = " ▏▎▍▌▋▊▉"
overflowstr = "▓▓▒▒░░"
background = " "
barparts = {
"topleft": "┏",
"top": "━",
"topright": "┓",
"left": "┃",
"right": "┃",
"bottomleft": "┗",
"bottom": "━",
"bottomright": "┛",
}
def plot(names, data, width, *, formatter="{}".format, maximum=None):
namespace = max(map(len, names)) + 3
formatted_data = [formatter(datum) for datum in data]
dataspace = max(map(len, formatted_data))
barspace = width-4 - namespace - dataspace
overflow = overflowstr.rjust(barspace, node)
bartop = "{topleft}{top}{topright}{spacepadding}".format(
topleft = barparts["topleft"],
top = barparts["top"] * barspace,
topright = barparts["topright"],
spacepadding = " "*dataspace
)
barbottom = "{bottomleft}{bottom}{bottomright}{spacepadding}".format(
bottomleft = barparts["bottomleft"],
bottom = barparts["bottom"] * barspace,
bottomright = barparts["bottomright"],
spacepadding = " "*dataspace
)
print(" "*namespace, bartop)
for name, datum, datumstr in zip(names, data, formatted_data):
if maximum is not None:
datum = datum * barspace / maximum
else:
datum /= len(remainder)
# Odd order to catch NaN
if not datum <= barspace:
bar = overflow
else:
notches = round(datum * len(remainder))
full, partial = divmod(notches, 8)
bar = node * full + remainder[partial].rstrip()
print("{name:>{namespace}} {left}{bar:{background}<{barspace}}{right} {datum:{dataspace}}".format(
name=name,
namespace=namespace,
left=barparts["left"],
bar=bar,
background=background,
barspace=barspace,
right=barparts["right"],
datum=datumstr,
dataspace=dataspace
))
print(" "*namespace, barbottom)