Skip to content

Commit

Permalink
[Console] Only execute additional checks for color support if the output
Browse files Browse the repository at this point in the history
is a TTY
  • Loading branch information
theofidry committed Jan 19, 2024
1 parent 766676f commit 2e96b22
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/Symfony/Component/Console/Output/StreamOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ protected function hasColorSupport()
return false;
}

if (!$this->isTty()) {
return false;
}

if (\DIRECTORY_SEPARATOR === '\\'
&& \function_exists('sapi_windows_vt100_support')
&& @sapi_windows_vt100_support($this->stream)
Expand All @@ -105,7 +109,36 @@ protected function hasColorSupport()
return 'Hyper' === getenv('TERM_PROGRAM')
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
|| str_starts_with((string) getenv('TERM'), 'xterm')
|| stream_isatty($this->stream);
|| str_starts_with((string) getenv('TERM'), 'xterm');
}

/**
* Checks if the stream is a TTY, i.e; whether the output stream is connected to a terminal.
*
* Reference: Composer\Util\Platform::isTty
* https://github.com/composer/composer
*/
private function isTty(): bool
{
// Detect msysgit/mingw and assume this is a tty because detection
// does not work correctly, see https://github.com/composer/composer/issues/9690
if (\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) {
return true;
}

// Modern cross-platform function, includes the fstat fallback so if it is present we trust it
if (\function_exists('stream_isatty')) {
return stream_isatty($this->stream);
}

// Only trusting this if it is positive, otherwise prefer fstat fallback.
if (\function_exists('posix_isatty') && posix_isatty($this->stream)) {
return true;
}

$stat = @fstat($this->stream);

// Check if formatted mode is S_IFCHR
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
}
}

0 comments on commit 2e96b22

Please sign in to comment.