8000 bug #26609 [Console] Fix check of color support on Windows (mlocati) · symfony/symfony@d73f491 · GitHub
[go: up one dir, main page]

Skip to content

Commit d73f491

Browse files
committed
bug #26609 [Console] Fix check of color support on Windows (mlocati)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix check of color support on Windows | 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 | If the stream is redirected, `StreamOutput::hasColorSupport()` returns `false` on POSIX systems. On Windows, this is not always true. Before PHP 7.2 we can't say if the stream is redirected, but since PHP 7.2 we have the `stream_isatty` function that works on Windows too: let's use it. Sure, `sapi_windows_vt100_support` should return `false` if the stream is redirected, but it's in `or` with the other conditions, so the logic was flawed. Commits ------- f7f8189 Fix check of color support on Windows
2 parents 3758a3b + f7f8189 commit d73f491

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Symfony/Component/Console/Output/StreamOutput.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,26 @@ protected function doWrite($message, $newline)
8181
*
8282
* Colorization is disabled if not supported by the stream:
8383
*
84-
* - Windows != 10.0.10586 without Ansicon, ConEmu or Mintty
84+
* - the stream is redirected (eg php file.php >log)
85+
* - Windows without VT100 support, Ansicon, ConEmu, Mintty
8586
* - non tty consoles
8687
*
8788
* @return bool true if the stream supports colorization, false otherwise
8889
*/
8990
protected function hasColorSupport()
9091
{
92+
if (function_exists('stream_isatty') && !@stream_isatty($this->stream)) {
93+
return false;
94+
}
9195
if (DIRECTORY_SEPARATOR === '\\') {
96+
if (function_exists('sapi_windows_vt100_support')) {
97+
$vt100Enabled = @sapi_windows_vt100_support($this->stream);
98+
} else {
99+
$vt100Enabled = '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD;
100+
}
101+
92102
return
93-
function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support($this->stream)
94-
|| '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
103+
$vt100Enabled
95104
|| false !== getenv('ANSICON')
96105
|| 'ON' === getenv('ConEmuANSI')
97106
|| 'xterm' === getenv('TERM');

0 commit comments

Comments
 (0)
0