From 286b9c97f6b2347625b2060687f34d27cbe29433 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 5 Feb 2024 17:03:04 +0100 Subject: [PATCH 1/2] [VarDumper][PhpUnitBridge] Fix color detection --- DeprecationErrorHandler.php | 51 +++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/DeprecationErrorHandler.php b/DeprecationErrorHandler.php index 4f93acd..e5396dd 100644 --- a/DeprecationErrorHandler.php +++ b/DeprecationErrorHandler.php @@ -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); + } + + /** + * 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; From c4d978502e03cc6a45d8454e06e59ff22d4907d5 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 8 Feb 2024 08:18:31 +0100 Subject: [PATCH 2/2] [PhpUnitBridge][VarDumper] fix color detection --- DeprecationErrorHandler.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/DeprecationErrorHandler.php b/DeprecationErrorHandler.php index e5396dd..adddfe6 100644 --- a/DeprecationErrorHandler.php +++ b/DeprecationErrorHandler.php @@ -404,13 +404,10 @@ private static function hasColorSupport() } if (!self::isTty()) { - return true; + return false; } - if ('\\' === \DIRECTORY_SEPARATOR - && \function_exists('sapi_windows_vt100_support') - && @sapi_windows_vt100_support(\STDOUT) - ) { + if ('\\' === \DIRECTORY_SEPARATOR && \function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support(\STDOUT)) { return true; }