Skip to content

Enable ANSI support on Windows #186#232

Closed
jaredtrog wants to merge 6 commits intogoogle:masterfrom
jaredtrog:issue-186
Closed

Enable ANSI support on Windows #186#232
jaredtrog wants to merge 6 commits intogoogle:masterfrom
jaredtrog:issue-186

Conversation

@jaredtrog
Copy link
Copy Markdown
Contributor

@jaredtrog jaredtrog commented Mar 5, 2020

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_DISABLED environment variable.

Additionally, native ANSI sequences are enabled on the Windows 10 platform when support is detected.

Tested on the following platforms:

  • Windows Server 2012R2
  • Windows 10
  • Windows Server 2016
  • Windows Server 2019

With the following consoles:

Fixes #186

@googlebot googlebot added the cla: yes Author has signed CLA label Mar 5, 2020
Copy link
Copy Markdown
Collaborator

@dbieber dbieber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jaredtrog
Copy link
Copy Markdown
Contributor Author

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 colorama.init called wrap. By default wrap is True and it:

'''
Implements a 'write()' method which, on Windows, will strip ANSI character
sequences from the text, and if outputting to a tty, will convert them into
win32 function calls.
'''

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 enable_native_ansi in setup_color.py that attempts to do so. If enabling native ansi support fails, it falls back to win32 calls by setting SHOULD_WRAP = True. All other versions of Windows will use the win32 calls for clean, but not pretty, output.

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?

@dbieber
Copy link
Copy Markdown
Collaborator

dbieber commented Mar 6, 2020

Thanks for the explanation. 🙏

so it may be better to have this portion fail silently by disabling ANSI?

Yeah, I think so. No need to update the PR though. We'll handle that as part of merging it.

python-fire-bot pushed a commit that referenced this pull request Mar 6, 2020
COPYBARA_INTEGRATE_REVIEW=#232 from jaredtrog:issue-186 61f6740
PiperOrigin-RevId: 299419699
Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
@dbieber
Copy link
Copy Markdown
Collaborator

dbieber commented Mar 6, 2020

Merged!
🙏

@dbieber dbieber closed this Mar 6, 2020
@jaredtrog jaredtrog deleted the issue-186 branch March 6, 2020 21:57
CherylMoonba added a commit to CherylMoonba/devices that referenced this pull request Jul 20, 2025
COPYBARA_INTEGRATE_REVIEW=google/python-fire#232 from jaredtrog:issue-186 61f67405fd842a52a3ba6353789804713190cb43
PiperOrigin-RevId: 299419699
Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
KurtMuelle added a commit to KurtMuelle/obtained that referenced this pull request Jul 22, 2025
COPYBARA_INTEGRATE_REVIEW=google/python-fire#232 from jaredtrog:issue-186 61f67405fd842a52a3ba6353789804713190cb43
PiperOrigin-RevId: 299419699
Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
hwlegends pushed a commit to hwlegends/python-fire that referenced this pull request Aug 20, 2025
COPYBARA_INTEGRATE_REVIEW=google/python-fire#232 from jaredtrog:issue-186 61f67405fd842a52a3ba6353789804713190cb43
PiperOrigin-RevId: 299419699
Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
Hernandez19891 added a commit to Hernandez19891/Illumina that referenced this pull request Sep 15, 2025
COPYBARA_INTEGRATE_REVIEW=google/python-fire#232 from jaredtrog:issue-186 61f67405fd842a52a3ba6353789804713190cb43
PiperOrigin-RevId: 299419699
Change-Id: I72d9ac020e22b9b3c1eadfad466072ce6d2dca3f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Author has signed CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

incorrect control code displayed in Windows console.

3 participants