Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Detect CLI color support for Windows 10 build 10586 #18385

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

Closed
wants to merge 5 commits into from

Conversation

mlocati
Copy link
Contributor

@mlocati mlocati commented Mar 31, 2016

Newer Windows 10 versions (builds starting from 10586) offer VT100 color support.
See http://www.nivot.org/blog/post/2016/02/04/Windows-10-TH2-(v1511)-Console-Host-Enhancements

Q A
Branch? master - Maybe it could be backported to other branches too
Bug fix? maybe 😉 - Do you you consider a bug not having colors on Windows?
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

@nicolas-grekas
Copy link
Member

note that the VarDumper component has similar logic that should be patched also

@@ -93,7 +93,16 @@ protected function doWrite($message, $newline)
protected function hasColorSupport()
{
if (DIRECTORY_SEPARATOR === '\\') {
return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM');
return
defined('PHP_WINDOWS_VERSION_MAJOR')
Copy link
Member

Choose a reason for hiding this comment

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

AFAIK, PHP_WINDOWS_VERSION_MAJOR goves the version of Windows where PHP was compiled, not the version where it runs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAIK, PHP_WINDOWS_VERSION_MAJOR goves the version of Windows where PHP was compiled, not the version where it runs

@stof Whoops, you're right!

@stof
Copy link
Member

stof commented Mar 31, 2016

Do you you consider a bug not having colors on Windows?

not a bugfix IMO (especially given that we can already force it with a CLI flag)

@mlocati mlocati closed this Apr 1, 2016
@mlocati mlocati deleted the use-cli-colors-on-win10 branch April 1, 2016 06:17
@mlocati
Copy link
Contributor Author

mlocati commented Apr 1, 2016

AFAIK, PHP_WINDOWS_VERSION_MAJOR goves the version of Windows where PHP was compiled, not the version where it runs

No, it's not true. They are determined at runtime.

I diggered a bit into these PHP_WINDOWS_VERSION_... constants, and the results are quite interesting.

I tested exactly the same PHP executables under these different PCs:

  • Windows 7
    version 6.1 32 bit (build 7601 SP 1)
    output of standard ver command in Windows Command Prompt: 6.1.7601
  • Windows 8
    version 6.2 32 bit (build 9200)
    output of standard ver command in Windows Command Prompt: 6.2.9200
  • Windows 8.1
    version 6.3 32 bit (build 9600)
    output of standard ver command in Windows Command Prompt: 6.3.9600
  • Windows 10
    version 1511 64 bit (build 10586.164)
    output of standard ver command in Windows Command Prompt: 10.0.10586

And here's the values of the three constants (concatenated with dots for simplicity) for every PHP version and every PC:

PHP Version Windows 7 Windows 8 Windows 8.1 Windows 10
5.3.0 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.1 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.2 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.3 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.4 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.5 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.6 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.7 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.8 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.9 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.10 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.11 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.12 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.13 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.14 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.15 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.16 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.17 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.18 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.19 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.20 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.21 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.22 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.23 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.24 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.25 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.26 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.27 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.28 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.3.29 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.0 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.1 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.2 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.3 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.4 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.5 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.6 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.7 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.8 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.9 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.10 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.11 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.12 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.13 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.14 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.15 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.16 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.17 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.18 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.19 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.20 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.21 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.22 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.23 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.24 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.25 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.26 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.27 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.28 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.29 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.30 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.31 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.32 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.33 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.34 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.35 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.36 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.37 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.38 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.39 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.40 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.41 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.42 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.43 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.44 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.4.45 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.0 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.1 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.2 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.3 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.4 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.5 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.6 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.7 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.8 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.9 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.10 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.11 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.12 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.13 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.14 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.15 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.16 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.17 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.18 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.19 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.20 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.21 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.22 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.23 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.24 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.25 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.26 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.5.27 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.5.28 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.5.29 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.5.30 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.5.31 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.5.32 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.5.33 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.5.34 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.0 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.1 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.2 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.3 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.4 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.5 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.6 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.7 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.8 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.9 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.10 6.1.7601 6.2.9200 6.2.9200 6.2.9200
5.6.11 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.12 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.13 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.14 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.15 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.16 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.17 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.18 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.19 6.1.7601 6.2.9200 6.3.9600 10.0.10586
5.6.20 6.1.7601 6.2.9200 6.3.9600 10.0.10586
7.0.0 6.1.7601 6.2.9200 6.3.9600 10.0.10586
7.0.1 6.1.7601 6.2.9200 6.3.9600 10.0.10586
7.0.2 6.1.7601 6.2.9200 6.3.9600 10.0.10586
7.0.3 6.1.7601 6.2.9200 6.3.9600 10.0.10586
7.0.4 6.1.7601 6.2.9200 6.3.9600 10.0.10586
7.0.5 6.1.7601 6.2.9200 6.3.9600 10.0.10586

For these versions:

  • PHP 5.4
  • PHP 5.5 up to 5.5.26
  • PHP 5.6 up to 5.6.10

the detected Windows version is wrong because of this Windows change.

These versions:

  • PHP 5.5 from 5.5.27
  • PHP 5.6 from 5.6.11
  • PHP 7.0

have this patch, so those versions have a reliable value for the constants.

Given all that, I think that this pull request is still valid, but it will work only for PHP 5.5 >= 5.5.27, PHP 5.6 >= 5.6.11 and PHP 7+ (older versions will continue to work as before).

So, @stof, shall I reopen this PR?

@mlocati
Copy link
Contributor Author

mlocati commented Apr 1, 2016

PS: a more reliable way to determine the exact Windows version would be to use exec('ver'), but:

  • we should check if exec can be used (like here)
  • the output of ver is localized, so we'd need to use a regex to catch its result

@stof
Copy link
Member

stof commented Apr 1, 2016

it is fine if the autodetection works fine only in uptodate PHP versions. Not detecting color support won't break the console (it will just fallback to non colored output by default)

@mlocati mlocati restored the use-cli-colors-on-win10 branch April 1, 2016 14:50
@mlocati mlocati reopened this Apr 1, 2016
@mlocati
Copy link
Contributor Author

mlocati commented Apr 1, 2016

I updated this PR, adding the Windows 10 detection code to DeprecationErrorHandler and CliDumper too

@@ -194,7 +194,16 @@ public static function register($mode = 0)
private static function hasColorSupport()
{
if ('\\' === DIRECTORY_SEPARATOR) {
return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM');
return
defined('PHP_WINDOWS_VERSION_MAJOR')
Copy link
Member

Choose a reason for hiding this comment

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

is this part actually needed ? We already detect windows just before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We just detected that the directory separator in \... I'm not 100% sure it's only used in Windows (there are more OSs out there than just *nix, Windows and Mac... What about OS2? Others?....)

Copy link
Member

Choose a reason for hiding this comment

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

I think we can safely state that only Windows does '\\' === DIRECTORY_SEPARATOR

@nicolas-grekas
Copy link
Member

👍, as a bug fix on 2.3 to me...

@fabpot
Copy link
Member

fabpot commented Apr 3, 2016

I don't think it qualifies as a bug fix (and this PR does not apply cleanly on 2.3 as two files did not exist back then). For such a PR, I propose to merge it on master and be done with it.

@nicolas-grekas
Copy link
Member

Thank you @mlocati.

nicolas-grekas added a commit that referenced this pull request Apr 4, 2016
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #18385).

Discussion
----------

Detect CLI color support for Windows 10 build 10586

Newer Windows 10 versions (builds starting from 10586) offer VT100 color support.
See http://www.nivot.org/blog/post/2016/02/04/Windows-10-TH2-(v1511)-Console-Host-Enhancements

| Q             | A
| ------------- | ---
| Branch?       | master - Maybe it could be backported to other branches too
| Bug fix?      | maybe 😉 - Do you you consider a bug not having colors on Windows?
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

472a7bf Detect CLI color support for Windows 10 build 10586
@fabpot fabpot mentioned this pull request Apr 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants