Skip to content

Commit a6d6be1

Browse files
committed
Make all written text built-in styles so that their styles can be customized
1 parent 7bca2a3 commit a6d6be1

File tree

6 files changed

+81
-212
lines changed

6 files changed

+81
-212
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,38 @@ Ahc\Cli\Output\Color::style('mystyle', [
465465
echo $color->mystyle('My text');
466466
```
467467

468+
#### Built-in styles
469+
470+
There are a number of pre-defined built-in styles that allows you granular customization to different output conditions such as help and prompts:
471+
472+
- answer
473+
- choice
474+
- comment
475+
- error
476+
- help_category
477+
- help_description
478+
- help_example
479+
- help_footer
480+
- help_group
481+
- help_header
482+
- help_item
483+
- help_summary
484+
- help_text
485+
- info
486+
- ok
487+
- question
488+
- version
489+
- warn
490+
491+
Overriding a built-in style works the same way as defining a new style:
492+
493+
```php
494+
Ahc\Cli\Output\Color::style('error', [
495+
'fg' => Ahc\Cli\Output\Color::RED,
496+
'bold' => 1,
497+
]);
498+
```
499+
468500
### Cursor
469501

470502
Move cursor around, erase line up or down, clear screen.

src/Helper/OutputHelper.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ public function showCommandsHelp(array $commands, string $header = '', string $f
182182
protected function showHelp(string $for, array $items, string $header = '', string $footer = ''): void
183183
{
184184
if ($header) {
185-
$this->writer->bold($header, true);
185+
$this->writer->help_header($header, true);
186186
}
187187

188-
$this->writer->eol()->boldGreen($for . ':', true);
188+
$this->writer->eol()->help_category($for . ':', true);
189189

190190
if (empty($items)) {
191-
$this->writer->bold(' (n/a)', true);
191+
$this->writer->help_text(' (n/a)', true);
192192

193193
return;
194194
}
@@ -200,17 +200,17 @@ protected function showHelp(string $for, array $items, string $header = '', stri
200200
foreach ($this->sortItems($items, $padLen, $for) as $item) {
201201
$name = $this->getName($item);
202202
if ($for === 'Commands' && $lastGroup !== $group = $item->group()) {
203-
$this->writer->boldYellow($group ?: '*', true);
203+
$this->writer->help_group($group ?: '*', true);
204204
$lastGroup = $group;
205205
}
206206
$desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc($withDefault));
207207

208-
$this->writer->bold(' ' . str_pad($name, $padLen + $space));
209-
$this->writer->comment($desc, true);
208+
$this->writer->help_item(' ' . str_pad($name, $padLen + $space));
209+
$this->writer->help_description($desc, true);
210210
}
211211

212212
if ($footer) {
213-
$this->writer->eol()->yellow($footer, true);
213+
$this->writer->eol()->help_footer($footer, true);
214214
}
215215
}
216216

@@ -224,7 +224,7 @@ public function showUsage(string $usage): self
224224
$usage = str_replace('$0', $_SERVER['argv'][0] ?? '[cmd]', $usage);
225225

226226
if (!str_contains($usage, ' ## ')) {
227-
$this->writer->eol()->boldGreen('Usage Examples:', true)->colors($usage)->eol();
227+
$this->writer->eol()->help_category('Usage Examples:', true)->colors($usage)->eol();
228228

229229
return $this;
230230
}
@@ -241,7 +241,7 @@ public function showUsage(string $usage): self
241241
return str_pad('# ', $maxlen - array_shift($lines), ' ', STR_PAD_LEFT);
242242
}, $usage);
243243

244-
$this->writer->eol()->boldGreen('Usage Examples:', true)->colors($usage)->eol();
244+
$this->writer->eol()->help_category('Usage Examples:', true)->colors($usage)->eol();
245245

246246
return $this;
247247
}

src/IO/Interactor.php

Lines changed: 7 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -37,145 +37,6 @@
3737
* @license MIT
3838
*
3939
* @link https://github.com/adhocore/cli
40-
*
41-
* @method Writer bgBlack($text, $eol = false)
42-
* @method Writer bgBlue($text, $eol = false)
43-
* @method Writer bgCyan($text, $eol = false)
44-
* @method Writer bgGreen($text, $eol = false)
45-
* @method Writer bgPurple($text, $eol = false)
46-
* @method Writer bgRed($text, $eol = false)
47-
* @method Writer bgWhite($text, $eol = false)
48-
* @method Writer bgYellow($text, $eol = false)
49-
* @method Writer black($text, $eol = false)
50-
* @method Writer blackBgBlue($text, $eol = false)
51-
* @method Writer blackBgCyan($text, $eol = false)
52-
* @method Writer blackBgGreen($text, $eol = false)
53-
* @method Writer blackBgPurple($text, $eol = false)
54-
* @method Writer blackBgRed($text, $eol = false)
55-
* @method Writer blackBgWhite($text, $eol = false)
56-
* @method Writer blackBgYellow($text, $eol = false)
57-
* @method Writer blue($text, $eol = false)
58-
* @method Writer blueBgBlack($text, $eol = false)
59-
* @method Writer blueBgCyan($text, $eol = false)
60-
* @method Writer blueBgGreen($text, $eol = false)
61-
* @method Writer blueBgPurple($text, $eol = false)
62-
* @method Writer blueBgRed($text, $eol = false)
63-
* @method Writer blueBgWhite($text, $eol = false)
64-
* @method Writer blueBgYellow($text, $eol = false)
65-
* @method Writer bold($text, $eol = false)
66-
* @method Writer boldBlack($text, $eol = false)
67-
* @method Writer boldBlackBgBlue($text, $eol = false)
68-
* @method Writer boldBlackBgCyan($text, $eol = false)
69-
* @method Writer boldBlackBgGreen($text, $eol = false)
70-
* @method Writer boldBlackBgPurple($text, $eol = false)
71-
* @method Writer boldBlackBgRed($text, $eol = false)
72-
* @method Writer boldBlackBgWhite($text, $eol = false)
73-
* @method Writer boldBlackBgYellow($text, $eol = false)
74-
* @method Writer boldBlue($text, $eol = false)
75-
* @method Writer boldBlueBgBlack($text, $eol = false)
76-
* @method Writer boldBlueBgCyan($text, $eol = false)
77-
* @method Writer boldBlueBgGreen($text, $eol = false)
78-
* @method Writer boldBlueBgPurple($text, $eol = false)
79-
* @method Writer boldBlueBgRed($text, $eol = false)
80-
* @method Writer boldBlueBgWhite($text, $eol = false)
81-
* @method Writer boldBlueBgYellow($text, $eol = false)
82-
* @method Writer boldCyan($text, $eol = false)
83-
* @method Writer boldCyanBgBlack($text, $eol = false)
84-
* @method Writer boldCyanBgBlue($text, $eol = false)
85-
* @method Writer boldCyanBgGreen($text, $eol = false)
86-
* @method Writer boldCyanBgPurple($text, $eol = false)
87-
* @method Writer boldCyanBgRed($text, $eol = false)
88-
* @method Writer boldCyanBgWhite($text, $eol = false)
89-
* @method Writer boldCyanBgYellow($text, $eol = false)
90-
* @method Writer boldGreen($text, $eol = false)
91-
* @method Writer boldGreenBgBlack($text, $eol = false)
92-
* @method Writer boldGreenBgBlue($text, $eol = false)
93-
* @method Writer boldGreenBgCyan($text, $eol = false)
94-
* @method Writer boldGreenBgPurple($text, $eol = false)
95-
* @method Writer boldGreenBgRed($text, $eol = false)
96-
* @method Writer boldGreenBgWhite($text, $eol = false)
97-
* @method Writer boldGreenBgYellow($text, $eol = false)
98-
* @method Writer boldPurple($text, $eol = false)
99-
* @method Writer boldPurpleBgBlack($text, $eol = false)
100-
* @method Writer boldPurpleBgBlue($text, $eol = false)
101-
* @method Writer boldPurpleBgCyan($text, $eol = false)
102-
* @method Writer boldPurpleBgGreen($text, $eol = false)
103-
* @method Writer boldPurpleBgRed($text, $eol = false)
104-
* @method Writer boldPurpleBgWhite($text, $eol = false)
105-
* @method Writer boldPurpleBgYellow($text, $eol = false)
106-
* @method Writer boldRed($text, $eol = false)
107-
* @method Writer boldRedBgBlack($text, $eol = false)
108-
* @method Writer boldRedBgBlue($text, $eol = false)
109-
* @method Writer boldRedBgCyan($text, $eol = false)
110-
* @method Writer boldRedBgGreen($text, $eol = false)
111-
* @method Writer boldRedBgPurple($text, $eol = false)
112-
* @method Writer boldRedBgWhite($text, $eol = false)
113-
* @method Writer boldRedBgYellow($text, $eol = false)
114-
* @method Writer boldWhite($text, $eol = false)
115-
* @method Writer boldWhiteBgBlack($text, $eol = false)
116-
* @method Writer boldWhiteBgBlue($text, $eol = false)
117-
* @method Writer boldWhiteBgCyan($text, $eol = false)
118-
* @method Writer boldWhiteBgGreen($text, $eol = false)
119-
* @method Writer boldWhiteBgPurple($text, $eol = false)
120-
* @method Writer boldWhiteBgRed($text, $eol = false)
121-
* @method Writer boldWhiteBgYellow($text, $eol = false)
122-
* @method Writer boldYellow($text, $eol = false)
123-
* @method Writer boldYellowBgBlack($text, $eol = false)
124-
* @method Writer boldYellowBgBlue($text, $eol = false)
125-
* @method Writer boldYellowBgCyan($text, $eol = false)
126-
* @method Writer boldYellowBgGreen($text, $eol = false)
127-
* @method Writer boldYellowBgPurple($text, $eol = false)
128-
* @method Writer boldYellowBgRed($text, $eol = false)
129-
* @method Writer boldYellowBgWhite($text, $eol = false)
130-
* @method Writer colors($text)
131-
* @method Writer comment($text, $eol = false)
132-
* @method Writer cyan($text, $eol = false)
133-
* @method Writer cyanBgBlack($text, $eol = false)
134-
* @method Writer cyanBgBlue($text, $eol = false)
135-
* @method Writer cyanBgGreen($text, $eol = false)
136-
* @method Writer cyanBgPurple($text, $eol = false)
137-
* @method Writer cyanBgRed($text, $eol = false)
138-
* @method Writer cyanBgWhite($text, $eol = false)
139-
* @method Writer cyanBgYellow($text, $eol = false)
140-
* @method Writer eol(int $n = 1)
141-
* @method Writer error($text, $eol = false)
142-
* @method Writer green($text, $eol = false)
143-
* @method Writer greenBgBlack($text, $eol = false)
144-
* @method Writer greenBgBlue($text, $eol = false)
145-
* @method Writer greenBgCyan($text, $eol = false)
146-
* @method Writer greenBgPurple($text, $eol = false)
147-
* @method Writer greenBgRed($text, $eol = false)
148-
* @method Writer greenBgWhite($text, $eol = false)
149-
* @method Writer greenBgYellow($text, $eol = false)
150-
* @method Writer info($text, $eol = false)
151-
* @method Writer ok($text, $eol = false)
152-
* @method Writer purple($text, $eol = false)
153-
* @method Writer purpleBgBlack($text, $eol = false)
154-
* @method Writer purpleBgBlue($text, $eol = false)
155-
* @method Writer purpleBgCyan($text, $eol = false)
156-
* @method Writer purpleBgGreen($text, $eol = false)
157-
* @method Writer purpleBgRed($text, $eol = false)
158-
* @method Writer purpleBgWhite($text, $eol = false)
159-
* @method Writer purpleBgYellow($text, $eol = false)
160-
* @method Writer red($text, $eol = false)
161-
* @method Writer redBgBlack($text, $eol = false)
162-
* @method Writer redBgBlue($text, $eol = false)
163-
* @method Writer redBgCyan($text, $eol = false)
164-
* @method Writer redBgGreen($text, $eol = false)
165-
* @method Writer redBgPurple($text, $eol = false)
166-
* @method Writer redBgWhite($text, $eol = false)
167-
* @method Writer redBgYellow($text, $eol = false)
168-
* @method Writer table(array $rows, array $styles = [])
169-
* @method Writer warn($text, $eol = false)
170-
* @method Writer white($text, $eol = false)
171-
* @method Writer yellow($text, $eol = false)
172-
* @method Writer yellowBgBlack($text, $eol = false)
173-
* @method Writer yellowBgBlue($text, $eol = false)
174-
* @method Writer yellowBgCyan($text, $eol = false)
175-
* @method Writer yellowBgGreen($text, $eol = false)
176-
* @method Writer yellowBgPurple($text, $eol = false)
177-
* @method Writer yellowBgRed($text, $eol = false)
178-
* @method Writer yellowBgWhite($text, $eol = false)
17940
*/
18041
class Interactor
18142
{
@@ -241,7 +102,7 @@ public function confirm(string $text, string $default = 'y'): bool
241102
*/
242103
public function choice(string $text, array $choices, $default = null, bool $case = false): mixed
243104
{
244-
$this->writer->yellow($text);
105+
$this->writer->question($text);
245106

246107
$this->listOptions($choices, $default, false);
247108

@@ -262,7 +123,7 @@ public function choice(string $text, array $choices, $default = null, bool $case
262123
*/
263124
public function choices(string $text, array $choices, $default = null, bool $case = false): mixed
264125
{
265-
$this->writer->yellow($text);
126+
$this->writer->question($text);
266127

267128
$this->listOptions($choices, $default, true);
268129

@@ -300,7 +161,7 @@ public function prompt(string $text, $default = null, ?callable $fn = null, int
300161
$hidden = func_get_args()[4] ?? false;
301162
$readFn = ['read', 'readHidden'][(int) $hidden];
302163

303-
$this->writer->yellow($text)->comment(null !== $default ? " [$default]: " : ': ');
164+
$this->writer->question($text)->answer(null !== $default ? " [$default]: " : ': ');
304165

305166
try {
306167
$input = $this->reader->{$readFn}($default, $fn);
@@ -310,7 +171,7 @@ public function prompt(string $text, $default = null, ?callable $fn = null, int
310171
}
311172

312173
if ($retry > 0 && $input === '') {
313-
$this->writer->bgRed($error, true);
174+
$this->writer->error($error, true);
314175

315176
return $this->prompt($text, $default, $fn, $retry - 1, $hidden);
316177
}
@@ -351,12 +212,12 @@ protected function listOptions(array $choices, $default = null, bool $multi = fa
351212
$maxLen = max(array_map('strlen', array_keys($choices)));
352213

353214
foreach ($choices as $choice => $desc) {
354-
$this->writer->eol()->cyan(str_pad(" [$choice]", $maxLen + 6))->comment($desc);
215+
$this->writer->eol()->choice(str_pad(" [$choice]", $maxLen + 6))->answer($desc);
355216
}
356217

357218
$label = $multi ? 'Choices (comma separated)' : 'Choice';
358219

359-
$this->writer->eol()->yellow($label);
220+
$this->writer->eol()->question($label);
360221

361222
return $this->promptOptions(array_keys($choices), $default);
362223
}
@@ -369,7 +230,7 @@ protected function promptOptions(array $choices, mixed $default): self
369230
$options = '';
370231

371232
foreach ($choices as $choice) {
372-
$style = in_array($choice, (array) $default) ? 'boldCyan' : 'cyan';
233+
$style = in_array($choice, (array) $default) ? 'boldChoice' : 'choice';
373234
$options .= "/<$style>$choice</end>";
374235
}
375236

src/Input/Command.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ public function showHelp(): mixed
298298
$io = $this->io();
299299
$helper = new OutputHelper($io->writer());
300300

301-
$io->bold("Command {$this->_name}, version {$this->_version}", true)->eol();
302-
$io->comment($this->_desc, true)->eol();
303-
$io->bold('Usage: ')->yellow("{$this->_name} [OPTIONS...] [ARGUMENTS...]", true);
301+
$io->help_header("Command {$this->_name}, version {$this->_version}", true)->eol();
302+
$io->help_summary($this->_desc, true)->eol();
303+
$io->help_text('Usage: ')->help_example("{$this->_name} [OPTIONS...] [ARGUMENTS...]", true);
304304

305305
$helper
306306
->showArgumentsHelp($this->allArguments())
@@ -318,7 +318,7 @@ public function showHelp(): mixed
318318
*/
319319
public function showVersion(): mixed
320320
{
321-
$this->writer()->bold($this->_version, true);
321+
$this->writer()->version($this->_version, true);
322322

323323
return $this->emit('_exit', 0);
324324
}

0 commit comments

Comments
 (0)