Skip to content

Commit

Permalink
better handling of ansi ctr char
Browse files Browse the repository at this point in the history
len() counts the ansi control characters which is not a desired
behavior, specially for the info of a data point.
Now we have a custom len function that removes those unwanted characters
before computing the length resulting in a correct alignment.
  • Loading branch information
kakwa committed Sep 30, 2017
1 parent cb11fc7 commit ad08111
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
18 changes: 12 additions & 6 deletions ascii_graph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import unicode_literals
import sys
import re
import collections
import copy

Expand Down Expand Up @@ -68,9 +69,9 @@ def __init__(self, line_length=79,
self.graphsymbol = self._u('█')
else:
self.graphsymbol = graphsymbol
if len(self.graphsymbol) != 1:
if self._len_noansi(self.graphsymbol) != 1:
raise Exception('Bad graphsymbol length, must be 1',
len(self.graphsymbol))
self._len_noansi(self.graphsymbol))
self.multivalue = multivalue
self.hsymbols = [self._u(''), self._u('K'), self._u('M'),
self._u('G'), self._u('T'), self._u('P'),
Expand All @@ -83,6 +84,11 @@ def __init__(self, line_length=79,
else:
self.divider = None

@staticmethod
def _len_noansi(string):
l = len(re.sub('\x1b[^m]*m', '', string))
return l

def _trans_hr(self, value):

if self.divider is None:
Expand Down Expand Up @@ -168,8 +174,8 @@ def _get_thresholds(self, data):
if maxvalue > all_thre['max_pos_value']:
all_thre['max_pos_value'] = maxvalue

if len(info) > all_thre['info_max_length']:
all_thre['info_max_length'] = len(info)
if self._len_noansi(info) > all_thre['info_max_length']:
all_thre['info_max_length'] = self._len_noansi(info)

if totalvalue_len > all_thre['value_max_length']:
all_thre['value_max_length'] = totalvalue_len
Expand Down Expand Up @@ -265,7 +271,7 @@ def _gen_graph_string_part(
def _gen_info_string(self, info, start_info_pos, line_length):
"""Generate the info string + padding
"""
number_of_space = (line_length - start_info_pos - len(info))
number_of_space = (line_length - start_info_pos - self._len_noansi(info))
return info + Pyasciigraph._u(' ') * number_of_space

def _gen_value_string(self, value, min_neg_value, color, start_value_pos, start_info_pos):
Expand Down Expand Up @@ -390,7 +396,7 @@ def graph(self, label=None, data=[]):

if not label is None:
san_label = self._sanitize_string(label)
label_len = len(san_label)
label_len = self._len_noansi(san_label)
else:
label_len = 0

Expand Down
16 changes: 8 additions & 8 deletions examples/color_multivalue_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
# Coloring data according to thresholds
from ascii_graph.colordata import hcolor

test = [('testval0', 600),
('testval1', 500),
('testval2', -400),
('testval3', 400),
('testval4', 300),
('testval5', 200),
('testval6', 100),
('testval7', 50 )]
test = [('testval0 (\033[92m+\033[0m)', 600),
('testval1 (\033[91m-\033[0m)', 500),
('testval2 (\033[92m+\033[0m)', -400),
('testval3 (\033[92m+\033[0m)', 400),
('testval4 (\033[92m+\033[0m)', 300),
('testval5 (\033[91m-\033[0m)', 200),
('testval6 (\033[92m+\033[0m)', 100),
('testval7 (\033[92m+\033[0m)', 50 )]

thresholds = {
51: Gre,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def test_unsorted_default_params(self):
gprint(expected)
assert res == expected

def test_noansi_info(self):
test = [('long_labe☭ (\033[92m+\033[0m)', 423), ('sl (\033[91m-\033[0m)', 1234), ('line3 (\033[91m-\033[0m)', 531), ('line4', 200), ('line5', 834)]
graph = Pyasciigraph()
res = graph.graph('☭test print', test)
expected = [u'\u262dtest print', u'###############################################################################', u'\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 423 long_labe\u262d (\x1b[92m+\x1b[0m)', u'\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 1234 sl (\x1b[91m-\x1b[0m) ', u'\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 531 line3 (\x1b[91m-\x1b[0m) ', u'\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 200 line4 ', u'\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 834 line5 ']
gprint(res)
gprint(expected)
print res
assert res == expected



def test_float_format(self):
test = [('long_labe☭', 423.197), ('sl', 1234.12341), ('line3', 531.11), ('line4', 200), ('line5', 834)]
graph = Pyasciigraph(float_format='{0:,.2f}')
Expand Down

0 comments on commit ad08111

Please sign in to comment.