diff --git a/outputformat/title.py b/outputformat/title.py index 003d4f7..05f456b 100644 --- a/outputformat/title.py +++ b/outputformat/title.py @@ -45,7 +45,7 @@ def linetitle(txt, style="-", color=False, cmap="cool", bold=False, return_str=F outputstring = "" txt = str(txt) - txt_size = len(txt) + txt_size = ouf.tools.real_string_length(txt) style = str(style) # bl = bottom left @@ -114,7 +114,7 @@ def boxtitle(txt, style="-", return_str=False, color=False, cmap="cool", bold=Fa """ txt = str(txt) - txt_size = len(txt) + txt_size = ouf.tools.real_string_length(txt) style = str(style) if style in ["line", "-"]: diff --git a/outputformat/tools.py b/outputformat/tools.py index f0e523a..cfcf516 100644 --- a/outputformat/tools.py +++ b/outputformat/tools.py @@ -3,6 +3,7 @@ from outputformat import emoji import colorsys from random import random +from re import sub def prepare_data(input_data, precision): @@ -203,3 +204,15 @@ def parse_color(color, cmap): color = (random(), default_saturation, 1) return color + +def real_string_length(txt): + """Compute the length of the strings without the ANSI escape sequences + + Returns + ------- + int + real length of the string txt + + """ + pattern = "\x1b\[[;\d]*m" + return len(sub(pattern,"",txt))