Commit f6b446ba authored by hydrargyrum's avatar hydrargyrum
Browse files

woob.tools: respect NO_COLOR env variable

NO_COLOR is a pseudo-standard variable respected by many apps, see
https://no-color.org/
parent 1bd5f68d
......@@ -468,7 +468,8 @@ def create_default_logger(cls):
format = '%(asctime)s:%(levelname)s:%(name)s:' + cls.VERSION +\
':%(filename)s:%(lineno)d:%(funcName)s %(message)s'
handler = logging.StreamHandler(cls.stderr)
handler.setFormatter(createColoredFormatter(cls.stderr, format))
if os.getenv("NO_COLOR") is None:
handler.setFormatter(createColoredFormatter(cls.stderr, format))
return handler
@classmethod
......
......@@ -89,6 +89,7 @@ class ConsoleApplication(Application):
# shell escape strings
if sys.platform == 'win32' \
or not sys.stdout.isatty() \
or os.getenv('NO_COLOR') is not None \
or os.getenv('ANSI_COLORS_DISABLED') is not None:
#workaround to disable bold
BOLD = ''
......
......@@ -31,6 +31,7 @@
except ImportError:
def colored(s, color=None, on_color=None, attrs=None):
if os.getenv('ANSI_COLORS_DISABLED') is None \
and os.getenv('NO_COLOR') is None \
and attrs is not None and 'bold' in attrs:
return '%s%s%s' % (IFormatter.BOLD, s, IFormatter.NC)
else:
......@@ -85,7 +86,11 @@ class IFormatter(object):
NC = ConsoleApplication.NC
def colored(self, string, color, attrs=None, on_color=None):
if self.outfile != sys.stdout or not self.outfile.isatty():
if (
self.outfile != sys.stdout
or not self.outfile.isatty()
or os.getenv("NO_COLOR") is not None
):
return string
if isinstance(attrs, basestring):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment