-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PhpUnitBridge][VarDumper] Fix color detection #53794
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't it be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why would it? preg_match doesn't trigger notices unless the regexp is badly formatted, which it isn't |
||
} | ||
|
||
/** | ||
* 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic in this method does not match the logic in the console component. A stream that is not a TTY must not report supporting colors. |
||
} | ||
|
||
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); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is broken. For not a TTY, it should says
false