From 09328b974236d716b8fe00c3e273d1ee8989b30a Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 7 Nov 2024 09:08:55 +0100 Subject: [PATCH] Create text helper to remove ansi color sequences --- src/text_helpers.py | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-) 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 -- 1.7.1