diff --git a/README.rst b/README.rst index 6e18167..6d8924e 100644 --- a/README.rst +++ b/README.rst @@ -176,12 +176,16 @@ init(autoreset=False): init(strip=None): Pass ``True`` or ``False`` to override whether ansi codes should be stripped from the output. The default behaviour is to strip if on Windows - or if output is redirected (not a tty). + or if output is redirected (not a tty), and if the ``FORCE_COLOR`` + environment variable is not set (with any value) or ``NO_COLOR`` is + set (with any value). init(convert=None): Pass ``True`` or ``False`` to override whether to convert ANSI codes in the output into win32 calls. The default behaviour is to convert if on Windows - and output is to a tty (terminal). + and output is to a tty (terminal), or the ``FORCE_COLOR`` environment + variable is set (with any value) and ``NO_COLOR`` is not set (with any + value). init(wrap=True): On Windows, colorama works by replacing ``sys.stdout`` and ``sys.stderr`` diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index 6039a05..2919ca3 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -87,14 +87,21 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False): # to support the ANSI codes. conversion_supported = on_windows and winapi_test() + # check if this is a TTY or if the FORCE_COLOR environment variable + # has been set and NO_COLOR has not been set. + not_closed = not self.stream.closed + force_color = 'FORCE_COLOR' in os.environ.keys() + no_color = 'NO_COLOR' in os.environ.keys() + color_allowed = ((not_closed and self.stream.isatty()) or force_color) and not no_color + # should we strip ANSI sequences from our output? if strip is None: - strip = conversion_supported or (not self.stream.closed and not self.stream.isatty()) + strip = conversion_supported or (not_closed and not color_allowed) self.strip = strip # should we should convert ANSI sequences into win32 calls? if convert is None: - convert = conversion_supported and not self.stream.closed and self.stream.isatty() + convert = conversion_supported and not_closed and color_allowed self.convert = convert # dict of ansi codes to win32 functions and parameters