-
-
Notifications
You must be signed in to change notification settings - Fork 89
Use ANSI color codes and Unicode characters on Windows as well #662
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
@MatmaRex Thanks for the PR.
However, similar to the other PR, I need to ask you to please either provide a reproduction scenario, screenshots or tests to demonstrate what you are trying to solve. Preferably all three of these should be provided with any PR which changes things in the UI.
I can take a guess what you are trying to solve, but my guess may be wrong, so without any of the above, I can't even begin to properly evaluate the PR.
PHP enables this by default since PHP 7.2. https://www.php.net/manual/en/function.sapi-windows-vt100-support.php
PHPCS 3.x still has a minimum supported PHP version of PHP 5.4, so we cannot rely on features from PHP 7.2.
Please test your changes with PHP < 7.2 and see what breaks (and yes, things will break).
I could tentatively earmark this PR for PHPCS 4.0 , which will have a PHP 7.2 minimum, but that still doesn't negate my remarks above.
While using Windows 10 or newer, and PHP 7.2 or newer, run PHPCS with
Note:
The If you really want to support these old systems, the only thing to do would be to disable |
Given the following simple file: <?php // This is a space indented line. $a = 10; // This is a tab indented line. And running the following command on the Windows 10 command line: phpcs ./phpcs-662.php --report=code --tab-width=4 --no-colors --standard=generic ☝️ Take note of the use of On PHP 8.4, everything looks swell:
Not so much on PHP 7.0....:
Which only goes to demonstrate why I insist on tests and will continue to do so...
Proper tests will run in CI and should have caught the above. If not, the tests would not be good enough. |
Oh, right, the "visible" characters are still applied in --no-colors mode. Thanks for pointing that out.
The encoding issue is interesting – it happens with PHP 7.0, but not with PHP 7.1, which I tested with.
Proper tests will run in CI and should have caught the above. If not, the tests would not be good enough.
I'm not very familiar with GitHub's CI – can you point out which of the jobs run on Windows?
Oh, right, the "visible" characters are still applied in
--no-colorsmode. Thanks for pointing that out.The encoding issue is interesting – it happens with PHP 7.0, but not with PHP 7.1, which I tested with.
Might just be that PHP added support for the Windows ANSI codes in PHP 7.1 and only added the function related to it in PHP 7.2.
Proper tests will run in CI and should have caught the above. If not, the tests would not be good enough.
I'm not very familiar with GitHub's CI – can you point out which of the jobs run on Windows?
At this time, there are no runs on Windows as there are no tests covering code which has special conditions for Windows. As soon as such tests would be added, the Windows OS would have to be added to the test matrix/workflow. (No worries about that, I can do that).
2ec254b to
e64ea14
Compare
Might just be that PHP added support for the Windows ANSI codes in PHP 7.1 and only added the function related to it in PHP 7.2.
The ANSI codes and the Unicode characters actually appear unrelated. I found a PHP doc page that documents the latter as a change in PHP 7.1. https://www.php.net/manual/en/migration71.windows-support.php
At this time, there are no runs on Windows as there are no tests covering code which has special conditions for Windows. As soon as such tests would be added, the Windows OS would have to be added to the test matrix/workflow. (No worries about that, I can do that).
I guess I can try adding some. Probably in yet another pull request.
e64ea14 to
294252d
Compare
This reduces the size of the output, and lessens weird effects of the ANSI color code characters on text wrapping, when the result of this method is used in a sniff error message (e.g. see LanguageConstructSpacingSniff).
Windows' console supports ANSI escape sequences since Windows 10. https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences PHP enables this by default since PHP 7.2. https://www.php.net/manual/en/function.sapi-windows-vt100-support.php On older PHP versions, the output with --colors will not be correct, but this is already the case when using this option. PHP CodeSniffer special-cased Windows in this code in 2014 (bfd095d). This workaround is no longer needed today in 2024. Note that the --colors option was already supported on Windows. This only affects inline highlighting in error and debug messages.
294252d to
9a33b8c
Compare
I extended the changes here slightly, adding two additional commits (see commit messages for details):
- Update tests, which were added in Tests: Add unit tests for Common::prepareForOutput() #663
- Improve the use of control characters in general
- Use the Unicode characters on PHP 7.1+ on Windows, per our discussion above
9a33b8c to
ff05ec9
Compare
...PHP 7.1+ PHP 7.1 fixed problems with printing Unicode characters to Windows console that forced us to avoid using them. https://www.php.net/manual/en/migration71.windows-support.php
ff05ec9 to
8e976bc
Compare
Sorry about the noise, I didn't realize at first that PHPUnit 4.8 (used on PHP 5.4) does not support the @requires PHP < 7.1 syntax. And thanks for enabling automatic test runs for me :)
@jrfnl I greatly appreciate your reviews so far. I'd love to hear your thoughts on these patches again whenever you have the time. Please do not feel hurried though, I just want to make it clear that I'm still interested in completing these changes!
@jrfnl I greatly appreciate your reviews so far. I'd love to hear your thoughts on these patches again whenever you have the time. Please do not feel hurried though, I just want to make it clear that I'm still interested in completing these changes!
@MatmaRex Sorry for the delay, looking at it again now.
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.
@MatmaRex Okay, so I have looked at this again.
Aside from the PR being blocked by the lack of tests for the Code report (which may be fixed via a future iteration on #664), I'm still concerned about the side-effects of this PR.
I've left a number of comments inline for you to consider.
I'm also a little concerned about the performance impact of the liberal use of array_* functions in this utility function. On modern PHP versions, it appears to be ~25% slower, while on older PHP versions, it appears to be about ~200% slower.
Before: https://3v4l.org/E3bVW/perf
After: https://3v4l.org/7NQUU/perf
Hope this helps.
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'm not sure I understand the reasoning for this change ?
I mean, in contrast to the "Output now has fewer redundant ANSI color codes" changelog suggestion, this ends up creating more redundant ANSI color codes (at least on Windows).
Before:
image
After:
image
Screenshots generated using the following command on the previously posted code snippet on PHP 7.1 on Windows.
phpcs ./phpcs-662.php --tab-width=4 --standard=generic -vv
Also note (not necessarily to be addressed in this PR, probably should be a separate one): the --no-colors option can also be used on non-Windows OSes and the previous implementation already didn't respect that.
The PHP class does have access to the Config though via the Tokenizer::$config option, so maybe it should respect the colors setting ?
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.
Is this condition sufficient ?
If I read the https://www.php.net/manual/en/migration71.windows-support.php page, I get the impression an additional check may be needed against the value of the internal_encoding ini setting - and if that's empty, the default_charset ini setting - to verify it is set to UTF-8 ?
Also note that internal_encoding and default_charset were both only introduced in PHP 5.6: https://www.php.net/manual/en/migration56.deprecated.php#migration56.deprecated.iconv-mbstring-encoding
Same remark applies to the Code report change.
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.
This ends up adding more redundant colour codes on Windows for PHP < 7.1, similar to what is happening in the PHP tokenizer.
@MatmaRex Just checking in - do you intend to continue with this PR ? If not, I suggest closing it.
Uh oh!
There was an error while loading. Please reload this page.
Description
Use ANSI color codes and Unicode characters on Windows as well, when possible. See commit messages for details.
Suggested changelog entry
--colorsoption, the output now has more colors.Related issues/external references
(none)
Types of changes
PR checklist