diff --git a/i3pystatus/core/util.py b/i3pystatus/core/util.py index f404aa5..c5bdcf4 100644 --- a/i3pystatus/core/util.py +++ b/i3pystatus/core/util.py @@ -364,6 +364,31 @@ def internet(): except OSError: return False +def make_graph(values, upper_limit=100.0): + """ + Draws a graph made of unicode characters. + + :param values: An array of values to graph. + :param upper_limit: Maximum value for the y axis. + :returns: Bar as a string + """ + values = [float(n) for n in values] + + # Add the upper limit to the end of the array so the graph doesn't distort + # as high values drop off the end. + values.append(float(upper_limit)) + + bar = u'_▁▂▃▄▅▆▇█' + bar_count = len(bar) - 1 + mn, mx = min(values), max(values) + extent = mx - mn + if extent == 0: + graph = '_' * len(values) + else: + graph = ''.join(bar[int( (n - mn) / extent * bar_count)] + for n in values[:len(values)-1]) # Don't show the upper limit value. + return graph + def make_bar(percentage): """ Draws a bar made of unicode box characters.