🐛 Fix clean_ansi's unicode compatibility

Closes #4392
This commit is contained in:
Gina Häußge committed 2022-02-07 10:37:12 +01:00
1 parent 5eeda404b6
commit 36d6768f6a
1 file changed
+11 -3
+11 -3
View File
@@ -31,7 +31,12 @@ _ANSI_CSI_PATTERN = (
b"\001?\033\\[(\\??(?:\\d|;)*)([a-zA-Z])\002?" # Control Sequence Introducer
)
_ANSI_OSC_PATTERN = b"\001?\033\\]((?:.|;)*?)(\x07)\002?" # Operating System Command
_ANSI_REGEX = re.compile(b"|".join([_ANSI_CSI_PATTERN, _ANSI_OSC_PATTERN]))
_ANSI_PATTERN = b"|".join([_ANSI_CSI_PATTERN, _ANSI_OSC_PATTERN])
_ANSI_REGEX = {
"bytes": re.compile(_ANSI_PATTERN),
"unicode": re.compile(_ANSI_PATTERN.decode("utf-8")),
}
def clean_ansi(line):
@@ -52,10 +57,13 @@ def clean_ansi(line):
>>> text = b"We \x1b[?25lhide the cursor here and then \x1b[?25hshow it again here"
>>> clean_ansi(text) # doctest: +ALLOW_BYTES
'We hide the cursor here and then show it again here'
>>> text = "We \x1b[?25lhide the cursor here and then \x1b[?25hshow it again here in unicode"
>>> clean_ansi(text) # doctest: +ALLOW_BYTES
'We hide the cursor here and then show it again here in unicode'
"""
if isinstance(line, unicode):
return _ANSI_REGEX.sub(b"", line.encode("latin1")).decode("latin1")
return _ANSI_REGEX.sub(b"", line)
return _ANSI_REGEX["unicode"].sub("", line)
return _ANSI_REGEX["bytes"].sub(b"", line)
class CommandlineError(Exception):