Skip to content

Commit 86e02bf

Browse files
authored
Merge pull request #110 from kodie/kodie/allow-256-colors
Add support for outputting 256 colors and also add documentation about default command functionality
2 parents fd794a7 + f4ead85 commit 86e02bf

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,21 @@ $app->add((new ConfigListCommand)->inGroup('Config'));
274274
...
275275
```
276276

277+
#### Default command
278+
279+
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.
280+
281+
```php
282+
$app->add(new InitCommand, 'i', true);
283+
284+
// Alternatively
285+
$app->command('init', 'Init something', 'i');
286+
$app->defaultCommand('init');
287+
288+
// Retrieve the name of the default command
289+
$default_command = $app->getDefaultCommand();
290+
```
291+
277292
#### Exception handler
278293

279294
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.
@@ -450,7 +465,7 @@ echo $color->ok('This is ok msg');
450465
```php
451466
Ahc\Cli\Output\Color::style('mystyle', [
452467
'bg' => Ahc\Cli\Output\Color::CYAN,
453-
'fg' => Ahc\Cli\Output\Color::WHITE,
468+
'fg' => Ahc\Cli\Output\Color::fg256(57), // 256 colors can be used as well
454469
'bold' => 1, // You can experiment with 0, 1, 2, 3 ... as well
455470
]);
456471

src/Output/Color.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ public function info(string $text, array $style = []): string
117117
return $this->line($text, static::$styles['info'] + $style);
118118
}
119119

120+
/**
121+
* Returns the color code for a 256 background color.
122+
*/
123+
public static function bg256(int $code): string
124+
{
125+
return "48;5;{$code}";
126+
}
127+
128+
/**
129+
* Returns the color code for a 256 foreground color.
130+
*/
131+
public static function fg256(int $code): string
132+
{
133+
return "38;5;{$code}";
134+
}
135+
120136
/**
121137
* Returns a formatted/colored line.
122138
*/
@@ -130,8 +146,8 @@ public function line(string $text, array $style = []): string
130146

131147
$line = strtr($format, [
132148
':mod:' => (int) ($style['mod'] ?? $style['bold']),
133-
':fg:' => (int) $style['fg'],
134-
':bg:' => (int) $style['bg'] + 10,
149+
':fg:' => $style['fg'],
150+
':bg:' => is_int($style['bg']) ? ($style['bg'] + 10) : $style['bg'],
135151
':txt:' => $text,
136152
]);
137153

tests/Output/ColorTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function test_comment()
3030

3131
public function test_custom_style()
3232
{
33-
Color::style('alert', ['bg' => Color::YELLOW, 'fg' => Color::RED]);
33+
Color::style('alert', ['bg' => '48;5;82', 'fg' => '38;5;57']);
3434

35-
$this->assertSame("\033[0;31;43malert\033[0m", (new Color)->alert('alert'));
36-
$this->assertSame("\033[1;31;43malert\033[0m", (new Color)->boldAlert('alert'));
37-
$this->assertSame("\033[1;31;43malert\033[0m", (new Color)->alertBold('alert'));
35+
$this->assertSame("\033[0;38;5;57;48;5;82malert\033[0m", (new Color)->alert('alert'));
36+
$this->assertSame("\033[1;38;5;57;48;5;82malert\033[0m", (new Color)->boldAlert('alert'));
37+
$this->assertSame("\033[1;38;5;57;48;5;82malert\033[0m", (new Color)->alertBold('alert'));
3838
}
3939

4040
public function test_invalid_custom_style()

0 commit comments

Comments
 (0)