8000 [VarDumper][PhpUnitBridge] Fix color detection · symfony/symfony@7ce86dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ce86dc

Browse files
[VarDumper][PhpUnitBridge] Fix color detection
1 parent 2bfac0d commit 7ce86dc

File tree

4 files changed

+65
-53
lines changed

4 files changed

+65
-53
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

+41-10
Original file line numberDiff line numberDiff line change
@@ -403,27 +403,58 @@ private static function hasColorSupport()
403403
return false;
404404
}
405405

406-
if ('Hyper' === getenv('TERM_PROGRAM')) {
406+
if (!self::isTty()) {
407407
return true;
408408
}
409409

410-
if (\DIRECTORY_SEPARATOR === '\\') {
411-
return (\function_exists('sapi_windows_vt100_support')
412-
&& sapi_windows_vt100_support(\STDOUT))
413-
|| false !== getenv('ANSICON')
414-
|| 'ON' === getenv('ConEmuANSI')
415-
|| 'xterm' === getenv('TERM');
410+
if ('\\' === \DIRECTORY_SEPARATOR
411+
&& \function_exists('sapi_windows_vt100_support')
412+
&& @sapi_windows_vt100_support(\STDOUT)
413+
) {
414+
return true;
416415
}
417416

417+
if ('Hyper' === getenv('TERM_PROGRAM')
418+
|| false !== getenv('COLORTERM')
419+
|| false !== getenv('ANSICON')
420+
|| 'ON' === getenv('ConEmuANSI')
421+
) {
422+
return true;
423+
}
424+
425+
if ('dumb' === $term = (string) getenv('TERM')) {
426+
return false;
427+
}
428+
429+
// See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
430+
return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
431+
}
432+
433+
/**
434+
* Checks if the stream is a TTY, i.e; whether the output stream is connected to a terminal.
435+
*
436+
* Reference: Composer\Util\Platform::isTty
437+
* https://github.com/composer/composer
438+
*/
439+
private static function isTty(): bool
440+
{
441+
// Detect msysgit/mingw and assume this is a tty because detection
442+
// does not work correctly, see https://github.com/composer/composer/issues/9690
443+
if (\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) {
444+
return true;
445+
}
446+
447+
// Modern cross-platform function, includes the fstat fallback so if it is present we trust it
418448
if (\function_exists('stream_isatty')) {
419449
return @stream_isatty(\STDOUT);
420450
}
421451

422-
if (\function_exists('posix_isatty')) {
423-
return @posix_isatty(\STDOUT);
452+
// Only trusting this if it is positive, otherwise prefer fstat fallback.
453+
if (\function_exists('posix_isatty') && @posix_isatty(\STDOUT)) {
454+
return true;
424455
}
425456

426-
$stat = fstat(\STDOUT);
457+
$stat = @fstat(\STDOUT);
427458

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

src/Symfony/Component/Console/Helper/QuestionHelper.php

+1-13
Original file line numberDiff line numberDiff line change
@@ -503,19 +503,7 @@ private function isInteractiveInput($inputStream): bool
503503
return self::$stdinIsInteractive;
504504
}
505505

506-
if (\function_exists('stream_isatty')) {
507-
return self::$stdinIsInteractive = @stream_isatty(fopen('php://stdin', 'r'));
508-
}
509-
510-
if (\function_exists('posix_isatty')) {
511-
return self::$stdinIsInteractive = @posix_isatty(fopen('php://stdin', 'r'));
512-
}
513-
514-
if (!\function_exists('shell_exec')) {
515-
return self::$stdinIsInteractive = true;
516-
}
517-
518-
return self::$stdinIsInteractive = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
506+
return self::$stdinIsInteractive = @stream_isatty(fopen('php://stdin', 'r'));
519507
}
520508

521509
/**

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

+4-22
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ protected function hasColorSupport()
9999
return false;
100100
}
101101

102-
if (\DIRECTORY_SEPARATOR === '\\'
103-
&& \function_exists('sapi_windows_vt100_support')
104-
&& @sapi_windows_vt100_support($this->stream)
105-
) {
102+
if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($this->stream)) {
106103
return true;
107104
}
108105

@@ -114,14 +111,12 @@ protected function hasColorSupport()
114111
return true;
115112
}
116113

117-
$term = (string) getenv('TERM');
118-
119-
if ('dumb' === $term) {
114+
if ('dumb' === $term = (string) getenv('TERM')) {
120115
return false;
121116
}
122117

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

127122
/**
@@ -138,19 +133,6 @@ private function isTty(): bool
138133
return true;
139134
}
140135

141-
// Modern cross-platform function, includes the fstat fallback so if it is present we trust it
142-
if (\function_exists('stream_isatty')) {
143-
return stream_isatty($this->stream);
144-
}
145-
146-
// Only trusting this if it is positive, otherwise prefer fstat fallback.
147-
if (\function_exists('posix_isatty') && posix_isatty($this->stream)) {
148-
return true;
149-
}
150-
151-
$stat = @fstat($this->stream);
152-
153-
// Check if formatted mode is S_IFCHR
154-
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
136+
return @stream_isatty($this->stream);
155137
}
156138
}

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

+19-8
Original file line numberDiff line numberDiff line change
@@ -610,19 +610,30 @@ private function hasColorSupport($stream): bool
610610
return false;
611611
}
612612

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

617-
if (\DIRECTORY_SEPARATOR === '\\') {
618-
return (\function_exists('sapi_windows_vt100_support')
619-
&& @sapi_windows_vt100_support($stream))
620-
|| false !== getenv('ANSICON')
621-
|| 'ON' === getenv('ConEmuANSI')
622-
|| 'xterm' === getenv('TERM');
619+
if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($stream)) {
620+
return true;
621+
}
622+
623+
if ('Hyper' === getenv('TERM_PROGRAM')
624+
|| false !== getenv('COLORTERM')
625+
|| false !== getenv('ANSICON')
626+
|| 'ON' === getenv('ConEmuANSI')
627+
) {
628+
return true;
629+
}
630+
631+
if ('dumb' === $term = (string) getenv('TERM')) {
632+
return false;
623633
}
624634

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

628639
/**

0 commit comments

Comments
 (0)
0