diff --git a/README.md b/README.md index 1f40a5b..3e1e3ee 100644 --- a/README.md +++ b/README.md @@ -508,6 +508,14 @@ Ahc\Cli\Output\Color::style('error', [ ]); ``` +#### Disable colors + +```php +Ahc\Cli\Output\Color::$enabled = false; +``` + +Colors will be automatically disabled if the `NO_COLOR` environment variable is set to true. + ### Cursor Move cursor around, erase line up or down, clear screen. diff --git a/src/Output/Color.php b/src/Output/Color.php index 876b503..4111038 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -50,6 +50,8 @@ class Color const GRAY = 47; const DARKGRAY = 100; + public static bool $enabled = true; + protected string $format = "\033[:mod:;:fg:;:bg:m:txt:\033[0m"; /** @var array Custom styles */ @@ -138,6 +140,10 @@ public static function fg256(int $code): string */ public function line(string $text, array $style = []): string { + if (!self::$enabled || getenv('NO_COLOR')) { + return $text; + } + $style += ['bg' => null, 'fg' => static::WHITE, 'bold' => 0, 'mod' => null]; $format = $style['bg'] === null @@ -229,6 +235,10 @@ public function __call(string $name, array $arguments): string } if (!method_exists($this, $name)) { + if (!self::$enabled || getenv('NO_COLOR')) { + return $text; + } + throw new InvalidArgumentException(sprintf('Style "%s" not defined', $name)); }