diff src/tools/common/regex.py @ 2297:ad2a8e8b52da

core (tools/common/regex): new ansiRemove method to remove ANSI escape codes from a string
author Goffi <goffi@goffi.org>
date Sun, 02 Jul 2017 19:53:44 +0200
parents 2daf7b4c6756
children 8b37a62336c3
line wrap: on
line diff
--- a/src/tools/common/regex.py	Sun Jul 02 19:52:21 2017 +0200
+++ b/src/tools/common/regex.py	Sun Jul 02 19:53:44 2017 +0200
@@ -23,6 +23,8 @@
 path_escape = {'%': '%25', '/': '%2F', '\\': '%5c'}
 path_escape_rev = {re.escape(v):k for k, v in path_escape.iteritems()}
 path_escape = {re.escape(k):v for k, v in path_escape.iteritems()}
+# thanks to Martijn Pieters (https://stackoverflow.com/a/14693789)
+RE_ANSI_REMOVE = re.compile(r'\x1b[^m]*m')
 
 
 def reJoin(exps):
@@ -60,3 +62,11 @@
     @return (str, unicode): unescaped string
     """
     return reSubDict(path_escape_rev_re, path_escape_rev, string)
+
+def ansiRemove(string):
+    """Remove ANSI escape codes from string
+
+    @param string(basestr): string to filter
+    @return (str, unicode): string without ANSI escape codes
+    """
+    return RE_ANSI_REMOVE.sub('', string)