From e1d2bc85de11a84d2c984d57661c75059470b7bc Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 28 Nov 2024 15:59:05 -0600 Subject: [PATCH 1/9] Allow disabling colors --- src/Output/Color.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Output/Color.php b/src/Output/Color.php index 876b503..c9e68f9 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -50,6 +50,8 @@ class Color const GRAY = 47; const DARKGRAY = 100; + static bool $colors_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::$colors_enabled) { + return $text; + } + $style += ['bg' => null, 'fg' => static::WHITE, 'bold' => 0, 'mod' => null]; $format = $style['bg'] === null From e484ad6d2d4247ced1da1d3711feb1b7072534a1 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 28 Nov 2024 16:07:01 -0600 Subject: [PATCH 2/9] Make a public static variable --- src/Output/Color.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Output/Color.php b/src/Output/Color.php index c9e68f9..f82a67e 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -50,7 +50,7 @@ class Color const GRAY = 47; const DARKGRAY = 100; - static bool $colors_enabled = true; + public static bool $colors_enabled = true; protected string $format = "\033[:mod:;:fg:;:bg:m:txt:\033[0m"; From d30bd49c5d5f4400c396ef0bbb30d35afecd4105 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 28 Nov 2024 16:11:48 -0600 Subject: [PATCH 3/9] Add documentation about disabling colors to readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 1f40a5b..c2c72d5 100644 --- a/README.md +++ b/README.md @@ -508,6 +508,13 @@ Ahc\Cli\Output\Color::style('error', [ ]); ``` +#### Disable colors + +```php +Ahc\Cli\Output\Color::$colors_enabled = false; +``` + + ### Cursor Move cursor around, erase line up or down, clear screen. From c172fce93c24bec99e74ecb5be8c3077714a59ae Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 28 Nov 2024 16:12:35 -0600 Subject: [PATCH 4/9] Remove empty line --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index c2c72d5..f7302fd 100644 --- a/README.md +++ b/README.md @@ -514,7 +514,6 @@ Ahc\Cli\Output\Color::style('error', [ Ahc\Cli\Output\Color::$colors_enabled = false; ``` - ### Cursor Move cursor around, erase line up or down, clear screen. From e6097dae5446b151de7e51f12d40fd709d9a7cfb Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 28 Nov 2024 16:28:39 -0600 Subject: [PATCH 5/9] Don't worry about missing styles if colors are disabled --- src/Output/Color.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Output/Color.php b/src/Output/Color.php index f82a67e..d1c3211 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -235,7 +235,11 @@ public function __call(string $name, array $arguments): string } if (!method_exists($this, $name)) { - throw new InvalidArgumentException(sprintf('Style "%s" not defined', $name)); + if (self::$colors_enabled) { + throw new InvalidArgumentException(sprintf('Style "%s" not defined', $name)); + } + + return $text; } return $this->{$name}($text, $style); From d7dd0ecc5abbc890401595eb440734b136c0e895 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 28 Nov 2024 16:30:32 -0600 Subject: [PATCH 6/9] Short-circut the missing method the same way we do line function --- src/Output/Color.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Output/Color.php b/src/Output/Color.php index d1c3211..f126ccc 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -235,11 +235,11 @@ public function __call(string $name, array $arguments): string } if (!method_exists($this, $name)) { - if (self::$colors_enabled) { - throw new InvalidArgumentException(sprintf('Style "%s" not defined', $name)); + if (!self::$colors_enabled) { + return $text; } - return $text; + throw new InvalidArgumentException(sprintf('Style "%s" not defined', $name)); } return $this->{$name}($text, $style); From dbd3078a0d956b1e1d5b3f1720005ee665784e14 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Thu, 28 Nov 2024 16:35:53 -0600 Subject: [PATCH 7/9] Rename Color's variable to just as we do camelcase here --- README.md | 2 +- src/Output/Color.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f7302fd..3ef2db9 100644 --- a/README.md +++ b/README.md @@ -511,7 +511,7 @@ Ahc\Cli\Output\Color::style('error', [ #### Disable colors ```php -Ahc\Cli\Output\Color::$colors_enabled = false; +Ahc\Cli\Output\Color::$enabled = false; ``` ### Cursor diff --git a/src/Output/Color.php b/src/Output/Color.php index f126ccc..5e1d9c7 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -50,7 +50,7 @@ class Color const GRAY = 47; const DARKGRAY = 100; - public static bool $colors_enabled = true; + public static bool $enabled = true; protected string $format = "\033[:mod:;:fg:;:bg:m:txt:\033[0m"; @@ -140,7 +140,7 @@ public static function fg256(int $code): string */ public function line(string $text, array $style = []): string { - if (!self::$colors_enabled) { + if (!self::$enabled) { return $text; } @@ -235,7 +235,7 @@ public function __call(string $name, array $arguments): string } if (!method_exists($this, $name)) { - if (!self::$colors_enabled) { + if (!self::$enabled) { return $text; } From 4e37cc2c162ab47cb07776f8583e07660cab1257 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Sat, 30 Nov 2024 18:26:41 -0600 Subject: [PATCH 8/9] Check if NO_COLOR is set when writing to the terminal --- src/Output/Color.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Output/Color.php b/src/Output/Color.php index 5e1d9c7..4111038 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -140,7 +140,7 @@ public static function fg256(int $code): string */ public function line(string $text, array $style = []): string { - if (!self::$enabled) { + if (!self::$enabled || getenv('NO_COLOR')) { return $text; } @@ -235,7 +235,7 @@ public function __call(string $name, array $arguments): string } if (!method_exists($this, $name)) { - if (!self::$enabled) { + if (!self::$enabled || getenv('NO_COLOR')) { return $text; } From 06c29073e4c39800cff3c4b4da7661f4636f2d9e Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Sat, 30 Nov 2024 18:32:12 -0600 Subject: [PATCH 9/9] Add info about NO_COLOR environment variable to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3ef2db9..3e1e3ee 100644 --- a/README.md +++ b/README.md @@ -514,6 +514,8 @@ Ahc\Cli\Output\Color::style('error', [ 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.