-
Notifications
You must be signed in to change notification settings - Fork 11.2k
enable ANSI color (instead of ANSI color codes) in the Windows terminal #4393 #4403
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
Conversation
…Windows terminal (issue scrapy#4393)
…Windows terminal (issue scrapy#4393)
Codecov Report
@@ Coverage Diff @@
## master #4403 +/- ##
==========================================
+ Coverage 84.90% 85.25% +0.34%
==========================================
Files 162 162
Lines 9748 9760 +12
Branches 1437 1439 +2
==========================================
+ Hits 8277 8321 +44
+ Misses 1211 1178 -33
- Partials 260 261 +1
|
|
Ideally the colorization should stay enabled on the terminals that actually support it, such as the ones from WSL or MSYS, but I suspect we need to check terminfo for that which is a lot of work for no significant gain. OTOH this PR doesn't allow enabling the colorization explicitly on Windows, maybe the check should be earlier. |
I just made a read for the problem and found out that Windows 10's console recently began interpreting escapes, but only with |
|
I wonder if this new code works on pre-Windows 10 terminals? And again, what about non-cmd.exe terminals? And I'd want some error handling there. |
I just added a check for the windows 10 release and for specific version |
For unix terminals running on windows its working fine according to me, @wRAR Please check it once. |
|
Just for reference, Django’s implementation: https://github.com/django/django/blob/master/django/core/management/color.py#L12 |
|
@wRAR Is there anything that concerns you about the changes that I have made? |
|
Django’s implementation seems less prone to issues to me, as it does not rely on specific Windows versions. Could you check if it also works for us? |
|
@akshaysharmajs my point about error handling is still valid. |
|
@wRAR @Gallaecio thanks for reply. I have added some error handling (used django's implementation for reference). |
|
@Gallaecio @elacuesta Please go through the changes. I have used only normal terminal formatter for all platforms and added a separate test for windows by mocking |
This test is passing fine as |
|
Could you work on 100% coverage of your changes? (see https://codecov.io/gh/scrapy/scrapy/compare/c86a1035dd9b8b10acaf8f9e8bdb1b5494a287e2...91bf06c48724a844886d1514e9c49b0ff3bb5a68/diff) |
|
Okay, I am working on it. |
scrapy/utils/display.py
Outdated
| if sys.platform == "win32" and parse_version(version()) >= parse_version("10.0.14393"): | ||
| try: | ||
| import ctypes | ||
| kernel32 = ctypes.windll.kernel32 | ||
| handle = kernel32.GetStdHandle(-11) | ||
| # set `ENABLE_VIRTUAL_TERMINAL_PROCESSING` flag | ||
| if not kernel32.SetConsoleMode(handle, 7): | ||
| raise ValueError | ||
| except ValueError: | ||
| return text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Gallaecio please suggest how should I cover this part. I tried mocking windows platform but I think parse_version is still failing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please push the test code that fails, so that I can have a look?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have pushed the test code.Please have a look, windows mocking part is failing giving this error AttributeError: <module 'ctypes' from '/usr/lib/python3.6/ctypes/__init__.py'> does not have the attribute 'windll'
|
Please, have a look at akshaysharmajs#1 It’s a refactoring proposal, where separate methods are used in the color-handling implementation for easier testing and better code readability. Note that I removed the |
Refactor output color handling
|
@Gallaecio thanks for the changes, looks perfect and precise to me. I see removing |
I thought those Windows versions would not support color. Is that what curses was for? Do older Windows versions correctly parse ANSI color codes if curses is installed? If so, maybe it makes sense to add back the curses code (but only for those old Windows versions?). — On a separate note, my refactoring broke the Flake8 checks, as well as Python 3.5 tests through the use of f-strings (not the first time I forgot Scrapy cannot use them yet 🤦 ). |
|
Older versions of windows also interpret ansi escape codes as terminal processing is enabled by default and in newer versions we have to enable it separately. |
😄😄 no problem I will fix it. |
Oh, I had no idea. I see you’ve already fixed the logic accordingly, so 👍. |
|
Yes I have fixed it accordingly. 😃 |
|
Please suggest, what else can be done here?? |
|
Thank you! |
modified scrapy/utils/display.py to stop ANSI color sequences in the Windows terminal (issue #4393)