colorama icon indicating copy to clipboard operation
colorama copied to clipboard

Provide `isinitialized` function

Open bdarnell opened this issue 8 years ago • 4 comments

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.

bdarnell avatar Apr 20 '17 02:04 bdarnell

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.

mivade avatar Apr 20 '17 02:04 mivade

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.

wiggin15 avatar Apr 20 '17 15:04 wiggin15

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))

wiggin15 avatar Apr 20 '17 15:04 wiggin15

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".

bdarnell avatar Apr 20 '17 15:04 bdarnell