From d04b5540655d1b78f7690e2bd0c5c041814cec11 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 1 Aug 2022 20:24:02 +0200 Subject: [PATCH] tools: idf.py: enable CLICOLOR_FORCE for interactive builds If stdout is a TTY (meaning that the output is not redirected), tell the build tool (GNU Make or Ninja) to enable colorized output. GNU Make and Ninja also check if their stdout is redirected and strip color escape sequences in that case. CLICOLOR_FORCE environment variable overrides this behavior. With this change, if the compiler was launched with the -fcolor-diagnostics flag and idf.py output is not redirected, the final output in the terminal will be colorized. (-fcolor-diagnostics is handled at CMake level by the previous commit) --- tools/idf_py_actions/constants.py | 6 +----- tools/idf_py_actions/tools.py | 8 +++++++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tools/idf_py_actions/constants.py b/tools/idf_py_actions/constants.py index 18ebe3e7c2..19e9e99587 100644 --- a/tools/idf_py_actions/constants.py +++ b/tools/idf_py_actions/constants.py @@ -12,7 +12,6 @@ GENERATORS: Dict[str, Union[str, Dict, list]] = collections.OrderedDict([ # - dry_run: command to run in dry run mode # - verbose_flag: verbose flag # - force_progression: one liner status of the progress - # - envvar: environment variables ('Ninja', { 'command': ['ninja'], 'version': ['ninja', '--version'], @@ -20,7 +19,6 @@ GENERATORS: Dict[str, Union[str, Dict, list]] = collections.OrderedDict([ 'verbose_flag': '-v', # as opposed to printing the status updates each in a in new line 'force_progression': True, - 'envvar': {} }), ]) @@ -30,9 +28,7 @@ if os.name != 'nt': 'version': [MAKE_CMD, '--version'], 'dry_run': [MAKE_CMD, '-n'], 'verbose_flag': 'VERBOSE=1', - 'force_progression': False, - # CLICOLOR_FORCE if set forcing make to print ANSI escape sequence - 'envvar': {'CLICOLOR_FORCE': '1'}} + 'force_progression': False} URL_TO_DOC = 'https://docs.espressif.com/projects/esp-idf' diff --git a/tools/idf_py_actions/tools.py b/tools/idf_py_actions/tools.py index 2eecd1d2b1..854ca48a10 100644 --- a/tools/idf_py_actions/tools.py +++ b/tools/idf_py_actions/tools.py @@ -300,11 +300,17 @@ def run_target(target_name: str, args: 'PropertyDict', env: Optional[Dict]=None, env = {} generator_cmd = GENERATORS[args.generator]['command'] - env.update(GENERATORS[args.generator]['envvar']) if args.verbose: generator_cmd += [GENERATORS[args.generator]['verbose_flag']] + # By default, GNU Make and Ninja strip away color escape sequences when they see that their stdout is redirected. + # If idf.py's stdout is not redirected, the final output is a TTY, so we can tell Make/Ninja to disable stripping + # of color escape sequences. (Requires Ninja v1.9.0 or later.) + if sys.stdout.isatty(): + if 'CLICOLOR_FORCE' not in env: + env['CLICOLOR_FORCE'] = '1' + RunTool(generator_cmd[0], generator_cmd + [target_name], args.build_dir, env, custom_error_handler, hints=not args.no_hints, force_progression=force_progression, interactive=interactive)()