Skip to content

Commit

Permalink
[VarDumper][PhpUnitBridge] Fix color detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Feb 5, 2024
1 parent 2bfac0d commit 7ce86dc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 53 deletions.
51 changes: 41 additions & 10 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,27 +403,58 @@ private static function hasColorSupport()
return false;
}

if ('Hyper' === getenv('TERM_PROGRAM')) {
if (!self::isTty()) {
return true;
}

if (\DIRECTORY_SEPARATOR === '\\') {
return (\function_exists('sapi_windows_vt100_support')
&& sapi_windows_vt100_support(\STDOUT))
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
|| 'xterm' === getenv('TERM');
if ('\\' === \DIRECTORY_SEPARATOR
&& \function_exists('sapi_windows_vt100_support')
&& @sapi_windows_vt100_support(\STDOUT)
) {
return true;
}

if ('Hyper' === getenv('TERM_PROGRAM')
|| false !== getenv('COLORTERM')
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
) {
return true;
}

if ('dumb' === $term = (string) getenv('TERM')) {
return false;
}

// See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);

Check failure on line 430 in src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidReturnStatement

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php:430:16: InvalidReturnStatement: The inferred type '0|1|false' does not match the declared return type 'bool' for Symfony\Bridge\PhpUnit\DeprecationErrorHandler::hasColorSupport (see https://psalm.dev/128)
}

/**
* 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 static 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(\STDOUT);
}

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

$stat = fstat(\STDOUT);
$stat = @fstat(\STDOUT);

// Check if formatted mode is S_IFCHR
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
Expand Down
14 changes: 1 addition & 13 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,19 +503,7 @@ private function isInteractiveInput($inputStream): bool
return self::$stdinIsInteractive;
}

if (\function_exists('stream_isatty')) {
return self::$stdinIsInteractive = @stream_isatty(fopen('php://stdin', 'r'));
}

if (\function_exists('posix_isatty')) {
return self::$stdinIsInteractive = @posix_isatty(fopen('php://stdin', 'r'));
}

if (!\function_exists('shell_exec')) {
return self::$stdinIsInteractive = true;
}

return self::$stdinIsInteractive = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
return self::$stdinIsInteractive = @stream_isatty(fopen('php://stdin', 'r'));
}

/**
Expand Down
26 changes: 4 additions & 22 deletions src/Symfony/Component/Console/Output/StreamOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ protected function hasColorSupport()
return false;
}

if (\DIRECTORY_SEPARATOR === '\\'
&& \function_exists('sapi_windows_vt100_support')
&& @sapi_windows_vt100_support($this->stream)
) {
if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($this->stream)) {
return true;
}

Expand All @@ -114,14 +111,12 @@ protected function hasColorSupport()
return true;
}

$term = (string) getenv('TERM');

if ('dumb' === $term) {
if ('dumb' === $term = (string) getenv('TERM')) {
return false;
}

// See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
return 1 === @preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
}

/**
Expand All @@ -138,19 +133,6 @@ private function isTty(): bool
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;
return @stream_isatty($this->stream);
}
}
27 changes: 19 additions & 8 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,19 +610,30 @@ private function hasColorSupport($stream): bool
return false;
}

if ('Hyper' === getenv('TERM_PROGRAM')) {
// Detect msysgit/mingw and assume this is a tty because detection
// does not work correctly, see https://github.com/composer/composer/issues/9690
if (!@stream_isatty($stream) && !\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) {
return true;
}

if (\DIRECTORY_SEPARATOR === '\\') {
return (\function_exists('sapi_windows_vt100_support')
&& @sapi_windows_vt100_support($stream))
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
|| 'xterm' === getenv('TERM');
if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($stream)) {
return true;
}

if ('Hyper' === getenv('TERM_PROGRAM')
|| false !== getenv('COLORTERM')
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
) {
return true;
}

if ('dumb' === $term = (string) getenv('TERM')) {
return false;
}

return stream_isatty($stream);
// See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
}

/**
Expand Down

0 comments on commit 7ce86dc

Please sign in to comment.