more braille styles (ideas from drawille)

added 'braille-peak' and 'braille-snake'.  'braille-peak'
renders only the top point, and 'braille-snake' fills some in.

I should have stressed earlier that I only got the braille drawing ideas
after coming across drawille ( https://github.com/asciimoo/drawille ).
This commit is contained in:
Matus Telgarsky 2015-01-28 16:54:42 -05:00
parent 117a2acfff
commit 82db7a87a2
3 changed files with 34 additions and 13 deletions

View File

@ -371,7 +371,7 @@ def make_graph(values, upper_limit=100.0, style="blocks"):
:param values: An array of values to graph. :param values: An array of values to graph.
:param upper_limit: Maximum value for the y axis. :param upper_limit: Maximum value for the y axis.
:param style: Drawing style (currently 'blocks' or 'braille'). :param style: Drawing style ('blocks', 'braille-fill', 'braille-peak', or 'braille-snake').
:returns: Bar as a string :returns: Bar as a string
""" """
values = [float(n) for n in values] values = [float(n) for n in values]
@ -384,18 +384,39 @@ def make_graph(values, upper_limit=100.0, style="blocks"):
graph = '_' * len(values) graph = '_' * len(values)
else: else:
graph = ''.join(bar[int((n - mn) / extent * bar_count)] for n in values) graph = ''.join(bar[int((n - mn) / extent * bar_count)] for n in values)
elif style == 'braille': elif style in ['braille-fill', 'braille-peak', 'braille-snake']:
# idea from https://github.com/asciimoo/drawille # idea from https://github.com/asciimoo/drawille
# unicode values from http://en.wikipedia.org/wiki/Braille # unicode values from http://en.wikipedia.org/wiki/Braille
v2 = values if len(values) % 2 == 0 else values + [mn]
l = len(v2) // 2 vpad = values if len(values) % 2 == 0 else values + [mn]
if extent == 0: vscale = [round(4 * (vp - mn) / extent) for vp in vpad]
graph = chr(0x2800) * l # should be visually equiv to ' ' l = len(vscale) // 2
# do the 2-character collapse separately for clarity
if 'fill' in style:
vbits = [[0, 0x40, 0x44, 0x46, 0x47][vs] for vs in vscale]
elif 'peak' in style:
vbits = [[0, 0x40, 0x04, 0x02, 0x01][vs] for vs in vscale]
else: else:
assert('snake' in style)
# there are a few choices for what to put last in vb2.
# arguable vscale[-1] from the _previous_ call is best.
vb2 = [vscale[0]] + vscale + [0]
vbits = []
for i in range(1, l + 1):
c = 0
for j in range(min(vb2[i - 1], vb2[i], vb2[i + 1]), vb2[i] + 1):
c |= [0, 0x40, 0x04, 0x02, 0x01][j]
vbits.append(c)
# 2-character collapse
graph = '' graph = ''
for i in range(0, l, 2): for i in range(0, l, 2):
b1 = [0, 0x40, 0x44, 0x46, 0x47][round(4 * (v2[i] - mn) / extent)] b1 = vbits[i]
b2 = [0, 0x80, 0xa0, 0xb0, 0xb8][round(4 * (v2[i + 1] - mn) / extent)] b2 = vbits[i + 1]
if b2 & 0x40:
b2 = b2 - 0x30
b2 = b2 << 3
graph += chr(0x2800 + b1 + b2) graph += chr(0x2800 + b1 + b2)
else: else:
raise NotImplementedError("Graph drawing style '%s' unimplemented." % style) raise NotImplementedError("Graph drawing style '%s' unimplemented." % style)

View File

@ -26,7 +26,7 @@ class CpuUsageGraph(CpuUsage, ColorRangeModule):
("start_color", "Hex or English name for start of color range, eg '#00FF00' or 'green'"), ("start_color", "Hex or English name for start of color range, eg '#00FF00' or 'green'"),
("end_color", "Hex or English name for end of color range, eg '#FF0000' or 'red'"), ("end_color", "Hex or English name for end of color range, eg '#FF0000' or 'red'"),
("graph_width", "Width of the cpu usage graph"), ("graph_width", "Width of the cpu usage graph"),
("graph_style", "Graph style, currently 'blocks' or 'braille'"), ("graph_style", "Graph style ('blocks', 'braille-fill', 'braille-peak', or 'braille-snake')"),
) )
graph_width = 15 graph_width = 15

View File

@ -254,7 +254,7 @@ class Network(IntervalModule, ColorRangeModule):
("start_color", "Hex or English name for start of color range, eg '#00FF00' or 'green'"), ("start_color", "Hex or English name for start of color range, eg '#00FF00' or 'green'"),
("end_color", "Hex or English name for end of color range, eg '#FF0000' or 'red'"), ("end_color", "Hex or English name for end of color range, eg '#FF0000' or 'red'"),
("graph_width", "Width of the network traffic graph"), ("graph_width", "Width of the network traffic graph"),
("graph_style", "Graph style, currently 'blocks' or 'braille'"), ("graph_style", "Graph style ('blocks', 'braille-fill', 'braille-peak', or 'braille-snake')"),
("upper_limit", ("upper_limit",
"Expected max kb/s. This value controls how the network traffic graph is drawn and in what color"), "Expected max kb/s. This value controls how the network traffic graph is drawn and in what color"),
("graph_type", "Whether to draw the network traffic graph for input or output. " ("graph_type", "Whether to draw the network traffic graph for input or output. "