Skip to content

Use new PHP7.2 functions in hasColorSupport #26910

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

Merged
merged 1 commit into from
Apr 22, 2018

Conversation

johnstevenson
Copy link
Contributor

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Fixes bc break in #26609
Reference: https://github.com/composer/xdebug-handler/blob/master/src/Process.php#L111

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

There are more places that check for color support (grep ConEmuANSI src/ -r)
could you please sync them all?

  • src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
  • src/Symfony/Component/VarDumper/Dumper/CliDumper.php

@johnstevenson
Copy link
Contributor Author

Sure. But I could do with some info regarding CliDumper:

The constructor checks for Windows color support (so it can set a restricted style set), but only uses environment variables and not the output.

  1. I guess it is safe to use $this->outputStream here?
  2. Or would it be better to set the styles within the supportsColors method later?

The supportsColors method does one type of check if $this->outputStream is not the default (is_resource && posix_isatty, which doesn't include windows) and a different, more stringent one if it is. An explanation why would be really useful. Likewise why if it is not the default, the method returns before checking for color options in argv.

I was wondering if the output needs to be differentiated, and if so, then what are the criteria for each type.

@nicolas-grekas
Copy link
Member

Which color set does new versions of Windows support? If it doesn't need the reduced set, then the current logic in the CliDumper constructor should be fine, so that only supportsColors should be updated.

@johnstevenson
Copy link
Contributor Author

Windows 10 has supported 24-bit color for over a year now, but you can obviously still run PHP7.2 on earlier versions.

return function_exists('posix_isatty') && @posix_isatty($this->stream);
if (function_exists('stream_isatty')) {
return @stream_isatty($this->stream);
} elseif (function_exists('posix_isatty')) {
Copy link
Member

Choose a reason for hiding this comment

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

just if, not elseif as the previous case returns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay

@johnstevenson
Copy link
Contributor Author

I've synced DeprecationErrorHandler.php, but without any info about CliDumper.php I have no idea if my changes are correct.

I've kept the current logic in the CliDumper constructor, which is not ideal since it won't show 24-bit color output in cmd.exe on Windows 10. Incidentally, the Windows tests pass for me locally, but not on Appveyor for some reason:

There were 3 failures:
1) Symfony\Component\HttpKernel\Tests\DataCollector\DumpDataCollectorTest::testCollectDefault
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-DumpDataCollectorTest.php on line 65:
-123
+DumpDataCollectorTest.php on line 65:
+123

C:\projects\symfony\src\Symfony\Component\HttpKernel\Tests\DataCollector\DumpDataCollectorTest.php:73

2) Symfony\Component\HttpKernel\Tests\DataCollector\DumpDataCollectorTest::testFlush
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-DumpDataCollectorTest.php on line 123:
-456
+DumpDataCollectorTest.php on line 123:
+456

C:\projects\symfony\src\Symfony\Component\HttpKernel\Tests\DataCollector\DumpDataCollectorTest.php:129

3) Symfony\Component\HttpKernel\Tests\DataCollector\DumpDataCollectorTest::testFlushNothingWhenDataDumperIsProvided
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-DumpDataCollectorTest.php on line 142:
-456
+DumpDataCollectorTest.php on line 142:
+456

C:\projects\symfony\src\Symfony\Component\HttpKernel\Tests\DataCollector

I've also added references to the source of the code, but I'll remove them if not considered desirable.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM, with one minor comment.
About the CliDumper constructor, we have to use 16 colors only on some terminals, which is what we do by default. Do you think we can improve the logic and somehow detect when the native Windows terminal supports 24b colors? That's what missing there.

* Reference: Composer\XdebugHandler\Process::supportsColor
* https://github.com/composer/xdebug-handler
*
* @return bool
Copy link
Member

Choose a reason for hiding this comment

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

this annotation should be removed and replaced by a return-type on the hasColorsSupport (same in other files below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

?? Sorry, you've lost me!

Copy link
Member

@nicolas-grekas nicolas-grekas Apr 16, 2018

Choose a reason for hiding this comment

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

Oups sorry, branch 2.7 doesn't support scalar type hints.

@johnstevenson
Copy link
Contributor Author

Do you think we can improve the logic and somehow detect when the native Windows terminal supports 24b colors?

Probably, but I need you to answer my original question:

The constructor checks for Windows color support (so it can set a restricted style set), but only uses environment variables and not the output.

  1. I guess it is safe to use $this->outputStream here?
  2. Or would it be better to set the styles within the supportsColors method later?

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Apr 16, 2018

It is not safe to use $this->outputStream. The check should be done against a global stream, STDOUT basically.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

(with one minor comment)

*/
private function isWindowsTrueColor()
{
$result = strval(getenv('ANSICON_VER')) >= '183'
Copy link
Member

Choose a reason for hiding this comment

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

thanks to PHP type juggling, this should be simplified to 183 <= getenv('ANSICON_VER')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, that is a much better way of doing it.

Copy link
Member

Choose a reason for hiding this comment

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

fixed in 9234171

@fabpot fabpot force-pushed the fix-output-hascolorsupport branch from 72482c5 to b0c9225 Compare April 22, 2018 05:55
@fabpot
Copy link
Member

fabpot commented Apr 22, 2018

Thank you @johnstevenson.

@fabpot fabpot merged commit b0c9225 into symfony:2.7 Apr 22, 2018
fabpot added a commit that referenced this pull request Apr 22, 2018
This PR was squashed before being merged into the 2.7 branch (closes #26910).

Discussion
----------

Use new PHP7.2 functions in hasColorSupport

| Q             | A
| ------------- | ---
| Branch?       |  2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Fixes bc break in #26609
Reference: https://github.com/composer/xdebug-handler/blob/master/src/Process.php#L111

Commits
-------

b0c9225 Use new PHP7.2 functions in hasColorSupport
@johnstevenson
Copy link
Contributor Author

Thanks for fixing this remaining item, and apologies for not fixing it myself (life/time problem!)

@johnstevenson johnstevenson deleted the fix-output-hascolorsupport branch April 22, 2018 10:41
nicolas-grekas added a commit that referenced this pull request Apr 25, 2018
…ts (chalasr)

This PR was merged into the 2.7 branch.

Discussion
----------

[HttpKernel] Remove decoration from actual output in tests

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes green again
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

AppVeyor has color support since #26910, that breaks the build.
Fixes it by removing decoration from tested DumpDataCollector CLI outputs, same as what's already done for HTML dumps

Commits
-------

c4daef9 [VarDumper] Remove decoration from actual output in tests
This was referenced Apr 27, 2018
@fabpot fabpot mentioned this pull request Apr 30, 2018
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.

5 participants