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)