Enable ANSI support on Windows #186#232
Conversation
dbieber
left a comment
There was a problem hiding this comment.
This looks good.
If we're on windows, we try to import colorama; if we cannot, we'll just disable coloring at line 61.
If we can import colorama, the logic from L37-L58 is entirely about determining SHOULD_WRAP.
I can't say I fully understand the criteria for setting SHOULD_WRAP, but you've tested it thoroughly so sgtm.
Do we actually want that print statement at 55?
It seems like even in that case (and I don't fully understand what that case is), Fire will continue operating normally.
|
I agree, the flow here is a bit opaque. Here is a bit more detail around SHOULD_WRAP. The SHOULD_WRAP decision refers to a boolean kwarg in
We get clean output with the win32 function calls that includes bold and other formatting attributes but not pretty output, like underlines, since those characters are apparently not supported in the win32 function calls. The goal was to prefer native ANSI where supported to get the most consistent output but fall back to win32 function calls if native ANSI is not supported. That is the purpose of the SHOULD_WRAP decision. On Windows 10 platforms (including Server 2016 and 2019), ANSI support can be enabled in the console. L39 - L58 is an adaptation from the function Arriving at the print statements means that something has gone quite wrong when attempting to enable native ansi support in a console that should support it. I agree that it isn't necessarily Fire's job to ensure consoles are configured correctly so it may be better to have this portion fail silently by disabling ANSI? |
|
Thanks for the explanation. 🙏
Yeah, I think so. No need to update the PR though. We'll handle that as part of merging it. |
|
Merged! |
COPYBARA_INTEGRATE_REVIEW=google/python-fire#232 from jaredtrog:issue-186 61f67405fd842a52a3ba6353789804713190cb43 PiperOrigin-RevId: 299419699 Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
COPYBARA_INTEGRATE_REVIEW=google/python-fire#232 from jaredtrog:issue-186 61f67405fd842a52a3ba6353789804713190cb43 PiperOrigin-RevId: 299419699 Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
COPYBARA_INTEGRATE_REVIEW=google/python-fire#232 from jaredtrog:issue-186 61f67405fd842a52a3ba6353789804713190cb43 PiperOrigin-RevId: 299419699 Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
COPYBARA_INTEGRATE_REVIEW=google/python-fire#232 from jaredtrog:issue-186 61f67405fd842a52a3ba6353789804713190cb43 PiperOrigin-RevId: 299419699 Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
This commit enables support for correct ANSI output on Windows when the colorama library is installed. If the colorama library is not installed then ANSI characters in output are disabled with the
ANSI_COLORS_DISABLEDenvironment variable.Additionally, native ANSI sequences are enabled on the Windows 10 platform when support is detected.
Tested on the following platforms:
With the following consoles:
Fixes #186