Skip to content

Commit cfdaac4

Browse files
committed
Merge branch '5.4' into 6.3
* 5.4: [Console] Only execute additional checks for color support if the output is a TTY fix aircraft inflection [TwigBundle] Fix configuration when 'paths' is null [String] Correct inflection of axis [Security] Fix `AuthenticationUtils::getLastUsername()` returning null [Process] Fixed inconsistent test
2 parents ca73e92 + 8c181cf commit cfdaac4

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

Output/StreamOutput.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ protected function hasColorSupport(): bool
9696
return false;
9797
}
9898

99+
if (!$this->isTty()) {
100+
return false;
101+
}
102+
99103
if (\DIRECTORY_SEPARATOR === '\\'
100104
&& \function_exists('sapi_windows_vt100_support')
101105
&& @sapi_windows_vt100_support($this->stream)
@@ -106,7 +110,36 @@ protected function hasColorSupport(): bool
106110
return 'Hyper' === getenv('TERM_PROGRAM')
107111
|| false !== getenv('ANSICON')
108112
|| 'ON' === getenv('ConEmuANSI')
109-
|| str_starts_with((string) getenv('TERM'), 'xterm')
110-
|| stream_isatty($this->stream);
113+
|| str_starts_with((string) getenv('TERM'), 'xterm');
114+
}
115+
116+
/**
117+
* Checks if the stream is a TTY, i.e; whether the output stream is connected to a terminal.
118+
*
119+
* Reference: Composer\Util\Platform::isTty
120+
* https://github.com/composer/composer
121+
*/
122+
private function isTty(): bool
123+
{
124+
// Detect msysgit/mingw and assume this is a tty because detection
125+
// does not work correctly, see https://github.com/composer/composer/issues/9690
126+
if (\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) {
127+
return true;
128+
}
129+
130+
// Modern cross-platform function, includes the fstat fallback so if it is present we trust it
131+
if (\function_exists('stream_isatty')) {
132+
return stream_isatty($this->stream);
133+
}
134+
135+
// Only trusting this if it is positive, otherwise prefer fstat fallback.
136+
if (\function_exists('posix_isatty') && posix_isatty($this->stream)) {
137+
return true;
138+
}
139+
140+
$stat = @fstat($this->stream);
141+
142+
// Check if formatted mode is S_IFCHR
143+
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
111144
}
112145
}

0 commit comments

Comments
 (0)