core (tools/common/regex): new ansiRemove method to remove ANSI escape codes from a string

This commit is contained in:
Goffi 2017-07-02 19:53:44 +02:00
commit 828d1564fb

View file

@ -23,6 +23,8 @@ import re
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 @@ def pathUnescape(string):
@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)