New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
autoreset=True appears not to work #47
Comments
This works correctly if you don't use |
When I forwarded this bug report, I hadn't realised that it was the redirection that was causing the autoreset codes to be omitted. Thanks for catching that @wiggin15! The aim behind the fix for issue 18 was that Colorama should behave like standard unixy utilities such as 'ls' or 'grep', which only put ANSI color codes in their output if it is going to a terminal. If their stdout is redirected to a file, they do not use color (unless you add extra flags to force them to do so.) If that is deemed to be desirable behaviour, then: (a) the omission of the autoreset codes is working as intended, and On the other hand, I take your point that it might be simpler to handle this, if at all, at the application level, and colorama should just always print color codes when it is asked to. The downside of that is that lots of diverse applications would have to replicate these 'is_tty' checks (not a biggie really), but also that it's a break from the philosophy of the current behaviour, which I'd be nervous about given Colorama's surprisingly large install base. |
Changing the implementation to always print the codes may be a break from the current philosophy, but not from the current behavior. If we fix this (autoreset), then colorama will continue to work as it does now, and will print colors in output redirections, so no applications will break. btw, here's a quick look at others:
|
If we really want, we can add (yet another) flag to |
Fair enough then. I thought I'd applied this tty check with wider scope than that, but apparently not. |
It gets deeper... Long explanation, sorry: On Windows we also check is_a_tty, because colorama can't convert anything using the Win32 API if there is no console (that's a different reason than the one we discussed. When there is redirection on Windows we can't do conversion, whether we want to or not). The proper fix should be to change:
to
Then, when autoreset is on:
In other words:
Another note: if init is called on Linux with autoreset=True, strip=True, what should happen? The fix above disables the autoreset in this case, which makes sense because there will be nothing to reset - the escape codes are stripped. I hope this wasn't too complicated. Am I making sense? |
Lordy. Thank heavens someone has their wits about them. Yep, when I trace through what you wrote carefully, that makes perfect sense. Linux autoreset=True, strip=True: My gut feel is that strip ought to trump autoreset, so nothing should be printed by the autoreset. My thought-experiment example use case for 'strip' is "my output device doesn't handle ansi codes, and I just want to avoid printing junk characters on the screen." |
I pushed the commit to fix this. |
reported by email from jason kenny via jonathan hartley (tartley)
If I write a simple program.. ie
and run on windows
I get this in out.txt
not the expected:
Is this known?
The text was updated successfully, but these errors were encountered: