diff --git a/README.md b/README.md index 4c3dd1c..d5445f1 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 160f4a2..6c1485c 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -52,6 +52,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 */ @@ -140,6 +142,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