From a77b06a25e0e7ed8f698d34c22d92de426bb3b64 Mon Sep 17 00:00:00 2001 From: facetoe Date: Mon, 6 Oct 2014 19:23:32 +0800 Subject: [PATCH] Added method for printing a graph of Unicode characters. --- i3pystatus/core/util.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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.