mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-01 03:34:32 +02:00
fix(monitor/ansi_converter): do not catch status and cursor escape sequences
This commit is contained in:
@@ -28,15 +28,14 @@ if os.name == 'nt':
|
||||
SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute # type: ignore
|
||||
|
||||
|
||||
def get_converter(orig_output_method=None, decode_output=False, force_color=False):
|
||||
# type: (Any[TextIO, Optional[TextIOBase]], bool, bool) -> Union[ANSIColorConverter, Optional[TextIOBase]]
|
||||
def get_converter(orig_output_method=None, force_color=False):
|
||||
# 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.
|
||||
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 convert ANSI in windows format
|
||||
The ANSIColorConverter with force_color=True will be forced to use ANSI color codes
|
||||
"""
|
||||
if os.name == 'nt':
|
||||
return ANSIColorConverter(orig_output_method, decode_output, force_color)
|
||||
if os.name == 'nt' and not force_color:
|
||||
return ANSIColorConverter(orig_output_method, force_color)
|
||||
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.
|
||||
"""
|
||||
|
||||
def __init__(self, output=None, decode_output=False, force_color=False):
|
||||
# type: (TextIOBase, bool, bool) -> None
|
||||
def __init__(self, output=None, force_color=False):
|
||||
# type: (TextIOBase, bool) -> None
|
||||
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.matched = b''
|
||||
self.force_color = force_color # always print ANSI for colors if true
|
||||
@@ -82,6 +82,7 @@ class ANSIColorConverter(object):
|
||||
data = bytearray(data)
|
||||
else:
|
||||
data = bytearray(data, 'utf-8')
|
||||
|
||||
for b in data:
|
||||
b = bytes([b])
|
||||
length = len(self.matched)
|
||||
@@ -91,12 +92,17 @@ class ANSIColorConverter(object):
|
||||
self.matched = b
|
||||
elif (length == 1 and b == b'[') or (1 < length < 7):
|
||||
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
|
||||
self.flush()
|
||||
SetConsoleTextAttribute(self.handle, FOREGROUND_GREY)
|
||||
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)
|
||||
if m is not None:
|
||||
color = ANSI_TO_WINDOWS_COLOR[int(m.group(2))]
|
||||
@@ -107,6 +113,7 @@ class ANSIColorConverter(object):
|
||||
SetConsoleTextAttribute(self.handle, color)
|
||||
else:
|
||||
self._output_write(self.matched) # not an ANSI color code, display verbatim
|
||||
self.flush()
|
||||
self.matched = b''
|
||||
else:
|
||||
self._output_write(b)
|
||||
|
Reference in New Issue
Block a user