Skip to content

Commit

Permalink
added Console::detectColors()
Browse files Browse the repository at this point in the history
supports
- NO_COLOR https://no-color.org
- sapi_windows_vt100_support
- stream_isatty
  • Loading branch information
dg committed May 5, 2020
1 parent 66ae4b1 commit 7027cbe
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/CommandLine/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


Expand Down Expand Up @@ -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);
}
}

0 comments on commit 7027cbe

Please sign in to comment.