From a7ebd892aacf11a5f6f2c2b8967fa27bec2c6503 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Wed, 9 Oct 2019 13:07:36 -0300 Subject: [PATCH] feat: change clean to strip ansi codes instead of all text? --- crayons.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/crayons.py b/crayons.py index 1e32dda..5bdc42f 100644 --- a/crayons.py +++ b/crayons.py @@ -119,16 +119,6 @@ class ColoredString(object): return ColoredString(self.color, s) -def clean(s): - strip = re.compile(r"([^-_a-zA-Z0-9!@#%&=,/'\";:~`\$\^\*\(\)\+\[\]\.\{\}\|\?\<\>\\]+|[^\s]+)") # noqa: E501 - txt = strip.sub('', str(s)) - - strip = re.compile(r'\[\d+m') - txt = strip.sub('', txt) - - return txt - - _colors = {x: x.upper() for x in COLORS} _colors['normal'] = 'RESET' @@ -140,6 +130,13 @@ for key, val in _colors.items(): del key, val, _colors, function +def clean(s): + strip = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') + txt = strip.sub('', s) + + return txt + + def random(string, always=False, bold=False, colors=COLORS): """Selects a color at random from a list.""" colors = list(filter(lambda color: color in COLORS, colors)) or COLORS