diff --git a/src/CommandLine/Console.php b/src/CommandLine/Console.php index 1ec9ccc..b26210c 100644 --- a/src/CommandLine/Console.php +++ b/src/CommandLine/Console.php @@ -21,8 +21,7 @@ class Console public function __construct() { - $this->useColors = PHP_SAPI === 'cli' && ((function_exists('posix_isatty') && posix_isatty(STDOUT)) - || getenv('ConEmuANSI') === 'ON' || getenv('ANSICON') !== false || getenv('term') === 'xterm-256color'); + $this->useColors = self::detectColors(); } @@ -51,4 +50,19 @@ public function color(?string $color, string $s = null): string } return (string) $s; } + + + public static function detectColors(): bool + { + return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') + && stream_isatty(STDOUT) + && getenv('NO_COLOR') === false // https://no-color.org + && (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 + : true); + } }