Skip to content

Commit

Permalink
Environment: improved color detection
Browse files Browse the repository at this point in the history
- on Windows rely on sapi_windows_vt100_support()
- on Linux rely on stream_isatty()
- respects NO_COLOR and FORCE_COLOR
  • Loading branch information
dg committed Mar 22, 2022
1 parent 937ae82 commit 8e93803
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Framework/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,12 @@ public static function setupColors(): void
self::$useColors = getenv(self::COLORS) !== false
? (bool) getenv(self::COLORS)
: (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')
&& (!function_exists('stream_isatty') || stream_isatty(STDOUT)) // PHP >= 7.2
&& getenv('NO_COLOR') === false
&& (defined('PHP_WINDOWS_VERSION_BUILD')
? (function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(STDOUT))
|| getenv('ConEmuANSI') === 'ON' // ConEmu
|| getenv('ANSICON') !== false // ANSICON
|| getenv('term') === 'xterm' // MSYS
|| getenv('term') === 'xterm-256color' // MSYS
: (!function_exists('posix_isatty') || posix_isatty(STDOUT))); // PHP < 7.2
&& getenv('NO_COLOR') === false // https://no-color.org
&& (getenv('FORCE_COLOR')
|| (function_exists('sapi_windows_vt100_support')
? sapi_windows_vt100_support(STDOUT)
: @stream_isatty(STDOUT)) // @ may trigger error 'cannot cast a filtered stream on this system'
);

ob_start(function (string $s): string {
return self::$useColors ? $s : Dumper::removeColors($s);
Expand Down

0 comments on commit 8e93803

Please sign in to comment.