From 0314ffb5dfd74b180e5f258fcf9d12edba2b7675 Mon Sep 17 00:00:00 2001 From: Pierre-Marie de Rodat Date: Fri, 15 Apr 2016 12:18:31 +0200 Subject: [PATCH] Disable colored output if stdout or stderr is not a TTY This will make it easy to spread colors everywhere without worrying about garbage test baselines. It will also clean build logs. Change-Id: I976b74fe1712ea8e5fbfd4476f4814ae375448eb TN: minor --- langkit/utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/langkit/utils.py b/langkit/utils.py index 40d46b353..1f10aa327 100644 --- a/langkit/utils.py +++ b/langkit/utils.py @@ -10,6 +10,7 @@ from copy import copy from itertools import takewhile import inspect +import sys class StructEq(object): @@ -95,6 +96,22 @@ class Colors: WARNING = YELLOW FAIL = RED + @classmethod + def disable_colors(cls): + """ + Reset color codes to empty strings. + """ + for name in dir(cls): + value = getattr(cls, name) + if (all(c.isalpha() for c in name) and + name.upper() == name and + isinstance(value, str)): + setattr(cls, name, '') + + +if not sys.stdout.isatty() or not sys.stderr.isatty(): + Colors.disable_colors() + def col(msg, color): return "{0}{1}{2}".format(color, msg, Colors.ENDC)