From c354406c865a25db0113f6c568e6e7a79116deaf Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 10:42:06 -0600 Subject: [PATCH 01/39] Add support for 256 colors --- README.md | 2 +- src/Output/Color.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4976efc..12449d9 100644 --- a/README.md +++ b/README.md @@ -443,7 +443,7 @@ echo $color->ok('This is ok msg'); ```php Ahc\Cli\Output\Color::style('mystyle', [ 'bg' => Ahc\Cli\Output\Color::CYAN, - 'fg' => Ahc\Cli\Output\Color::WHITE, + 'fg' => '38;5;57', // 256 colors can be used as well 'bold' => 1, // You can experiment with 0, 1, 2, 3 ... as well ]); diff --git a/src/Output/Color.php b/src/Output/Color.php index d2be69a..11f606e 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -108,8 +108,8 @@ public function line(string $text, array $style = []): string $line = strtr($format, [ ':mod:' => (int) ($style['mod'] ?? $style['bold']), - ':fg:' => (int) $style['fg'], - ':bg:' => (int) $style['bg'] + 10, + ':fg:' => $style['fg'], + ':bg:' => is_int($style['bg']) ? ($style['bg'] + 10) : $style['bg'], ':txt:' => $text, ]); From e59800f58625281e9e2a9b016fde91ba443a1e69 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 10:42:37 -0600 Subject: [PATCH 02/39] Add documentation about default command functionality --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 12449d9..c1c6f42 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,21 @@ $app->add((new ConfigListCommand)->inGroup('Config')); ... ``` +#### Default command + +By default, running your CLI app without any arguments will show the help screen. However you can set the default action to run one of your commands either by setting the third parameter of the `add` function to `true` or by using the `defaultCommand` function. + +```php +$app->add(new InitCommand, 'i', true); + +# Alternatively +$app->command('init', 'Init something', 'i'); +$app->defaultCommand('init'); + +# Retrieve the name of the default command +$default_command = $app->getDefaultCommand(); +``` + #### Exception handler Set a custom exception handler as callback. The callback receives exception & exit code. The callback may rethrow exception or may exit the program or just log exception and do nothing else. From 57b0ddc35d9a1d7a723dfad1a4e98f082e0fb31e Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 10:52:06 -0600 Subject: [PATCH 03/39] Use 256 colors in test_custom_style test --- tests/Output/ColorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Output/ColorTest.php b/tests/Output/ColorTest.php index 607beda..92ed0ce 100644 --- a/tests/Output/ColorTest.php +++ b/tests/Output/ColorTest.php @@ -30,9 +30,9 @@ public function test_comment() public function test_custom_style() { - Color::style('alert', ['bg' => Color::YELLOW, 'fg' => Color::RED, 'bold' => 1]); + Color::style('alert', ['bg' => '48;5;82', 'fg' => '38;5;57', 'bold' => 1]); - $this->assertSame("\033[1;31;43malert\033[0m", (new Color)->alert('alert')); + $this->assertSame("\033[1;38;5;57;48;5;82malert\033[0m", (new Color)->alert('alert')); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Trying to define existing style'); From 30992868f6b9782f07356e98b4ba0c6f48546f41 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:35:10 -0600 Subject: [PATCH 04/39] Add Color::bg256 and Color::fg256 --- README.md | 2 +- src/Output/Color.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c1c6f42..b690c09 100644 --- a/README.md +++ b/README.md @@ -458,7 +458,7 @@ echo $color->ok('This is ok msg'); ```php Ahc\Cli\Output\Color::style('mystyle', [ 'bg' => Ahc\Cli\Output\Color::CYAN, - 'fg' => '38;5;57', // 256 colors can be used as well + 'fg' => hc\Cli\Output\Color::fg256(57), // 256 colors can be used as well 'bold' => 1, // You can experiment with 0, 1, 2, 3 ... as well ]); diff --git a/src/Output/Color.php b/src/Output/Color.php index 11f606e..ae2feb3 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -95,6 +95,22 @@ public function info(string $text, array $style = []): string return $this->line($text, ['fg' => static::BLUE] + $style); } + /** + * Returns the color code for a 256 background color + */ + public function bg256(int $code) + { + return "48;5;{$code}"; + } + + /** + * Returns the color code for a 256 foreground color + */ + public function fg256(int $code) + { + return "38;5;{$code}"; + } + /** * Returns a formatted/colored line. */ From d9c947c02f2e27682d9ad25c83709d5d3eee37f4 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:38:31 -0600 Subject: [PATCH 05/39] Add periods to bg256 and fg256 descriptions --- 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 ae2feb3..2c5a763 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -96,7 +96,7 @@ public function info(string $text, array $style = []): string } /** - * Returns the color code for a 256 background color + * Returns the color code for a 256 background color. */ public function bg256(int $code) { @@ -104,7 +104,7 @@ public function bg256(int $code) } /** - * Returns the color code for a 256 foreground color + * Returns the color code for a 256 foreground color. */ public function fg256(int $code) { From d69720ed96bf43cfc61a09ae3e0c7444d806b024 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:40:26 -0600 Subject: [PATCH 06/39] Add mising A to fg256 example in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b690c09..041e532 100644 --- a/README.md +++ b/README.md @@ -458,7 +458,7 @@ echo $color->ok('This is ok msg'); ```php Ahc\Cli\Output\Color::style('mystyle', [ 'bg' => Ahc\Cli\Output\Color::CYAN, - 'fg' => hc\Cli\Output\Color::fg256(57), // 256 colors can be used as well + 'fg' => Ahc\Cli\Output\Color::fg256(57), // 256 colors can be used as well 'bold' => 1, // You can experiment with 0, 1, 2, 3 ... as well ]); From 9d758dcf35a033611476abafb8c19341208739ed Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:41:57 -0600 Subject: [PATCH 07/39] Add return types for bg256 and fg256 functions --- 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 2c5a763..3d98424 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -98,7 +98,7 @@ public function info(string $text, array $style = []): string /** * Returns the color code for a 256 background color. */ - public function bg256(int $code) + public function bg256(int $code): string { return "48;5;{$code}"; } @@ -106,7 +106,7 @@ public function bg256(int $code) /** * Returns the color code for a 256 foreground color. */ - public function fg256(int $code) + public function fg256(int $code): string { return "38;5;{$code}"; } From 6e08fdb72077b38b0757f8fdb09044dbd649169e Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:55:03 -0600 Subject: [PATCH 08/39] Make bg256 and fg256 functions static --- 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 3d98424..49b9805 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -98,7 +98,7 @@ public function info(string $text, array $style = []): string /** * Returns the color code for a 256 background color. */ - public function bg256(int $code): string + public static function bg256(int $code): string { return "48;5;{$code}"; } @@ -106,7 +106,7 @@ public function bg256(int $code): string /** * Returns the color code for a 256 foreground color. */ - public function fg256(int $code): string + public static function fg256(int $code): string { return "38;5;{$code}"; } From 7bca2a3362a1cec9f94d039df5a46566a208bc1a Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 22:22:08 -0600 Subject: [PATCH 09/39] Use PHP comments in Default command example in readme instead of Bash comments --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 041e532..7dbd88c 100644 --- a/README.md +++ b/README.md @@ -274,11 +274,11 @@ By default, running your CLI app without any arguments will show the help screen ```php $app->add(new InitCommand, 'i', true); -# Alternatively +// Alternatively $app->command('init', 'Init something', 'i'); $app->defaultCommand('init'); -# Retrieve the name of the default command +// Retrieve the name of the default command $default_command = $app->getDefaultCommand(); ``` From a6d6be19bc942c3329effc2c99ffa266a75af92f Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 15:07:48 -0600 Subject: [PATCH 10/39] Make all written text built-in styles so that their styles can be customized --- README.md | 32 ++++++++ src/Helper/OutputHelper.php | 18 ++--- src/IO/Interactor.php | 153 ++---------------------------------- src/Input/Command.php | 8 +- src/Output/Color.php | 71 ++++++----------- tests/Output/ColorTest.php | 11 +-- 6 files changed, 81 insertions(+), 212 deletions(-) diff --git a/README.md b/README.md index 7dbd88c..d97d571 100644 --- a/README.md +++ b/README.md @@ -465,6 +465,38 @@ Ahc\Cli\Output\Color::style('mystyle', [ echo $color->mystyle('My text'); ``` +#### Built-in styles + +There are a number of pre-defined built-in styles that allows you granular customization to different output conditions such as help and prompts: + + - answer + - choice + - comment + - error + - help_category + - help_description + - help_example + - help_footer + - help_group + - help_header + - help_item + - help_summary + - help_text + - info + - ok + - question + - version + - warn + +Overriding a built-in style works the same way as defining a new style: + +```php +Ahc\Cli\Output\Color::style('error', [ + 'fg' => Ahc\Cli\Output\Color::RED, + 'bold' => 1, +]); +``` + ### Cursor Move cursor around, erase line up or down, clear screen. diff --git a/src/Helper/OutputHelper.php b/src/Helper/OutputHelper.php index 7cf9c5c..df518c8 100644 --- a/src/Helper/OutputHelper.php +++ b/src/Helper/OutputHelper.php @@ -182,13 +182,13 @@ public function showCommandsHelp(array $commands, string $header = '', string $f protected function showHelp(string $for, array $items, string $header = '', string $footer = ''): void { if ($header) { - $this->writer->bold($header, true); + $this->writer->help_header($header, true); } - $this->writer->eol()->boldGreen($for . ':', true); + $this->writer->eol()->help_category($for . ':', true); if (empty($items)) { - $this->writer->bold(' (n/a)', true); + $this->writer->help_text(' (n/a)', true); return; } @@ -200,17 +200,17 @@ protected function showHelp(string $for, array $items, string $header = '', stri foreach ($this->sortItems($items, $padLen, $for) as $item) { $name = $this->getName($item); if ($for === 'Commands' && $lastGroup !== $group = $item->group()) { - $this->writer->boldYellow($group ?: '*', true); + $this->writer->help_group($group ?: '*', true); $lastGroup = $group; } $desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc($withDefault)); - $this->writer->bold(' ' . str_pad($name, $padLen + $space)); - $this->writer->comment($desc, true); + $this->writer->help_item(' ' . str_pad($name, $padLen + $space)); + $this->writer->help_description($desc, true); } if ($footer) { - $this->writer->eol()->yellow($footer, true); + $this->writer->eol()->help_footer($footer, true); } } @@ -224,7 +224,7 @@ public function showUsage(string $usage): self $usage = str_replace('$0', $_SERVER['argv'][0] ?? '[cmd]', $usage); if (!str_contains($usage, ' ## ')) { - $this->writer->eol()->boldGreen('Usage Examples:', true)->colors($usage)->eol(); + $this->writer->eol()->help_category('Usage Examples:', true)->colors($usage)->eol(); return $this; } @@ -241,7 +241,7 @@ public function showUsage(string $usage): self return str_pad('# ', $maxlen - array_shift($lines), ' ', STR_PAD_LEFT); }, $usage); - $this->writer->eol()->boldGreen('Usage Examples:', true)->colors($usage)->eol(); + $this->writer->eol()->help_category('Usage Examples:', true)->colors($usage)->eol(); return $this; } diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index 37c5a78..e711097 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -37,145 +37,6 @@ * @license MIT * * @link https://github.com/adhocore/cli - * - * @method Writer bgBlack($text, $eol = false) - * @method Writer bgBlue($text, $eol = false) - * @method Writer bgCyan($text, $eol = false) - * @method Writer bgGreen($text, $eol = false) - * @method Writer bgPurple($text, $eol = false) - * @method Writer bgRed($text, $eol = false) - * @method Writer bgWhite($text, $eol = false) - * @method Writer bgYellow($text, $eol = false) - * @method Writer black($text, $eol = false) - * @method Writer blackBgBlue($text, $eol = false) - * @method Writer blackBgCyan($text, $eol = false) - * @method Writer blackBgGreen($text, $eol = false) - * @method Writer blackBgPurple($text, $eol = false) - * @method Writer blackBgRed($text, $eol = false) - * @method Writer blackBgWhite($text, $eol = false) - * @method Writer blackBgYellow($text, $eol = false) - * @method Writer blue($text, $eol = false) - * @method Writer blueBgBlack($text, $eol = false) - * @method Writer blueBgCyan($text, $eol = false) - * @method Writer blueBgGreen($text, $eol = false) - * @method Writer blueBgPurple($text, $eol = false) - * @method Writer blueBgRed($text, $eol = false) - * @method Writer blueBgWhite($text, $eol = false) - * @method Writer blueBgYellow($text, $eol = false) - * @method Writer bold($text, $eol = false) - * @method Writer boldBlack($text, $eol = false) - * @method Writer boldBlackBgBlue($text, $eol = false) - * @method Writer boldBlackBgCyan($text, $eol = false) - * @method Writer boldBlackBgGreen($text, $eol = false) - * @method Writer boldBlackBgPurple($text, $eol = false) - * @method Writer boldBlackBgRed($text, $eol = false) - * @method Writer boldBlackBgWhite($text, $eol = false) - * @method Writer boldBlackBgYellow($text, $eol = false) - * @method Writer boldBlue($text, $eol = false) - * @method Writer boldBlueBgBlack($text, $eol = false) - * @method Writer boldBlueBgCyan($text, $eol = false) - * @method Writer boldBlueBgGreen($text, $eol = false) - * @method Writer boldBlueBgPurple($text, $eol = false) - * @method Writer boldBlueBgRed($text, $eol = false) - * @method Writer boldBlueBgWhite($text, $eol = false) - * @method Writer boldBlueBgYellow($text, $eol = false) - * @method Writer boldCyan($text, $eol = false) - * @method Writer boldCyanBgBlack($text, $eol = false) - * @method Writer boldCyanBgBlue($text, $eol = false) - * @method Writer boldCyanBgGreen($text, $eol = false) - * @method Writer boldCyanBgPurple($text, $eol = false) - * @method Writer boldCyanBgRed($text, $eol = false) - * @method Writer boldCyanBgWhite($text, $eol = false) - * @method Writer boldCyanBgYellow($text, $eol = false) - * @method Writer boldGreen($text, $eol = false) - * @method Writer boldGreenBgBlack($text, $eol = false) - * @method Writer boldGreenBgBlue($text, $eol = false) - * @method Writer boldGreenBgCyan($text, $eol = false) - * @method Writer boldGreenBgPurple($text, $eol = false) - * @method Writer boldGreenBgRed($text, $eol = false) - * @method Writer boldGreenBgWhite($text, $eol = false) - * @method Writer boldGreenBgYellow($text, $eol = false) - * @method Writer boldPurple($text, $eol = false) - * @method Writer boldPurpleBgBlack($text, $eol = false) - * @method Writer boldPurpleBgBlue($text, $eol = false) - * @method Writer boldPurpleBgCyan($text, $eol = false) - * @method Writer boldPurpleBgGreen($text, $eol = false) - * @method Writer boldPurpleBgRed($text, $eol = false) - * @method Writer boldPurpleBgWhite($text, $eol = false) - * @method Writer boldPurpleBgYellow($text, $eol = false) - * @method Writer boldRed($text, $eol = false) - * @method Writer boldRedBgBlack($text, $eol = false) - * @method Writer boldRedBgBlue($text, $eol = false) - * @method Writer boldRedBgCyan($text, $eol = false) - * @method Writer boldRedBgGreen($text, $eol = false) - * @method Writer boldRedBgPurple($text, $eol = false) - * @method Writer boldRedBgWhite($text, $eol = false) - * @method Writer boldRedBgYellow($text, $eol = false) - * @method Writer boldWhite($text, $eol = false) - * @method Writer boldWhiteBgBlack($text, $eol = false) - * @method Writer boldWhiteBgBlue($text, $eol = false) - * @method Writer boldWhiteBgCyan($text, $eol = false) - * @method Writer boldWhiteBgGreen($text, $eol = false) - * @method Writer boldWhiteBgPurple($text, $eol = false) - * @method Writer boldWhiteBgRed($text, $eol = false) - * @method Writer boldWhiteBgYellow($text, $eol = false) - * @method Writer boldYellow($text, $eol = false) - * @method Writer boldYellowBgBlack($text, $eol = false) - * @method Writer boldYellowBgBlue($text, $eol = false) - * @method Writer boldYellowBgCyan($text, $eol = false) - * @method Writer boldYellowBgGreen($text, $eol = false) - * @method Writer boldYellowBgPurple($text, $eol = false) - * @method Writer boldYellowBgRed($text, $eol = false) - * @method Writer boldYellowBgWhite($text, $eol = false) - * @method Writer colors($text) - * @method Writer comment($text, $eol = false) - * @method Writer cyan($text, $eol = false) - * @method Writer cyanBgBlack($text, $eol = false) - * @method Writer cyanBgBlue($text, $eol = false) - * @method Writer cyanBgGreen($text, $eol = false) - * @method Writer cyanBgPurple($text, $eol = false) - * @method Writer cyanBgRed($text, $eol = false) - * @method Writer cyanBgWhite($text, $eol = false) - * @method Writer cyanBgYellow($text, $eol = false) - * @method Writer eol(int $n = 1) - * @method Writer error($text, $eol = false) - * @method Writer green($text, $eol = false) - * @method Writer greenBgBlack($text, $eol = false) - * @method Writer greenBgBlue($text, $eol = false) - * @method Writer greenBgCyan($text, $eol = false) - * @method Writer greenBgPurple($text, $eol = false) - * @method Writer greenBgRed($text, $eol = false) - * @method Writer greenBgWhite($text, $eol = false) - * @method Writer greenBgYellow($text, $eol = false) - * @method Writer info($text, $eol = false) - * @method Writer ok($text, $eol = false) - * @method Writer purple($text, $eol = false) - * @method Writer purpleBgBlack($text, $eol = false) - * @method Writer purpleBgBlue($text, $eol = false) - * @method Writer purpleBgCyan($text, $eol = false) - * @method Writer purpleBgGreen($text, $eol = false) - * @method Writer purpleBgRed($text, $eol = false) - * @method Writer purpleBgWhite($text, $eol = false) - * @method Writer purpleBgYellow($text, $eol = false) - * @method Writer red($text, $eol = false) - * @method Writer redBgBlack($text, $eol = false) - * @method Writer redBgBlue($text, $eol = false) - * @method Writer redBgCyan($text, $eol = false) - * @method Writer redBgGreen($text, $eol = false) - * @method Writer redBgPurple($text, $eol = false) - * @method Writer redBgWhite($text, $eol = false) - * @method Writer redBgYellow($text, $eol = false) - * @method Writer table(array $rows, array $styles = []) - * @method Writer warn($text, $eol = false) - * @method Writer white($text, $eol = false) - * @method Writer yellow($text, $eol = false) - * @method Writer yellowBgBlack($text, $eol = false) - * @method Writer yellowBgBlue($text, $eol = false) - * @method Writer yellowBgCyan($text, $eol = false) - * @method Writer yellowBgGreen($text, $eol = false) - * @method Writer yellowBgPurple($text, $eol = false) - * @method Writer yellowBgRed($text, $eol = false) - * @method Writer yellowBgWhite($text, $eol = false) */ class Interactor { @@ -241,7 +102,7 @@ public function confirm(string $text, string $default = 'y'): bool */ public function choice(string $text, array $choices, $default = null, bool $case = false): mixed { - $this->writer->yellow($text); + $this->writer->question($text); $this->listOptions($choices, $default, false); @@ -262,7 +123,7 @@ public function choice(string $text, array $choices, $default = null, bool $case */ public function choices(string $text, array $choices, $default = null, bool $case = false): mixed { - $this->writer->yellow($text); + $this->writer->question($text); $this->listOptions($choices, $default, true); @@ -300,7 +161,7 @@ public function prompt(string $text, $default = null, ?callable $fn = null, int $hidden = func_get_args()[4] ?? false; $readFn = ['read', 'readHidden'][(int) $hidden]; - $this->writer->yellow($text)->comment(null !== $default ? " [$default]: " : ': '); + $this->writer->question($text)->answer(null !== $default ? " [$default]: " : ': '); try { $input = $this->reader->{$readFn}($default, $fn); @@ -310,7 +171,7 @@ public function prompt(string $text, $default = null, ?callable $fn = null, int } if ($retry > 0 && $input === '') { - $this->writer->bgRed($error, true); + $this->writer->error($error, true); return $this->prompt($text, $default, $fn, $retry - 1, $hidden); } @@ -351,12 +212,12 @@ protected function listOptions(array $choices, $default = null, bool $multi = fa $maxLen = max(array_map('strlen', array_keys($choices))); foreach ($choices as $choice => $desc) { - $this->writer->eol()->cyan(str_pad(" [$choice]", $maxLen + 6))->comment($desc); + $this->writer->eol()->choice(str_pad(" [$choice]", $maxLen + 6))->answer($desc); } $label = $multi ? 'Choices (comma separated)' : 'Choice'; - $this->writer->eol()->yellow($label); + $this->writer->eol()->question($label); return $this->promptOptions(array_keys($choices), $default); } @@ -369,7 +230,7 @@ protected function promptOptions(array $choices, mixed $default): self $options = ''; foreach ($choices as $choice) { - $style = in_array($choice, (array) $default) ? 'boldCyan' : 'cyan'; + $style = in_array($choice, (array) $default) ? 'boldChoice' : 'choice'; $options .= "/<$style>$choice"; } diff --git a/src/Input/Command.php b/src/Input/Command.php index 5b999ee..49dbfb4 100644 --- a/src/Input/Command.php +++ b/src/Input/Command.php @@ -298,9 +298,9 @@ public function showHelp(): mixed $io = $this->io(); $helper = new OutputHelper($io->writer()); - $io->bold("Command {$this->_name}, version {$this->_version}", true)->eol(); - $io->comment($this->_desc, true)->eol(); - $io->bold('Usage: ')->yellow("{$this->_name} [OPTIONS...] [ARGUMENTS...]", true); + $io->help_header("Command {$this->_name}, version {$this->_version}", true)->eol(); + $io->help_summary($this->_desc, true)->eol(); + $io->help_text('Usage: ')->help_example("{$this->_name} [OPTIONS...] [ARGUMENTS...]", true); $helper ->showArgumentsHelp($this->allArguments()) @@ -318,7 +318,7 @@ public function showHelp(): mixed */ public function showVersion(): mixed { - $this->writer()->bold($this->_version, true); + $this->writer()->version($this->_version, true); return $this->emit('_exit', 0); } diff --git a/src/Output/Color.php b/src/Output/Color.php index 49b9805..0c8ede1 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -53,47 +53,26 @@ class Color protected string $format = "\033[:mod:;:fg:;:bg:m:txt:\033[0m"; /** @var array Custom styles */ - protected static array $styles = []; - - /** - * Returns a line formatted as comment. - */ - public function comment(string $text, array $style = []): string - { - return $this->line($text, ['mod' => 2] + $style); - } - - /** - * Returns a line formatted as comment. - */ - public function error(string $text, array $style = []): string - { - return $this->line($text, ['fg' => static::RED] + $style); - } - - /** - * Returns a line formatted as ok msg. - */ - public function ok(string $text, array $style = []): string - { - return $this->line($text, ['fg' => static::GREEN] + $style); - } - - /** - * Returns a line formatted as warning. - */ - public function warn(string $text, array $style = []): string - { - return $this->line($text, ['fg' => static::YELLOW] + $style); - } - - /** - * Returns a line formatted as info. - */ - public function info(string $text, array $style = []): string - { - return $this->line($text, ['fg' => static::BLUE] + $style); - } + protected static array $styles = [ + 'answer' => ['fg' => 37, 'mod' => 2], + 'choice' => ['fg' => 36], + 'comment' => ['fg' => 37, 'mod' => 2], + 'error' => ['fg' => 31], + 'help_category' => ['fg' => 32, 'mod' => 1], + 'help_description' => ['fg' => 37, 'mod' => 2], + 'help_example' => ['fg' => 33], + 'help_footer' => ['fg' => 33], + 'help_group' => ['fg' => 33, 'mod' => 1], + 'help_header' => ['fg' => 37, 'mod' => 1], + 'help_item' => ['fg' => 37, 'mod' => 1], + 'help_summary' => ['fg' => 37, 'mod' => 2], + 'help_text' => ['fg' => 37, 'mod' => 1], + 'info' => ['fg' => 34], + 'ok' => ['fg' => 32], + 'question' => ['fg' => 33], + 'version' => ['fg' => 37, 'mod' => 1], + 'warn' => ['fg' => 33], + ]; /** * Returns the color code for a 256 background color. @@ -173,10 +152,6 @@ public static function style(string $name, array $style): void throw new InvalidArgumentException('Trying to set empty or invalid style'); } - if (isset(static::$styles[$name]) || method_exists(static::class, $name)) { - throw new InvalidArgumentException('Trying to define existing style'); - } - static::$styles[$name] = $style; } @@ -197,7 +172,7 @@ public function __call(string $name, array $arguments): string [$name, $text, $style] = $this->parseCall($name, $arguments); if (isset(static::$styles[$name])) { - return $this->line($text, $style + static::$styles[$name]); + return $this->line($text, static::$styles[$name] + $style); } if (defined($color = static::class . '::' . strtoupper($name))) { @@ -228,6 +203,10 @@ protected function parseCall(string $name, array $arguments): array } } + if (isset(static::$styles[strtolower($name)])) { + $name = strtolower($name); + } + if (!preg_match_all('/([b|B|f|F]g)?([A-Z][a-z]+)([^A-Z])?/', $name, $matches)) { return [lcfirst($name) ?: 'line', $text, $style]; } diff --git a/tests/Output/ColorTest.php b/tests/Output/ColorTest.php index 92ed0ce..c7e7cf4 100644 --- a/tests/Output/ColorTest.php +++ b/tests/Output/ColorTest.php @@ -30,14 +30,11 @@ public function test_comment() public function test_custom_style() { - Color::style('alert', ['bg' => '48;5;82', 'fg' => '38;5;57', 'bold' => 1]); + Color::style('alert', ['bg' => '48;5;82', 'fg' => '38;5;57']); - $this->assertSame("\033[1;38;5;57;48;5;82malert\033[0m", (new Color)->alert('alert')); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Trying to define existing style'); - - Color::style('alert', ['bg' => Color::BLACK]); + $this->assertSame("\033[0;38;5;57;48;5;82malert\033[0m", (new Color)->alert('alert')); + $this->assertSame("\033[1;38;5;57;48;5;82malert\033[0m", (new Color)->boldAlert('alert')); + $this->assertSame("\033[1;38;5;57;48;5;82malert\033[0m", (new Color)->alertBold('alert')); } public function test_invalid_custom_style() From 9e32278ff8e10723b6a1844f5b56a73146899ad0 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 16:45:03 -0600 Subject: [PATCH 11/39] Put back Interactor Writer method definitions --- src/IO/Interactor.php | 136 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index e711097..68fbbb6 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -37,6 +37,142 @@ * @license MIT * * @link https://github.com/adhocore/cli + * @method Writer bgBlack($text, $eol = false) + * @method Writer bgBlue($text, $eol = false) + * @method Writer bgCyan($text, $eol = false) + * @method Writer bgGreen($text, $eol = false) + * @method Writer bgPurple($text, $eol = false) + * @method Writer bgRed($text, $eol = false) + * @method Writer bgWhite($text, $eol = false) + * @method Writer bgYellow($text, $eol = false) + * @method Writer black($text, $eol = false) + * @method Writer blackBgBlue($text, $eol = false) + * @method Writer blackBgCyan($text, $eol = false) + * @method Writer blackBgGreen($text, $eol = false) + * @method Writer blackBgPurple($text, $eol = false) + * @method Writer blackBgRed($text, $eol = false) + * @method Writer blackBgWhite($text, $eol = false) + * @method Writer blackBgYellow($text, $eol = false) + * @method Writer blue($text, $eol = false) + * @method Writer blueBgBlack($text, $eol = false) + * @method Writer blueBgCyan($text, $eol = false) + * @method Writer blueBgGreen($text, $eol = false) + * @method Writer blueBgPurple($text, $eol = false) + * @method Writer blueBgRed($text, $eol = false) + * @method Writer blueBgWhite($text, $eol = false) + * @method Writer blueBgYellow($text, $eol = false) + * @method Writer bold($text, $eol = false) + * @method Writer boldBlack($text, $eol = false) + * @method Writer boldBlackBgBlue($text, $eol = false) + * @method Writer boldBlackBgCyan($text, $eol = false) + * @method Writer boldBlackBgGreen($text, $eol = false) + * @method Writer boldBlackBgPurple($text, $eol = false) + * @method Writer boldBlackBgRed($text, $eol = false) + * @method Writer boldBlackBgWhite($text, $eol = false) + * @method Writer boldBlackBgYellow($text, $eol = false) + * @method Writer boldBlue($text, $eol = false) + * @method Writer boldBlueBgBlack($text, $eol = false) + * @method Writer boldBlueBgCyan($text, $eol = false) + * @method Writer boldBlueBgGreen($text, $eol = false) + * @method Writer boldBlueBgPurple($text, $eol = false) + * @method Writer boldBlueBgRed($text, $eol = false) + * @method Writer boldBlueBgWhite($text, $eol = false) + * @method Writer boldBlueBgYellow($text, $eol = false) + * @method Writer boldCyan($text, $eol = false) + * @method Writer boldCyanBgBlack($text, $eol = false) + * @method Writer boldCyanBgBlue($text, $eol = false) + * @method Writer boldCyanBgGreen($text, $eol = false) + * @method Writer boldCyanBgPurple($text, $eol = false) + * @method Writer boldCyanBgRed($text, $eol = false) + * @method Writer boldCyanBgWhite($text, $eol = false) + * @method Writer boldCyanBgYellow($text, $eol = false) + * @method Writer boldGreen($text, $eol = false) + * @method Writer boldGreenBgBlack($text, $eol = false) + * @method Writer boldGreenBgBlue($text, $eol = false) + * @method Writer boldGreenBgCyan($text, $eol = false) + * @method Writer boldGreenBgPurple($text, $eol = false) + * @method Writer boldGreenBgRed($text, $eol = false) + * @method Writer boldGreenBgWhite($text, $eol = false) + * @method Writer boldGreenBgYellow($text, $eol = false) + * @method Writer boldPurple($text, $eol = false) + * @method Writer boldPurpleBgBlack($text, $eol = false) + * @method Writer boldPurpleBgBlue($text, $eol = false) + * @method Writer boldPurpleBgCyan($text, $eol = false) + * @method Writer boldPurpleBgGreen($text, $eol = false) + * @method Writer boldPurpleBgRed($text, $eol = false) + * @method Writer boldPurpleBgWhite($text, $eol = false) + * @method Writer boldPurpleBgYellow($text, $eol = false) + * @method Writer boldRed($text, $eol = false) + * @method Writer boldRedBgBlack($text, $eol = false) + * @method Writer boldRedBgBlue($text, $eol = false) + * @method Writer boldRedBgCyan($text, $eol = false) + * @method Writer boldRedBgGreen($text, $eol = false) + * @method Writer boldRedBgPurple($text, $eol = false) + * @method Writer boldRedBgWhite($text, $eol = false) + * @method Writer boldRedBgYellow($text, $eol = false) + * @method Writer boldWhite($text, $eol = false) + * @method Writer boldWhiteBgBlack($text, $eol = false) + * @method Writer boldWhiteBgBlue($text, $eol = false) + * @method Writer boldWhiteBgCyan($text, $eol = false) + * @method Writer boldWhiteBgGreen($text, $eol = false) + * @method Writer boldWhiteBgPurple($text, $eol = false) + * @method Writer boldWhiteBgRed($text, $eol = false) + * @method Writer boldWhiteBgYellow($text, $eol = false) + * @method Writer boldYellow($text, $eol = false) + * @method Writer boldYellowBgBlack($text, $eol = false) + * @method Writer boldYellowBgBlue($text, $eol = false) + * @method Writer boldYellowBgCyan($text, $eol = false) + * @method Writer boldYellowBgGreen($text, $eol = false) + * @method Writer boldYellowBgPurple($text, $eol = false) + * @method Writer boldYellowBgRed($text, $eol = false) + * @method Writer boldYellowBgWhite($text, $eol = false) + * @method Writer colors($text) + * @method Writer comment($text, $eol = false) + * @method Writer cyan($text, $eol = false) + * @method Writer cyanBgBlack($text, $eol = false) + * @method Writer cyanBgBlue($text, $eol = false) + * @method Writer cyanBgGreen($text, $eol = false) + * @method Writer cyanBgPurple($text, $eol = false) + * @method Writer cyanBgRed($text, $eol = false) + * @method Writer cyanBgWhite($text, $eol = false) + * @method Writer cyanBgYellow($text, $eol = false) + * @method Writer error($text, $eol = false) + * @method Writer green($text, $eol = false) + * @method Writer greenBgBlack($text, $eol = false) + * @method Writer greenBgBlue($text, $eol = false) + * @method Writer greenBgCyan($text, $eol = false) + * @method Writer greenBgPurple($text, $eol = false) + * @method Writer greenBgRed($text, $eol = false) + * @method Writer greenBgWhite($text, $eol = false) + * @method Writer greenBgYellow($text, $eol = false) + * @method Writer info($text, $eol = false) + * @method Writer ok($text, $eol = false) + * @method Writer purple($text, $eol = false) + * @method Writer purpleBgBlack($text, $eol = false) + * @method Writer purpleBgBlue($text, $eol = false) + * @method Writer purpleBgCyan($text, $eol = false) + * @method Writer purpleBgGreen($text, $eol = false) + * @method Writer purpleBgRed($text, $eol = false) + * @method Writer purpleBgWhite($text, $eol = false) + * @method Writer purpleBgYellow($text, $eol = false) + * @method Writer red($text, $eol = false) + * @method Writer redBgBlack($text, $eol = false) + * @method Writer redBgBlue($text, $eol = false) + * @method Writer redBgCyan($text, $eol = false) + * @method Writer redBgGreen($text, $eol = false) + * @method Writer redBgPurple($text, $eol = false) + * @method Writer redBgWhite($text, $eol = false) + * @method Writer redBgYellow($text, $eol = false) + * @method Writer warn($text, $eol = false) + * @method Writer white($text, $eol = false) + * @method Writer yellow($text, $eol = false) + * @method Writer yellowBgBlack($text, $eol = false) + * @method Writer yellowBgBlue($text, $eol = false) + * @method Writer yellowBgCyan($text, $eol = false) + * @method Writer yellowBgGreen($text, $eol = false) + * @method Writer yellowBgPurple($text, $eol = false) + * @method Writer yellowBgRed($text, $eol = false) + * @method Writer yellowBgWhite($text, $eol = false) */ class Interactor { From 84c19f86933c1ac8150fe489eac4dc40e278f7f6 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 16:46:54 -0600 Subject: [PATCH 12/39] Put back eol and table method definitions for Interactor --- src/IO/Interactor.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index 68fbbb6..5e2deab 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -136,6 +136,7 @@ * @method Writer cyanBgRed($text, $eol = false) * @method Writer cyanBgWhite($text, $eol = false) * @method Writer cyanBgYellow($text, $eol = false) + * @method Writer eol(int $n = 1) * @method Writer error($text, $eol = false) * @method Writer green($text, $eol = false) * @method Writer greenBgBlack($text, $eol = false) @@ -163,6 +164,7 @@ * @method Writer redBgPurple($text, $eol = false) * @method Writer redBgWhite($text, $eol = false) * @method Writer redBgYellow($text, $eol = false) + * @method Writer table(array $rows, array $styles = []) * @method Writer warn($text, $eol = false) * @method Writer white($text, $eol = false) * @method Writer yellow($text, $eol = false) From 2c908c499a742142cfc1bdf22d089b8b88329690 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 16:49:46 -0600 Subject: [PATCH 13/39] Put this blank line back? I'm not sure how that happened --- src/IO/Interactor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index 5e2deab..ec6e2a7 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -35,7 +35,7 @@ * * @author Jitendra Adhikari * @license MIT - * + * * @link https://github.com/adhocore/cli * @method Writer bgBlack($text, $eol = false) * @method Writer bgBlue($text, $eol = false) From 360eb14760d5749737d7cdb8521cc03c96b5d821 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 16:52:04 -0600 Subject: [PATCH 14/39] Remove un-needed space --- src/IO/Interactor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index ec6e2a7..5e2deab 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -35,7 +35,7 @@ * * @author Jitendra Adhikari * @license MIT - * + * * @link https://github.com/adhocore/cli * @method Writer bgBlack($text, $eol = false) * @method Writer bgBlue($text, $eol = false) From f852324e738c29d124c52aaccd0d4728ebe12250 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 17:00:50 -0600 Subject: [PATCH 15/39] Hopefully fix issue with test --- src/IO/Interactor.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index 5e2deab..6fda3ce 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -37,6 +37,7 @@ * @license MIT * * @link https://github.com/adhocore/cli + * * @method Writer bgBlack($text, $eol = false) * @method Writer bgBlue($text, $eol = false) * @method Writer bgCyan($text, $eol = false) From 0bb1d315afc912fda7e3bc6f4f7ceb61a1d45cb9 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 19:39:46 -0600 Subject: [PATCH 16/39] Put factory color functions back --- src/Output/Color.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Output/Color.php b/src/Output/Color.php index 0c8ede1..1838cd1 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -74,6 +74,46 @@ class Color 'warn' => ['fg' => 33], ]; + /** + * Returns a line formatted as comment. + */ + public function comment(string $text, array $style = []): string + { + return $this->line($text, static::$styles['comment'] + $style); + } + + /** + * Returns a line formatted as comment. + */ + public function error(string $text, array $style = []): string + { + return $this->line($text, static::$styles['error'] + $style); + } + + /** + * Returns a line formatted as ok msg. + */ + public function ok(string $text, array $style = []): string + { + return $this->line($text, static::$styles['ok'] + $style); + } + + /** + * Returns a line formatted as warning. + */ + public function warn(string $text, array $style = []): string + { + return $this->line($text, static::$styles['warn'] + $style); + } + + /** + * Returns a line formatted as info. + */ + public function info(string $text, array $style = []): string + { + return $this->line($text, static::$styles['info'] + $style); + } + /** * Returns the color code for a 256 background color. */ From 346c0f7acbf0824b31a31bb4b6ae452bcd2243a3 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 19:51:51 -0600 Subject: [PATCH 17/39] Prevent users from making factory functions invisible --- src/Output/Color.php | 4 ++++ tests/Output/ColorTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Output/Color.php b/src/Output/Color.php index 1838cd1..c98c8df 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -192,6 +192,10 @@ public static function style(string $name, array $style): void throw new InvalidArgumentException('Trying to set empty or invalid style'); } + if (method_exists(static::class, $name) && isset($style['bg']) && isset($style['fg']) && $style['bg'] === $style['fg']) { + throw new InvalidArgumentException('Built-in styles cannot be invisible (matching background and foreground)'); + } + static::$styles[$name] = $style; } diff --git a/tests/Output/ColorTest.php b/tests/Output/ColorTest.php index c7e7cf4..29daf6c 100644 --- a/tests/Output/ColorTest.php +++ b/tests/Output/ColorTest.php @@ -45,6 +45,14 @@ public function test_invalid_custom_style() Color::style('alert', ['invalid' => true]); } + public function test_invisible_built_in_style() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Built-in styles cannot be invisible (matching background and foreground)'); + + Color::style('error', ['bg' => Color::RED, 'fg' => Color::RED]); + } + public function test_colors() { $c = new Color; From 274abbb7d1f57e7e579a14ffc240676c5a3b7a41 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 19:53:30 -0600 Subject: [PATCH 18/39] Shorten a couple lines --- src/Output/Color.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Output/Color.php b/src/Output/Color.php index c98c8df..0e244b1 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -192,8 +192,9 @@ public static function style(string $name, array $style): void throw new InvalidArgumentException('Trying to set empty or invalid style'); } - if (method_exists(static::class, $name) && isset($style['bg']) && isset($style['fg']) && $style['bg'] === $style['fg']) { - throw new InvalidArgumentException('Built-in styles cannot be invisible (matching background and foreground)'); + $invisible = (isset($style['bg']) && isset($style['fg']) && $style['bg'] === $style['fg']); + if ($invisible && method_exists(static::class, $name)) { + throw new InvalidArgumentException('Built-in styles cannot be invisible'); } static::$styles[$name] = $style; From 0fdd4e6353a3051ec260b43c30df4b0ca4b625e0 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 20:04:13 -0600 Subject: [PATCH 19/39] Update invisible_built_in_style expected exception message --- tests/Output/ColorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Output/ColorTest.php b/tests/Output/ColorTest.php index 29daf6c..bde4bdd 100644 --- a/tests/Output/ColorTest.php +++ b/tests/Output/ColorTest.php @@ -48,7 +48,7 @@ public function test_invalid_custom_style() public function test_invisible_built_in_style() { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Built-in styles cannot be invisible (matching background and foreground)'); + $this->expectExceptionMessage('Built-in styles cannot be invisible'); Color::style('error', ['bg' => Color::RED, 'fg' => Color::RED]); } From f2d89226c9aef630dc83d5894b53b30b95a51306 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 22:09:48 -0600 Subject: [PATCH 20/39] Add new method definitions for Writer and Interactor classes --- src/IO/Interactor.php | 13 +++++++++++++ src/Output/Writer.php | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index 6fda3ce..1881ea7 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -38,6 +38,7 @@ * * @link https://github.com/adhocore/cli * + * @method Writer answer($text, $eol = false) * @method Writer bgBlack($text, $eol = false) * @method Writer bgBlue($text, $eol = false) * @method Writer bgCyan($text, $eol = false) @@ -127,6 +128,7 @@ * @method Writer boldYellowBgPurple($text, $eol = false) * @method Writer boldYellowBgRed($text, $eol = false) * @method Writer boldYellowBgWhite($text, $eol = false) + * @method Writer choice($text, $eol = false) * @method Writer colors($text) * @method Writer comment($text, $eol = false) * @method Writer cyan($text, $eol = false) @@ -147,6 +149,15 @@ * @method Writer greenBgRed($text, $eol = false) * @method Writer greenBgWhite($text, $eol = false) * @method Writer greenBgYellow($text, $eol = false) + * @method Writer help_category($text, $eol = false) + * @method Writer help_description($text, $eol = false) + * @method Writer help_example($text, $eol = false) + * @method Writer help_footer($text, $eol = false) + * @method Writer help_group($text, $eol = false) + * @method Writer help_header($text, $eol = false) + * @method Writer help_item($text, $eol = false) + * @method Writer help_summary($text, $eol = false) + * @method Writer help_text($text, $eol = false) * @method Writer info($text, $eol = false) * @method Writer ok($text, $eol = false) * @method Writer purple($text, $eol = false) @@ -157,6 +168,7 @@ * @method Writer purpleBgRed($text, $eol = false) * @method Writer purpleBgWhite($text, $eol = false) * @method Writer purpleBgYellow($text, $eol = false) + * @method Writer question($text, $eol = false) * @method Writer red($text, $eol = false) * @method Writer redBgBlack($text, $eol = false) * @method Writer redBgBlue($text, $eol = false) @@ -166,6 +178,7 @@ * @method Writer redBgWhite($text, $eol = false) * @method Writer redBgYellow($text, $eol = false) * @method Writer table(array $rows, array $styles = []) + * @method Writer version($text, $eol = false) * @method Writer warn($text, $eol = false) * @method Writer white($text, $eol = false) * @method Writer yellow($text, $eol = false) diff --git a/src/Output/Writer.php b/src/Output/Writer.php index 626978f..38f3ebe 100644 --- a/src/Output/Writer.php +++ b/src/Output/Writer.php @@ -35,6 +35,7 @@ * * @link https://github.com/adhocore/cli * + * @method Writer answer($text, $eol = false) * @method Writer bgBlack($text, $eol = false) * @method Writer bgBlue($text, $eol = false) * @method Writer bgCyan($text, $eol = false) @@ -124,6 +125,7 @@ * @method Writer boldYellowBgPurple($text, $eol = false) * @method Writer boldYellowBgRed($text, $eol = false) * @method Writer boldYellowBgWhite($text, $eol = false) + * @method Writer choice($text, $eol = false) * @method Writer colors($text) * @method Writer comment($text, $eol = false) * @method Writer cyan($text, $eol = false) @@ -143,6 +145,15 @@ * @method Writer greenBgRed($text, $eol = false) * @method Writer greenBgWhite($text, $eol = false) * @method Writer greenBgYellow($text, $eol = false) + * @method Writer help_category($text, $eol = false) + * @method Writer help_description($text, $eol = false) + * @method Writer help_example($text, $eol = false) + * @method Writer help_footer($text, $eol = false) + * @method Writer help_group($text, $eol = false) + * @method Writer help_header($text, $eol = false) + * @method Writer help_item($text, $eol = false) + * @method Writer help_summary($text, $eol = false) + * @method Writer help_text($text, $eol = false) * @method Writer info($text, $eol = false) * @method Writer ok($text, $eol = false) * @method Writer purple($text, $eol = false) @@ -153,6 +164,7 @@ * @method Writer purpleBgRed($text, $eol = false) * @method Writer purpleBgWhite($text, $eol = false) * @method Writer purpleBgYellow($text, $eol = false) + * @method Writer question($text, $eol = false) * @method Writer red($text, $eol = false) * @method Writer redBgBlack($text, $eol = false) * @method Writer redBgBlue($text, $eol = false) @@ -161,6 +173,7 @@ * @method Writer redBgPurple($text, $eol = false) * @method Writer redBgWhite($text, $eol = false) * @method Writer redBgYellow($text, $eol = false) + * @method Writer version($text, $eol = false) * @method Writer warn($text, $eol = false) * @method Writer white($text, $eol = false) * @method Writer yellow($text, $eol = false) From 8ea5a657b1af6c86eec23d6b266c05e253503eca Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 23:20:28 -0600 Subject: [PATCH 21/39] Make logo a built-in style --- README.md | 1 + src/Application.php | 2 +- src/IO/Interactor.php | 1 + src/Output/Color.php | 1 + src/Output/Writer.php | 1 + 5 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d97d571..e69dc6e 100644 --- a/README.md +++ b/README.md @@ -483,6 +483,7 @@ There are a number of pre-defined built-in styles that allows you granular custo - help_summary - help_text - info + - logo - ok - question - version diff --git a/src/Application.php b/src/Application.php index 3b41793..eb5d5e1 100644 --- a/src/Application.php +++ b/src/Application.php @@ -353,7 +353,7 @@ public function showHelp(): mixed $footer = 'Run ` --help` for specific help'; if ($this->logo) { - $writer->write($this->logo, true); + $writer->logo($this->logo, true); } $this->outputHelper()->showCommandsHelp($this->commands(), $header, $footer); diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index 1881ea7..a36747b 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -159,6 +159,7 @@ * @method Writer help_summary($text, $eol = false) * @method Writer help_text($text, $eol = false) * @method Writer info($text, $eol = false) + * @method Writer logo($text, $eol = false) * @method Writer ok($text, $eol = false) * @method Writer purple($text, $eol = false) * @method Writer purpleBgBlack($text, $eol = false) diff --git a/src/Output/Color.php b/src/Output/Color.php index 0e244b1..ee175cf 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -68,6 +68,7 @@ class Color 'help_summary' => ['fg' => 37, 'mod' => 2], 'help_text' => ['fg' => 37, 'mod' => 1], 'info' => ['fg' => 34], + 'logo' => ['fg' => 37], 'ok' => ['fg' => 32], 'question' => ['fg' => 33], 'version' => ['fg' => 37, 'mod' => 1], diff --git a/src/Output/Writer.php b/src/Output/Writer.php index 38f3ebe..9528c12 100644 --- a/src/Output/Writer.php +++ b/src/Output/Writer.php @@ -155,6 +155,7 @@ * @method Writer help_summary($text, $eol = false) * @method Writer help_text($text, $eol = false) * @method Writer info($text, $eol = false) + * @method Writer logo($text, $eol = false) * @method Writer ok($text, $eol = false) * @method Writer purple($text, $eol = false) * @method Writer purpleBgBlack($text, $eol = false) From a128794b61c484fda92033f724e3bdffe8b9a7cc Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 23:57:54 -0600 Subject: [PATCH 22/39] Make help_description into help_description_even/odd and make help_item into help_item_even/odd --- README.md | 6 ++++-- src/Helper/OutputHelper.php | 11 +++++++--- src/IO/Interactor.php | 6 ++++-- src/Output/Color.php | 40 +++++++++++++++++++------------------ src/Output/Writer.php | 6 ++++-- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index e69dc6e..20e21f5 100644 --- a/README.md +++ b/README.md @@ -474,12 +474,14 @@ There are a number of pre-defined built-in styles that allows you granular custo - comment - error - help_category - - help_description + - help_description_even + - help_description_odd - help_example - help_footer - help_group - help_header - - help_item + - help_item_even + - help_item_odd - help_summary - help_text - info diff --git a/src/Helper/OutputHelper.php b/src/Helper/OutputHelper.php index df518c8..21e9583 100644 --- a/src/Helper/OutputHelper.php +++ b/src/Helper/OutputHelper.php @@ -197,7 +197,7 @@ protected function showHelp(string $for, array $items, string $header = '', stri $group = $lastGroup = null; $withDefault = $for === 'Options' || $for === 'Arguments'; - foreach ($this->sortItems($items, $padLen, $for) as $item) { + foreach (array_values($this->sortItems($items, $padLen, $for)) as $idx => $item) { $name = $this->getName($item); if ($for === 'Commands' && $lastGroup !== $group = $item->group()) { $this->writer->help_group($group ?: '*', true); @@ -205,8 +205,13 @@ protected function showHelp(string $for, array $items, string $header = '', stri } $desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc($withDefault)); - $this->writer->help_item(' ' . str_pad($name, $padLen + $space)); - $this->writer->help_description($desc, true); + if ($idx % 2 == 0) { + $this->writer->help_item_even(' ' . str_pad($name, $padLen + $space)); + $this->writer->help_description_even($desc, true); + } else { + $this->writer->help_item_odd(' ' . str_pad($name, $padLen + $space)); + $this->writer->help_description_odd($desc, true); + } } if ($footer) { diff --git a/src/IO/Interactor.php b/src/IO/Interactor.php index a36747b..9112383 100644 --- a/src/IO/Interactor.php +++ b/src/IO/Interactor.php @@ -150,12 +150,14 @@ * @method Writer greenBgWhite($text, $eol = false) * @method Writer greenBgYellow($text, $eol = false) * @method Writer help_category($text, $eol = false) - * @method Writer help_description($text, $eol = false) + * @method Writer help_description_even($text, $eol = false) + * @method Writer help_description_odd($text, $eol = false) * @method Writer help_example($text, $eol = false) * @method Writer help_footer($text, $eol = false) * @method Writer help_group($text, $eol = false) * @method Writer help_header($text, $eol = false) - * @method Writer help_item($text, $eol = false) + * @method Writer help_item_even($text, $eol = false) + * @method Writer help_item_odd($text, $eol = false) * @method Writer help_summary($text, $eol = false) * @method Writer help_text($text, $eol = false) * @method Writer info($text, $eol = false) diff --git a/src/Output/Color.php b/src/Output/Color.php index ee175cf..876b503 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -54,25 +54,27 @@ class Color /** @var array Custom styles */ protected static array $styles = [ - 'answer' => ['fg' => 37, 'mod' => 2], - 'choice' => ['fg' => 36], - 'comment' => ['fg' => 37, 'mod' => 2], - 'error' => ['fg' => 31], - 'help_category' => ['fg' => 32, 'mod' => 1], - 'help_description' => ['fg' => 37, 'mod' => 2], - 'help_example' => ['fg' => 33], - 'help_footer' => ['fg' => 33], - 'help_group' => ['fg' => 33, 'mod' => 1], - 'help_header' => ['fg' => 37, 'mod' => 1], - 'help_item' => ['fg' => 37, 'mod' => 1], - 'help_summary' => ['fg' => 37, 'mod' => 2], - 'help_text' => ['fg' => 37, 'mod' => 1], - 'info' => ['fg' => 34], - 'logo' => ['fg' => 37], - 'ok' => ['fg' => 32], - 'question' => ['fg' => 33], - 'version' => ['fg' => 37, 'mod' => 1], - 'warn' => ['fg' => 33], + 'answer' => ['fg' => 37, 'mod' => 2], + 'choice' => ['fg' => 36], + 'comment' => ['fg' => 37, 'mod' => 2], + 'error' => ['fg' => 31], + 'help_category' => ['fg' => 32, 'mod' => 1], + 'help_description_even' => ['fg' => 37, 'mod' => 2], + 'help_description_odd' => ['fg' => 37, 'mod' => 2], + 'help_example' => ['fg' => 33], + 'help_footer' => ['fg' => 33], + 'help_group' => ['fg' => 33, 'mod' => 1], + 'help_header' => ['fg' => 37, 'mod' => 1], + 'help_item_even' => ['fg' => 37, 'mod' => 1], + 'help_item_odd' => ['fg' => 37, 'mod' => 1], + 'help_summary' => ['fg' => 37, 'mod' => 2], + 'help_text' => ['fg' => 37, 'mod' => 1], + 'info' => ['fg' => 34], + 'logo' => ['fg' => 37], + 'ok' => ['fg' => 32], + 'question' => ['fg' => 33], + 'version' => ['fg' => 37, 'mod' => 1], + 'warn' => ['fg' => 33], ]; /** diff --git a/src/Output/Writer.php b/src/Output/Writer.php index 9528c12..ca1cff9 100644 --- a/src/Output/Writer.php +++ b/src/Output/Writer.php @@ -146,12 +146,14 @@ * @method Writer greenBgWhite($text, $eol = false) * @method Writer greenBgYellow($text, $eol = false) * @method Writer help_category($text, $eol = false) - * @method Writer help_description($text, $eol = false) + * @method Writer help_description_even($text, $eol = false) + * @method Writer help_description_odd($text, $eol = false) * @method Writer help_example($text, $eol = false) * @method Writer help_footer($text, $eol = false) * @method Writer help_group($text, $eol = false) * @method Writer help_header($text, $eol = false) - * @method Writer help_item($text, $eol = false) + * @method Writer help_item_even($text, $eol = false) + * @method Writer help_item_odd($text, $eol = false) * @method Writer help_summary($text, $eol = false) * @method Writer help_text($text, $eol = false) * @method Writer info($text, $eol = false) From d3e7e7613b1df0ea8c47ece32cf4e09a826426f4 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Tue, 26 Nov 2024 00:04:20 -0600 Subject: [PATCH 23/39] Fix indentation --- src/Helper/OutputHelper.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Helper/OutputHelper.php b/src/Helper/OutputHelper.php index 21e9583..5f54200 100644 --- a/src/Helper/OutputHelper.php +++ b/src/Helper/OutputHelper.php @@ -206,11 +206,11 @@ protected function showHelp(string $for, array $items, string $header = '', stri $desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc($withDefault)); if ($idx % 2 == 0) { - $this->writer->help_item_even(' ' . str_pad($name, $padLen + $space)); - $this->writer->help_description_even($desc, true); + $this->writer->help_item_even(' ' . str_pad($name, $padLen + $space)); + $this->writer->help_description_even($desc, true); } else { - $this->writer->help_item_odd(' ' . str_pad($name, $padLen + $space)); - $this->writer->help_description_odd($desc, true); + $this->writer->help_item_odd(' ' . str_pad($name, $padLen + $space)); + $this->writer->help_description_odd($desc, true); } } From 2c9858c8a743823974b7050fa08212206457c5d2 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 19:25:49 -0600 Subject: [PATCH 24/39] Add support for custom help screens --- README.md | 4 ++++ src/Application.php | 38 ++++++++++++++++++++++++++++++++++++- src/Input/Command.php | 36 ++++++++++++++++++++++++++++++++++- tests/ApplicationTest.php | 11 +++++++++++ tests/Input/CommandTest.php | 7 +++++++ 5 files changed, 94 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 20e21f5..88bd995 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,7 @@ class InitCommand extends Ahc\Cli\Input\Command ->argument('[arg2]', 'The Arg2') ->option('-a --apple', 'The Apple') ->option('-b --ball', 'The ball') + ->help('Custom help screen (if omitted one will be generated)') // Usage examples: ->usage( // append details or explanation of given example with ` ## ` so they will be uniformly aligned when shown @@ -243,6 +244,9 @@ $app->add(new OtherCommand, 'o'); // Set logo $app->logo('Ascii art logo of your app'); +// Custom help screen +$app->help('Custom help screen (if omitted one will be generated)'); + $app->handle($_SERVER['argv']); // if argv[1] is `i` or `init` it executes InitCommand ``` diff --git a/src/Application.php b/src/Application.php index eb5d5e1..2796325 100644 --- a/src/Application.php +++ b/src/Application.php @@ -52,6 +52,10 @@ class Application /** @var string Ascii art logo */ protected string $logo = ''; + /** @var string Custom help screen */ + protected string $_help = ''; + + /** @var string Name of default command */ protected string $default = '__default__'; /** @var null|Interactor */ @@ -344,9 +348,41 @@ protected function aliasesFor(Command $command): array } /** - * Show help of all commands. + * Sets or gets the custom help screen contents. + * + * @param string|null $help + * + * @return string|self + */ + public function help(?string $help = null): mixed + { + if (func_num_args() === 0) { + return $this->_help; + } + + $this->_help = $help; + + return $this; + } + + /** + * Show custom help screen if one is set, otherwise shows the default one. */ public function showHelp(): mixed + { + if ($help = $this->help()) { + $writer = $this->io()->writer(); + $writer->write($help, true); + return ($this->onExit)(); + } + + return $this->showDefaultHelp(); + } + + /** + * Shows command help then aborts. + */ + public function showDefaultHelp(): mixed { $writer = $this->io()->writer(); $header = "{$this->name}, version {$this->version}"; diff --git a/src/Input/Command.php b/src/Input/Command.php index 49dbfb4..6920742 100644 --- a/src/Input/Command.php +++ b/src/Input/Command.php @@ -52,6 +52,8 @@ class Command extends Parser implements Groupable protected ?string $_alias = null; + protected string $_help = ''; + private array $_events = []; private bool $_argVariadic = false; @@ -291,9 +293,41 @@ protected function handleUnknown(string $arg, ?string $value = null): mixed } /** - * Shows command help then aborts. + * Sets or gets the custom help screen contents. + * + * @param string|null $help + * + * @return string|self + */ + public function help(?string $help = null): mixed + { + if (func_num_args() === 0) { + return $this->_help; + } + + $this->_help = $help; + + return $this; + } + + /** + * Show custom help screen if one is set, otherwise shows the default one. */ public function showHelp(): mixed + { + if ($help = $this->help()) { + $writer = $this->io()->writer(); + $writer->write($help, true); + return $this->emit('_exit', 0); + } + + return $this->showDefaultHelp(); + } + + /** + * Shows command help then aborts. + */ + public function showDefaultHelp(): mixed { $io = $this->io(); $helper = new OutputHelper($io->writer()); diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index b6fce4f..de4cbb5 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -147,6 +147,17 @@ public function test_help() $this->assertStringContainsString('stage change', $out); } + public function testCustomHelp() + { + $this->newApp('git', '0.0.2') + ->help('This should be my custom help screen') + ->parse(['git', '--help']); + + $out = file_get_contents(static::$ou); + + $this->assertStringContainsString('This should be my custom help screen', $out); + } + public function test_action() { ($a = $this->newApp('git', '0.0.2')) diff --git a/tests/Input/CommandTest.php b/tests/Input/CommandTest.php index 4c03622..f2c58cf 100644 --- a/tests/Input/CommandTest.php +++ b/tests/Input/CommandTest.php @@ -284,6 +284,13 @@ public function __construct($tester) } }; } + + public function test_custom_help() + { + $p = $this->newCommand(); + $p->help('This should be my custom help screen'); + $this->assertStringContainsString('This should be my custom help screen', $p->help()); + } protected function newCommand(string $version = '0.0.1', string $desc = '', bool $allowUnknown = false, $app = null) { From 87f723d76d241c1b2e10209c2f40400dc8fb82dc Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 19:30:20 -0600 Subject: [PATCH 25/39] Fix identation --- src/Application.php | 2 +- src/Input/Command.php | 2 +- tests/Input/CommandTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Application.php b/src/Application.php index 2796325..104fb58 100644 --- a/src/Application.php +++ b/src/Application.php @@ -357,7 +357,7 @@ protected function aliasesFor(Command $command): array public function help(?string $help = null): mixed { if (func_num_args() === 0) { - return $this->_help; + return $this->_help; } $this->_help = $help; diff --git a/src/Input/Command.php b/src/Input/Command.php index 6920742..a308f6d 100644 --- a/src/Input/Command.php +++ b/src/Input/Command.php @@ -302,7 +302,7 @@ protected function handleUnknown(string $arg, ?string $value = null): mixed public function help(?string $help = null): mixed { if (func_num_args() === 0) { - return $this->_help; + return $this->_help; } $this->_help = $help; diff --git a/tests/Input/CommandTest.php b/tests/Input/CommandTest.php index f2c58cf..4cb5754 100644 --- a/tests/Input/CommandTest.php +++ b/tests/Input/CommandTest.php @@ -284,7 +284,7 @@ public function __construct($tester) } }; } - + public function test_custom_help() { $p = $this->newCommand(); From 34dd52106cf4aa1106baa9861f353dfb2efb2778 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 19:32:04 -0600 Subject: [PATCH 26/39] Hopefully fix issues with tests --- src/Application.php | 2 +- src/Input/Command.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Application.php b/src/Application.php index 104fb58..1b57f22 100644 --- a/src/Application.php +++ b/src/Application.php @@ -52,7 +52,6 @@ class Application /** @var string Ascii art logo */ protected string $logo = ''; - /** @var string Custom help screen */ protected string $_help = ''; /** @var string Name of default command */ @@ -373,6 +372,7 @@ public function showHelp(): mixed if ($help = $this->help()) { $writer = $this->io()->writer(); $writer->write($help, true); + return ($this->onExit)(); } diff --git a/src/Input/Command.php b/src/Input/Command.php index a308f6d..c1fc9f2 100644 --- a/src/Input/Command.php +++ b/src/Input/Command.php @@ -318,6 +318,7 @@ public function showHelp(): mixed if ($help = $this->help()) { $writer = $this->io()->writer(); $writer->write($help, true); + return $this->emit('_exit', 0); } From 88b224eb9148c9abd87662e3b8dfdf1564eb3c9c Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 19:34:10 -0600 Subject: [PATCH 27/39] Tests don't like variables starting with underscores in the application --- src/Application.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Application.php b/src/Application.php index 1b57f22..0d30a89 100644 --- a/src/Application.php +++ b/src/Application.php @@ -52,7 +52,8 @@ class Application /** @var string Ascii art logo */ protected string $logo = ''; - protected string $_help = ''; + /** @var string Custom help screen */ + protected string $custom_help = ''; /** @var string Name of default command */ protected string $default = '__default__'; @@ -356,10 +357,10 @@ protected function aliasesFor(Command $command): array public function help(?string $help = null): mixed { if (func_num_args() === 0) { - return $this->_help; + return $this->custom_help; } - $this->_help = $help; + $this->custom_help = $help; return $this; } From ef297278a285bc6553e3161a1c4c882137ef01cd Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:03:21 -0600 Subject: [PATCH 28/39] Add example to readme showing how you can get colors working in a custom help screen --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 88bd995..05b655b 100644 --- a/README.md +++ b/README.md @@ -170,12 +170,16 @@ class InitCommand extends Ahc\Cli\Input\Command { parent::__construct('init', 'Init something'); + $help = 'Custom help screen'; + $writer = new Ahc\Cli\Output\Writer(); + $rendered_help = $writer->colorizer()->colors($help); + $this ->argument('', 'The Arrg') ->argument('[arg2]', 'The Arg2') ->option('-a --apple', 'The Apple') ->option('-b --ball', 'The ball') - ->help('Custom help screen (if omitted one will be generated)') + ->help($rendered_help) // Usage examples: ->usage( // append details or explanation of given example with ` ## ` so they will be uniformly aligned when shown From 86ecf3051ccdcfce49b13c710ca8e6bb00054308 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 22:16:31 -0600 Subject: [PATCH 29/39] Just pass the colorizer as the help parameter in the custom help readme example --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 05b655b..e88d23d 100644 --- a/README.md +++ b/README.md @@ -172,14 +172,13 @@ class InitCommand extends Ahc\Cli\Input\Command $help = 'Custom help screen'; $writer = new Ahc\Cli\Output\Writer(); - $rendered_help = $writer->colorizer()->colors($help); $this ->argument('', 'The Arrg') ->argument('[arg2]', 'The Arg2') ->option('-a --apple', 'The Apple') ->option('-b --ball', 'The ball') - ->help($rendered_help) + ->help($writer->colorizer()->colors($help)) // Usage examples: ->usage( // append details or explanation of given example with ` ## ` so they will be uniformly aligned when shown From bc70bff05e11f4a6cdf555402e09fa2dd0e15a79 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 22:17:26 -0600 Subject: [PATCH 30/39] Rename custom_help to help --- src/Application.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Application.php b/src/Application.php index 0d30a89..187089c 100644 --- a/src/Application.php +++ b/src/Application.php @@ -53,7 +53,7 @@ class Application protected string $logo = ''; /** @var string Custom help screen */ - protected string $custom_help = ''; + protected string $help = ''; /** @var string Name of default command */ protected string $default = '__default__'; @@ -357,10 +357,10 @@ protected function aliasesFor(Command $command): array public function help(?string $help = null): mixed { if (func_num_args() === 0) { - return $this->custom_help; + return $this->help; } - $this->custom_help = $help; + $this->help = $help; return $this; } From 21d49f791529da111f177ae004ec8311871f352e Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 10:42:06 -0600 Subject: [PATCH 31/39] Add support for 256 colors --- README.md | 2 +- src/Output/Color.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 416627a..f5f3e38 100644 --- a/README.md +++ b/README.md @@ -450,7 +450,7 @@ echo $color->ok('This is ok msg'); ```php Ahc\Cli\Output\Color::style('mystyle', [ 'bg' => Ahc\Cli\Output\Color::CYAN, - 'fg' => Ahc\Cli\Output\Color::WHITE, + 'fg' => '38;5;57', // 256 colors can be used as well 'bold' => 1, // You can experiment with 0, 1, 2, 3 ... as well ]); diff --git a/src/Output/Color.php b/src/Output/Color.php index e53ee69..12fe047 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -130,8 +130,8 @@ public function line(string $text, array $style = []): string $line = strtr($format, [ ':mod:' => (int) ($style['mod'] ?? $style['bold']), - ':fg:' => (int) $style['fg'], - ':bg:' => (int) $style['bg'] + 10, + ':fg:' => $style['fg'], + ':bg:' => is_int($style['bg']) ? ($style['bg'] + 10) : $style['bg'], ':txt:' => $text, ]); From 96c51fe2e5271c8f5c2e17ec298a94616d8e3040 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 10:42:37 -0600 Subject: [PATCH 32/39] Add documentation about default command functionality --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index f5f3e38..770d7dd 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,21 @@ $app->add((new ConfigListCommand)->inGroup('Config')); ... ``` +#### Default command + +By default, running your CLI app without any arguments will show the help screen. However you can set the default action to run one of your commands either by setting the third parameter of the `add` function to `true` or by using the `defaultCommand` function. + +```php +$app->add(new InitCommand, 'i', true); + +# Alternatively +$app->command('init', 'Init something', 'i'); +$app->defaultCommand('init'); + +# Retrieve the name of the default command +$default_command = $app->getDefaultCommand(); +``` + #### Exception handler Set a custom exception handler as callback. The callback receives exception & exit code. The callback may rethrow exception or may exit the program or just log exception and do nothing else. From 5fc43fb7ccd8e68bb81db94ab1f3b642922f27c3 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 10:52:06 -0600 Subject: [PATCH 33/39] Use 256 colors in test_custom_style test --- tests/Output/ColorTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Output/ColorTest.php b/tests/Output/ColorTest.php index 622ddf8..bde4bdd 100644 --- a/tests/Output/ColorTest.php +++ b/tests/Output/ColorTest.php @@ -30,11 +30,11 @@ public function test_comment() public function test_custom_style() { - Color::style('alert', ['bg' => Color::YELLOW, 'fg' => Color::RED]); + Color::style('alert', ['bg' => '48;5;82', 'fg' => '38;5;57']); - $this->assertSame("\033[0;31;43malert\033[0m", (new Color)->alert('alert')); - $this->assertSame("\033[1;31;43malert\033[0m", (new Color)->boldAlert('alert')); - $this->assertSame("\033[1;31;43malert\033[0m", (new Color)->alertBold('alert')); + $this->assertSame("\033[0;38;5;57;48;5;82malert\033[0m", (new Color)->alert('alert')); + $this->assertSame("\033[1;38;5;57;48;5;82malert\033[0m", (new Color)->boldAlert('alert')); + $this->assertSame("\033[1;38;5;57;48;5;82malert\033[0m", (new Color)->alertBold('alert')); } public function test_invalid_custom_style() From 45ae4cd89674ef7ef2d4154756871bc389733a87 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:35:10 -0600 Subject: [PATCH 34/39] Add Color::bg256 and Color::fg256 --- README.md | 2 +- src/Output/Color.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 770d7dd..146f5a1 100644 --- a/README.md +++ b/README.md @@ -465,7 +465,7 @@ echo $color->ok('This is ok msg'); ```php Ahc\Cli\Output\Color::style('mystyle', [ 'bg' => Ahc\Cli\Output\Color::CYAN, - 'fg' => '38;5;57', // 256 colors can be used as well + 'fg' => hc\Cli\Output\Color::fg256(57), // 256 colors can be used as well 'bold' => 1, // You can experiment with 0, 1, 2, 3 ... as well ]); diff --git a/src/Output/Color.php b/src/Output/Color.php index 12fe047..4b50fe1 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -117,6 +117,22 @@ public function info(string $text, array $style = []): string return $this->line($text, static::$styles['info'] + $style); } + /** + * Returns the color code for a 256 background color + */ + public function bg256(int $code) + { + return "48;5;{$code}"; + } + + /** + * Returns the color code for a 256 foreground color + */ + public function fg256(int $code) + { + return "38;5;{$code}"; + } + /** * Returns a formatted/colored line. */ From 8f8f9c4803cf7a019c96c0c46d5e49a38d463668 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:38:31 -0600 Subject: [PATCH 35/39] Add periods to bg256 and fg256 descriptions --- 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 4b50fe1..55e11de 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -118,7 +118,7 @@ public function info(string $text, array $style = []): string } /** - * Returns the color code for a 256 background color + * Returns the color code for a 256 background color. */ public function bg256(int $code) { @@ -126,7 +126,7 @@ public function bg256(int $code) } /** - * Returns the color code for a 256 foreground color + * Returns the color code for a 256 foreground color. */ public function fg256(int $code) { From e45892fb6b7790989286680498a51fbd23a98ff6 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:40:26 -0600 Subject: [PATCH 36/39] Add mising A to fg256 example in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 146f5a1..088abbd 100644 --- a/README.md +++ b/README.md @@ -465,7 +465,7 @@ echo $color->ok('This is ok msg'); ```php Ahc\Cli\Output\Color::style('mystyle', [ 'bg' => Ahc\Cli\Output\Color::CYAN, - 'fg' => hc\Cli\Output\Color::fg256(57), // 256 colors can be used as well + 'fg' => Ahc\Cli\Output\Color::fg256(57), // 256 colors can be used as well 'bold' => 1, // You can experiment with 0, 1, 2, 3 ... as well ]); From 2e9d02894d54ffef31242e4da8e07c4073333a21 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:41:57 -0600 Subject: [PATCH 37/39] Add return types for bg256 and fg256 functions --- 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 55e11de..cc0d633 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -120,7 +120,7 @@ public function info(string $text, array $style = []): string /** * Returns the color code for a 256 background color. */ - public function bg256(int $code) + public function bg256(int $code): string { return "48;5;{$code}"; } @@ -128,7 +128,7 @@ public function bg256(int $code) /** * Returns the color code for a 256 foreground color. */ - public function fg256(int $code) + public function fg256(int $code): string { return "38;5;{$code}"; } From d608baaacdb835ef66b96e29ebfa2ef01ba9f692 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 21:55:03 -0600 Subject: [PATCH 38/39] Make bg256 and fg256 functions static --- 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 cc0d633..876b503 100644 --- a/src/Output/Color.php +++ b/src/Output/Color.php @@ -120,7 +120,7 @@ public function info(string $text, array $style = []): string /** * Returns the color code for a 256 background color. */ - public function bg256(int $code): string + public static function bg256(int $code): string { return "48;5;{$code}"; } @@ -128,7 +128,7 @@ public function bg256(int $code): string /** * Returns the color code for a 256 foreground color. */ - public function fg256(int $code): string + public static function fg256(int $code): string { return "38;5;{$code}"; } From 65caea5d940aeba908e0fe567b141338daea5ea5 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Mon, 25 Nov 2024 22:22:08 -0600 Subject: [PATCH 39/39] Use PHP comments in Default command example in readme instead of Bash comments --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 088abbd..e88d23d 100644 --- a/README.md +++ b/README.md @@ -281,11 +281,11 @@ By default, running your CLI app without any arguments will show the help screen ```php $app->add(new InitCommand, 'i', true); -# Alternatively +// Alternatively $app->command('init', 'Init something', 'i'); $app->defaultCommand('init'); -# Retrieve the name of the default command +// Retrieve the name of the default command $default_command = $app->getDefaultCommand(); ```