fix(monitor/ansi_converter): do not catch status and cursor escape sequences

This commit is contained in:
Peter Dragun
2023-03-10 15:49:22 +01:00
parent 5746495450
commit d47c309d7f

View File

@@ -28,15 +28,14 @@ if os.name == 'nt':
SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute # type: ignore SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute # type: ignore
def get_converter(orig_output_method=None, decode_output=False, force_color=False): def get_converter(orig_output_method=None, force_color=False):
# type: (Any[TextIO, Optional[TextIOBase]], bool, bool) -> Union[ANSIColorConverter, Optional[TextIOBase]] # type: (Any[TextIO, Optional[TextIOBase]], bool) -> Union[ANSIColorConverter, Optional[TextIOBase]]
""" """
Returns an ANSIColorConverter on Windows and the original output method (orig_output_method) on other platforms. Returns an ANSIColorConverter on Windows and the original output method (orig_output_method) on other platforms.
The ANSIColorConverter with decode_output=True will decode the bytes before passing them to the output The ANSIColorConverter with force_color=True will be forced to use ANSI color codes
The ANSIColorConverter with force_color=True will be forced to convert ANSI in windows format
""" """
if os.name == 'nt': if os.name == 'nt' and not force_color:
return ANSIColorConverter(orig_output_method, decode_output, force_color) return ANSIColorConverter(orig_output_method, force_color)
return orig_output_method return orig_output_method
@@ -51,10 +50,11 @@ class ANSIColorConverter(object):
least-bad working solution, as winpty doesn't support any "passthrough" mode for raw output. least-bad working solution, as winpty doesn't support any "passthrough" mode for raw output.
""" """
def __init__(self, output=None, decode_output=False, force_color=False): def __init__(self, output=None, force_color=False):
# type: (TextIOBase, bool, bool) -> None # type: (TextIOBase, bool) -> None
self.output = output self.output = output
self.decode_output = decode_output # string stream has to be encoded and then decoded back for proper escape sequence handling
self.decode_output = isinstance(output, TextIOBase)
self.handle = GetStdHandle(STD_ERROR_HANDLE if self.output == sys.stderr else STD_OUTPUT_HANDLE) self.handle = GetStdHandle(STD_ERROR_HANDLE if self.output == sys.stderr else STD_OUTPUT_HANDLE)
self.matched = b'' self.matched = b''
self.force_color = force_color # always print ANSI for colors if true self.force_color = force_color # always print ANSI for colors if true
@@ -82,6 +82,7 @@ class ANSIColorConverter(object):
data = bytearray(data) data = bytearray(data)
else: else:
data = bytearray(data, 'utf-8') data = bytearray(data, 'utf-8')
for b in data: for b in data:
b = bytes([b]) b = bytes([b])
length = len(self.matched) length = len(self.matched)
@@ -91,12 +92,17 @@ class ANSIColorConverter(object):
self.matched = b self.matched = b
elif (length == 1 and b == b'[') or (1 < length < 7): elif (length == 1 and b == b'[') or (1 < length < 7):
self.matched += b self.matched += b
if not self.force_color and self.matched == ANSI_NORMAL.encode('latin-1'): # reset console if self.matched == ANSI_NORMAL.encode('latin-1'): # reset console
# Flush is required only with Python3 - switching color before it is printed would mess up the console # Flush is required only with Python3 - switching color before it is printed would mess up the console
self.flush() self.flush()
SetConsoleTextAttribute(self.handle, FOREGROUND_GREY) SetConsoleTextAttribute(self.handle, FOREGROUND_GREY)
self.matched = b'' self.matched = b''
elif self.force_color or len(self.matched) == 7: # could be an ANSI sequence elif (length == 2 and b not in [b'0', b'1']) or (length == 3 and b != b';'):
# other escape sequences like cursor position and status reports
self._output_write(self.matched)
self.flush()
self.matched = b''
elif length == 6: # could be an ANSI sequence
m = re.match(RE_ANSI_COLOR, self.matched) m = re.match(RE_ANSI_COLOR, self.matched)
if m is not None: if m is not None:
color = ANSI_TO_WINDOWS_COLOR[int(m.group(2))] color = ANSI_TO_WINDOWS_COLOR[int(m.group(2))]
@@ -107,6 +113,7 @@ class ANSIColorConverter(object):
SetConsoleTextAttribute(self.handle, color) SetConsoleTextAttribute(self.handle, color)
else: else:
self._output_write(self.matched) # not an ANSI color code, display verbatim self._output_write(self.matched) # not an ANSI color code, display verbatim
self.flush()
self.matched = b'' self.matched = b''
else: else:
self._output_write(b) self._output_write(b)