in knack/cli.py [0:0]
def _should_enable_color(self):
# When run in a normal terminal, color is only enabled when all conditions are met:
# 1. [core] no_color config is not set
# 2. stdout is a tty
# - Otherwise, if the downstream command doesn't support color, Knack will fail with
# BrokenPipeError: [Errno 32] Broken pipe, like `az --version | head --lines=1`
# https://github.com/Azure/azure-cli/issues/13413
# - May also hit https://github.com/tartley/colorama/issues/200
# 3. stderr is a tty.
# - Otherwise, the output in stderr won't have LEVEL tag
# 4. out_file is stdout
no_color_config = self.config.getboolean('core', 'no_color', fallback=False)
# If color is disabled by config explicitly, never enable color
if no_color_config:
self.init_debug_log.append("Color is disabled by config.")
return False
if sys.stdout.isatty() and sys.stderr.isatty() and self.out_file is sys.stdout:
self.init_debug_log.append("Enable color in terminal.")
return True
if 'PYCHARM_HOSTED' in os.environ and sys.stdout == sys.__stdout__ and sys.stderr == sys.__stderr__:
self.init_debug_log.append("Enable color in PyCharm.")
return True
self.init_debug_log.append("Cannot enable color.")
return False