Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for outputting 256 colors and also add documentation about default command functionality #110

Merged
merged 61 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
c354406
Add support for 256 colors
kodie Nov 25, 2024
e59800f
Add documentation about default command functionality
kodie Nov 25, 2024
57b0ddc
Use 256 colors in test_custom_style test
kodie Nov 25, 2024
3099286
Add Color::bg256 and Color::fg256
kodie Nov 26, 2024
d9c947c
Add periods to bg256 and fg256 descriptions
kodie Nov 26, 2024
d69720e
Add mising A to fg256 example in readme
kodie Nov 26, 2024
9d758dc
Add return types for bg256 and fg256 functions
kodie Nov 26, 2024
6e08fdb
Make bg256 and fg256 functions static
kodie Nov 26, 2024
7bca2a3
Use PHP comments in Default command example in readme instead of Bash…
kodie Nov 26, 2024
1d306be
Make all written text built-in styles so that their styles can be cus…
kodie Nov 25, 2024
6c1c1ae
Put back Interactor Writer method definitions
kodie Nov 25, 2024
4753bdb
Put back eol and table method definitions for Interactor
kodie Nov 25, 2024
eff6243
Put this blank line back? I'm not sure how that happened
kodie Nov 25, 2024
1b7c236
Remove un-needed space
kodie Nov 25, 2024
9bdeea2
Hopefully fix issue with test
kodie Nov 25, 2024
c85ad40
Put factory color functions back
kodie Nov 26, 2024
6574e93
Prevent users from making factory functions invisible
kodie Nov 26, 2024
bc39541
Shorten a couple lines
kodie Nov 26, 2024
8a8dba0
Update invisible_built_in_style expected exception message
kodie Nov 26, 2024
52c02c2
Add new method definitions for Writer and Interactor classes
kodie Nov 26, 2024
3c04803
Make logo a built-in style
kodie Nov 26, 2024
a1cd34f
Make help_description into help_description_even/odd and make help_it…
kodie Nov 26, 2024
2bd83f4
Fix indentation
kodie Nov 26, 2024
9604577
Add support for custom help screens
kodie Nov 26, 2024
e661fd5
Fix identation
kodie Nov 26, 2024
1053704
Hopefully fix issues with tests
kodie Nov 26, 2024
f0ac531
Tests don't like variables starting with underscores in the application
kodie Nov 26, 2024
0439020
Add example to readme showing how you can get colors working in a cus…
kodie Nov 26, 2024
639aeb5
Just pass the colorizer as the help parameter in the custom help read…
kodie Nov 26, 2024
fd794a7
Rename custom_help to help
kodie Nov 26, 2024
a6d6be1
Make all written text built-in styles so that their styles can be cus…
kodie Nov 25, 2024
9e32278
Put back Interactor Writer method definitions
kodie Nov 25, 2024
84c19f8
Put back eol and table method definitions for Interactor
kodie Nov 25, 2024
2c908c4
Put this blank line back? I'm not sure how that happened
kodie Nov 25, 2024
360eb14
Remove un-needed space
kodie Nov 25, 2024
f852324
Hopefully fix issue with test
kodie Nov 25, 2024
0bb1d31
Put factory color functions back
kodie Nov 26, 2024
346c0f7
Prevent users from making factory functions invisible
kodie Nov 26, 2024
274abbb
Shorten a couple lines
kodie Nov 26, 2024
0fdd4e6
Update invisible_built_in_style expected exception message
kodie Nov 26, 2024
f2d8922
Add new method definitions for Writer and Interactor classes
kodie Nov 26, 2024
8ea5a65
Make logo a built-in style
kodie Nov 26, 2024
a128794
Make help_description into help_description_even/odd and make help_it…
kodie Nov 26, 2024
d3e7e76
Fix indentation
kodie Nov 26, 2024
2c9858c
Add support for custom help screens
kodie Nov 26, 2024
87f723d
Fix identation
kodie Nov 26, 2024
34dd521
Hopefully fix issues with tests
kodie Nov 26, 2024
88b224e
Tests don't like variables starting with underscores in the application
kodie Nov 26, 2024
ef29727
Add example to readme showing how you can get colors working in a cus…
kodie Nov 26, 2024
86ecf30
Just pass the colorizer as the help parameter in the custom help read…
kodie Nov 26, 2024
bc70bff
Rename custom_help to help
kodie Nov 26, 2024
21d49f7
Add support for 256 colors
kodie Nov 25, 2024
96c51fe
Add documentation about default command functionality
kodie Nov 25, 2024
5fc43fb
Use 256 colors in test_custom_style test
kodie Nov 25, 2024
45ae4cd
Add Color::bg256 and Color::fg256
kodie Nov 26, 2024
8f8f9c4
Add periods to bg256 and fg256 descriptions
kodie Nov 26, 2024
e45892f
Add mising A to fg256 example in readme
kodie Nov 26, 2024
2e9d028
Add return types for bg256 and fg256 functions
kodie Nov 26, 2024
d608baa
Make bg256 and fg256 functions static
kodie Nov 26, 2024
65caea5
Use PHP comments in Default command example in readme instead of Bash…
kodie Nov 26, 2024
f4ead85
Merge branch 'kodie/allow-256-colors' of github.com:kodie/adhocore-ph…
kodie Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -443,7 +458,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' => 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
]);

Expand Down
20 changes: 18 additions & 2 deletions src/Output/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -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): string
{
return "48;5;{$code}";
}

/**
* Returns the color code for a 256 foreground color.
*/
public function fg256(int $code): string
{
return "38;5;{$code}";
}

/**
* Returns a formatted/colored line.
*/
Expand All @@ -108,8 +124,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,
]);

Expand Down
4 changes: 2 additions & 2 deletions tests/Output/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading