Create text helper to remove ansi color sequences
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 7 Nov 2024 08:08:55 +0000 (09:08 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 12 Nov 2024 09:38:39 +0000 (10:38 +0100)
src/text_helpers.py

index 3017176..26a9c7c 100644 (file)
@@ -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