From: Christian Herdtweck Date: Thu, 7 Nov 2024 08:08:55 +0000 (+0100) Subject: Create text helper to remove ansi color sequences X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=09328b974236d716b8fe00c3e273d1ee8989b30a;p=pyi2ncommon Create text helper to remove ansi color sequences --- diff --git a/src/text_helpers.py b/src/text_helpers.py index 3017176..26a9c7c 100644 --- a/src/text_helpers.py +++ b/src/text_helpers.py @@ -49,6 +49,7 @@ from builtins import print as _builtin_print from functools import partial from itertools import islice +import re from .type_helpers import isstr from sys import stdout @@ -193,6 +194,10 @@ except Exception: _STDOUT_IS_TTY = False +def strip_color(text): + """return same text but without any ansi color sequences""" + return re.sub(r"\x1b\[[\d;]*[mD]", "", text, count=0, flags=re.IGNORECASE) + def colored(text, foreground=None, background=None, style=None): """ return text with given foreground/background ANSI color escape sequence