Skip to content

Commit

Permalink
Make all written text built-in styles so that their styles can be cus…
Browse files Browse the repository at this point in the history
…tomized
  • Loading branch information
kodie committed Nov 25, 2024
1 parent 0945f82 commit 3c5afcf
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 212 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,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.
Expand Down
18 changes: 9 additions & 9 deletions src/Helper/OutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
153 changes: 7 additions & 146 deletions src/IO/Interactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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</end>";
}

Expand Down
8 changes: 4 additions & 4 deletions src/Input/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 3c5afcf

Please sign in to comment.