From 574649545050ef8ef6f08773a826c2b58733f1c8 Mon Sep 17 00:00:00 2001 From: Peter Dragun Date: Fri, 13 Jan 2023 14:19:45 +0100 Subject: [PATCH] bug(idf_monitor): fix color on windows with hints Closes https://github.com/espressif/esp-idf/issues/9610 --- components/console/linenoise/linenoise.c | 2 +- docs/en/api-guides/tools/idf-py.rst | 2 +- tools/idf_py_actions/serial_ext.py | 6 +++--- tools/idf_py_actions/tools.py | 11 ++++++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index a21673184d..48e510f521 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -1046,7 +1046,7 @@ int linenoiseProbe(void) { flushWrite(); /* Try to read response */ - int timeout_ms = 300; + int timeout_ms = 500; const int retry_ms = 10; size_t read_bytes = 0; while (timeout_ms > 0 && read_bytes < 4) { // response is ESC[0n or ESC[3n diff --git a/docs/en/api-guides/tools/idf-py.rst b/docs/en/api-guides/tools/idf-py.rst index 9bdf505fdb..8f4cfc9102 100644 --- a/docs/en/api-guides/tools/idf-py.rst +++ b/docs/en/api-guides/tools/idf-py.rst @@ -146,7 +146,7 @@ or partition table as applicable. Hints on how to resolve errors ============================== -``idf.py`` will try to suggest hints on how to resolve errors. It works with a database of hints stored in :idf_file:`tools/idf_py_actions/hints.yml` and the hints will be printed if a match is found for the given error. The monitor, menuconfig, gdb and openocd targets are not supported at the moment by automatic hints on resolving errors. +``idf.py`` will try to suggest hints on how to resolve errors. It works with a database of hints stored in :idf_file:`tools/idf_py_actions/hints.yml` and the hints will be printed if a match is found for the given error. The menuconfig, gdb and openocd targets are not supported at the moment by automatic hints on resolving errors. The ``--no-hints`` argument of ``idf.py`` can be used to turn the hints off in case they are not desired. diff --git a/tools/idf_py_actions/serial_ext.py b/tools/idf_py_actions/serial_ext.py index 51dd9f6ca6..f24196053b 100644 --- a/tools/idf_py_actions/serial_ext.py +++ b/tools/idf_py_actions/serial_ext.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 import json @@ -154,9 +154,9 @@ def action_extensions(base_actions: Dict, project_path: str) -> Dict: idf_py = [PYTHON] + _get_commandline_options(ctx) # commands to re-run idf.py monitor_args += ['-m', ' '.join("'%s'" % a for a in idf_py)] - hints = False # Temporarily disabled because of https://github.com/espressif/esp-idf/issues/9610 + hints = not args.no_hints - RunTool('idf_monitor', monitor_args, args.project_dir, build_dir=args.build_dir, hints=hints, interactive=True)() + RunTool('idf_monitor', monitor_args, args.project_dir, build_dir=args.build_dir, hints=hints, interactive=True, convert_output=True)() def flash(action: str, ctx: click.core.Context, args: PropertyDict) -> None: """ diff --git a/tools/idf_py_actions/tools.py b/tools/idf_py_actions/tools.py index 1b46c9c43e..8b660dab58 100644 --- a/tools/idf_py_actions/tools.py +++ b/tools/idf_py_actions/tools.py @@ -12,6 +12,7 @@ from typing import Any, Dict, List, Match, Optional, TextIO, Tuple, Union import click import yaml +from idf_monitor_base.ansi_color_converter import get_ansi_converter from .constants import GENERATORS from .errors import FatalError @@ -165,7 +166,7 @@ def fit_text_in_terminal(out: str) -> str: class RunTool: def __init__(self, tool_name: str, args: List, cwd: str, env: Dict=None, custom_error_handler: FunctionType=None, build_dir: str=None, - hints: bool=True, force_progression: bool=False, interactive: bool=False) -> None: + hints: bool=True, force_progression: bool=False, interactive: bool=False, convert_output: bool=False) -> None: self.tool_name = tool_name self.args = args self.cwd = cwd @@ -176,6 +177,7 @@ class RunTool: self.hints = hints self.force_progression = force_progression self.interactive = interactive + self.convert_output = convert_output def __call__(self) -> None: def quote_arg(arg: str) -> str: @@ -275,6 +277,9 @@ class RunTool: # and still can not decode it we can just ignore some bytes return buffer.decode(errors='ignore') + # use ANSI color converter for Monitor on Windows + output_converter = get_ansi_converter(output_stream) if self.convert_output else output_stream + try: with open(output_filename, 'w', encoding='utf8') as output_file: while True: @@ -297,8 +302,8 @@ class RunTool: # print output in progression way but only the progression related (that started with '[') and if verbose flag is not set print_progression(output) else: - output_stream.write(output) - output_stream.flush() + output_converter.write(output) + output_converter.flush() except (RuntimeError, EnvironmentError) as e: yellow_print('WARNING: The exception {} was raised and we can\'t capture all your {} and ' 'hints on how to resolve errors can be not accurate.'.format(e, output_stream.name.strip('<>')))