Merge pull request #528 from somospocos/braille-graph
[core/output] add support for drawing graphs using Braille chars
This commit is contained in:
commit
f86fc33a96
1 changed files with 101 additions and 0 deletions
|
@ -30,6 +30,33 @@ VBARS = [
|
|||
u"\u258a",
|
||||
u"\u2589",
|
||||
u"\u2588"]
|
||||
BRAILLE = {
|
||||
(0, 0): u" ",
|
||||
(1, 0): u"\u2840",
|
||||
(2, 0): u"\u2844",
|
||||
(3, 0): u"\u2846",
|
||||
(4, 0): u"\u2847",
|
||||
(0, 1): u"\u2880",
|
||||
(0, 2): u"\u28a0",
|
||||
(0, 3): u"\u28b0",
|
||||
(0, 4): u"\u28b8",
|
||||
(1, 1): u"\u28c0",
|
||||
(2, 1): u"\u28c4",
|
||||
(3, 1): u"\u28c6",
|
||||
(4, 1): u"\u28c7",
|
||||
(1, 2): u"\u28e0",
|
||||
(2, 2): u"\u28e4",
|
||||
(3, 2): u"\u28e6",
|
||||
(4, 2): u"\u28e7",
|
||||
(1, 3): u"\u28f0",
|
||||
(2, 3): u"\u28f4",
|
||||
(3, 3): u"\u28f6",
|
||||
(4, 3): u"\u28f7",
|
||||
(1, 4): u"\u28f8",
|
||||
(2, 4): u"\u28fc",
|
||||
(3, 4): u"\u28fe",
|
||||
(4, 4): u"\u28ff"
|
||||
}
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -169,6 +196,80 @@ def vbar(value, width):
|
|||
return VBar(value, width).get_chars()
|
||||
|
||||
|
||||
class BrailleGraph(object):
|
||||
"""
|
||||
graph using Braille chars
|
||||
scaled to passed values
|
||||
"""
|
||||
def __init__(self, values):
|
||||
"""
|
||||
Args:
|
||||
|
||||
values (list): list of values
|
||||
"""
|
||||
self.values = values
|
||||
# length of values list must be even
|
||||
# because one Braille char displays two values
|
||||
if len(self.values) % 2 == 1:
|
||||
self.values.append(0)
|
||||
self.steps = self.get_steps()
|
||||
self.parts = [tuple(self.steps[i:i+2])
|
||||
for i in range(len(self.steps))[::2]]
|
||||
|
||||
@staticmethod
|
||||
def get_height(value, unit):
|
||||
"""
|
||||
Compute height of a value relative to unit
|
||||
|
||||
Args:
|
||||
|
||||
value (number): value
|
||||
|
||||
unit (number): unit
|
||||
"""
|
||||
if value < unit / 10.:
|
||||
return 0
|
||||
elif value <= unit:
|
||||
return 1
|
||||
elif value <= unit * 2:
|
||||
return 2
|
||||
elif value <= unit * 3:
|
||||
return 3
|
||||
else:
|
||||
return 4
|
||||
|
||||
def get_steps(self):
|
||||
"""
|
||||
Convert the list of values to a list of steps
|
||||
|
||||
Return: list
|
||||
"""
|
||||
maxval = max(self.values)
|
||||
unit = maxval / 4.
|
||||
if unit == 0:
|
||||
return [0] * len(self.values)
|
||||
stepslist = []
|
||||
for value in self.values:
|
||||
stepslist.append(self.get_height(value, unit))
|
||||
return stepslist
|
||||
|
||||
def get_chars(self):
|
||||
"""
|
||||
Decide which chars to draw
|
||||
|
||||
Return: str
|
||||
"""
|
||||
chars = []
|
||||
for part in self.parts:
|
||||
chars.append(BRAILLE[part])
|
||||
return "".join(chars)
|
||||
|
||||
|
||||
def bgraph(values):
|
||||
"""wrapper function"""
|
||||
return BrailleGraph(values).get_chars()
|
||||
|
||||
|
||||
class Widget(bumblebee.store.Store):
|
||||
"""Represents a single visible block in the status bar"""
|
||||
def __init__(self, full_text="", name=""):
|
||||
|
|
Loading…
Reference in a new issue