3
3
4
4
from __future__ import unicode_literals
5
5
import sys
6
+ import re
6
7
import collections
7
8
import copy
8
9
@@ -68,9 +69,9 @@ def __init__(self, line_length=79,
68
69
self .graphsymbol = self ._u ('█' )
69
70
else :
70
71
self .graphsymbol = graphsymbol
71
- if len (self .graphsymbol ) != 1 :
72
+ if self . _len_noansi (self .graphsymbol ) != 1 :
72
73
raise Exception ('Bad graphsymbol length, must be 1' ,
73
- len (self .graphsymbol ))
74
+ self . _len_noansi (self .graphsymbol ))
74
75
self .multivalue = multivalue
75
76
self .hsymbols = [self ._u ('' ), self ._u ('K' ), self ._u ('M' ),
76
77
self ._u ('G' ), self ._u ('T' ), self ._u ('P' ),
@@ -83,6 +84,11 @@ def __init__(self, line_length=79,
83
84
else :
84
85
self .divider = None
85
86
87
+ @staticmethod
88
+ def _len_noansi (string ):
89
+ l = len (re .sub ('\x1b [^m]*m' , '' , string ))
90
+ return l
91
+
86
92
def _trans_hr (self , value ):
87
93
88
94
if self .divider is None :
@@ -168,8 +174,8 @@ def _get_thresholds(self, data):
168
174
if maxvalue > all_thre ['max_pos_value' ]:
169
175
all_thre ['max_pos_value' ] = maxvalue
170
176
171
- if len (info ) > all_thre ['info_max_length' ]:
172
- all_thre ['info_max_length' ] = len (info )
177
+ if self . _len_noansi (info ) > all_thre ['info_max_length' ]:
178
+ all_thre ['info_max_length' ] = self . _len_noansi (info )
173
179
174
180
if totalvalue_len > all_thre ['value_max_length' ]:
175
181
all_thre ['value_max_length' ] = totalvalue_len
@@ -265,7 +271,7 @@ def _gen_graph_string_part(
265
271
def _gen_info_string (self , info , start_info_pos , line_length ):
266
272
"""Generate the info string + padding
267
273
"""
268
- number_of_space = (line_length - start_info_pos - len (info ))
274
+ number_of_space = (line_length - start_info_pos - self . _len_noansi (info ))
269
275
return info + Pyasciigraph ._u (' ' ) * number_of_space
270
276
271
277
def _gen_value_string (self , value , min_neg_value , color , start_value_pos , start_info_pos ):
@@ -390,7 +396,7 @@ def graph(self, label=None, data=[]):
390
396
391
397
if not label is None :
392
398
san_label = self ._sanitize_string (label )
393
- label_len = len (san_label )
399
+ label_len = self . _len_noansi (san_label )
394
400
else :
395
401
label_len = 0
396
402
0 commit comments