mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-01 03:34:32 +02:00
idf.py: ensure that build log is always sanitized from color sequences
The actual output from the build tool (CMake/Ninja) may or may not contain color escape codes, depending on various factors. The output written to the log file should never include color escape codes, though. This is because color escape codes in files are usually not rendered as "color" in editors, and complicate reading. Also escape codes would break the regular expressions used to display hints for compilation errors.
This commit is contained in:
@@ -233,12 +233,6 @@ class RunTool:
|
||||
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
||||
return ansi_escape.sub('', text)
|
||||
|
||||
def prepare_for_print(out: str) -> str:
|
||||
if not output_stream.isatty():
|
||||
# delete escape sequence if we printing in environments where ANSI coloring is disabled
|
||||
return delete_ansi_escape(out)
|
||||
return out
|
||||
|
||||
def print_progression(output: str) -> None:
|
||||
# Print a new line on top of the previous line
|
||||
sys.stdout.write('\x1b[K')
|
||||
@@ -279,8 +273,15 @@ class RunTool:
|
||||
output = await read_stream()
|
||||
if not output:
|
||||
break
|
||||
output = prepare_for_print(output)
|
||||
output_file.write(output)
|
||||
output_noescape = delete_ansi_escape(output)
|
||||
# Always remove escape sequences when writing the build log.
|
||||
output_file.write(output_noescape)
|
||||
# If idf.py output is redirected and the output stream is not a TTY,
|
||||
# strip the escape sequences as well.
|
||||
# (There shouldn't be any, but just in case.)
|
||||
if not output_stream.isatty():
|
||||
output = output_noescape
|
||||
|
||||
if self.force_progression and output[0] == '[' and '-v' not in self.args and output_stream.isatty():
|
||||
# print output in progression way but only the progression related (that started with '[') and if verbose flag is not set
|
||||
print_progression(output)
|
||||
|
Reference in New Issue
Block a user