colorama
colorama copied to clipboard
Provide `isinitialized` function
In tornadoweb/tornado#2019, we need a way to tell whether colorama is enabled (analogous to the check a curses-based system would do by looking at the TERM environment variable). There doesn't appear to be any documented way to do this. We're considering a couple of undocumented hacks to discover this, but it would be better to have a documented and supported solution.
Specifically, the hacks are to either check that sys.stderr is the same as colorama.initialise.wrapped_stderr or that sys.stderr is (on Windows) an instance of colorama.ansitowin32.StreamWrapper.
The first suggested fix (check if sys.stderr is wrapped_stderr) won't work for sure because in some cases wrapped_stderr could be the original sys.stderr (not really a wrapped stream) - so they could be equal even if colorama is not in effect. This happens when wrapper.should_wrap() is False.
What that means is that you can call colorama.init and it will not wrap anything. So maybe what we need is not isinitialized, but is_wrapped?
Something like this:
def is_wrapped():
return (isinstance(sys.stdout, StreamWrapper) or
isinstance(sys.stderr, StreamWrapper))
I think is_wrapped is too specific in light of #104 - in the future colorama might recognize that the terminal supports color without wrapping anything. What we want is basically "does stderr support color".