Skip to content

Commit

Permalink
Merge branch '5.4' into 6.3
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xabbuh committed Jan 19, 2024
2 parents ca73e92 + 8c181cf commit cfdaac4
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions Output/StreamOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ protected function hasColorSupport(): bool
return false;
}

if (!$this->isTty()) {
return false;
}

if (\DIRECTORY_SEPARATOR === '\\'
&& \function_exists('sapi_windows_vt100_support')
&& @sapi_windows_vt100_support($this->stream)
Expand All @@ -106,7 +110,36 @@ protected function hasColorSupport(): bool
return 'Hyper' === getenv('TERM_PROGRAM')
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
|| str_starts_with((string) getenv('TERM'), 'xterm')
|| stream_isatty($this->stream);
|| str_starts_with((string) getenv('TERM'), 'xterm');
}

/**
* 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 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($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;
}
}

0 comments on commit cfdaac4

Please sign in to comment.