Skip to content

Commit

Permalink
Merge pull request #110 from kodie/kodie/allow-256-colors
Browse files Browse the repository at this point in the history
Add support for outputting 256 colors and also add documentation about default command functionality
  • Loading branch information
adhocore authored Nov 26, 2024
2 parents fd794a7 + f4ead85 commit 86e02bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -450,7 +465,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 @@ -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 static function bg256(int $code): string
{
return "48;5;{$code}";
}

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

/**
* Returns a formatted/colored line.
*/
Expand All @@ -130,8 +146,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
8 changes: 4 additions & 4 deletions tests/Output/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 86e02bf

Please sign in to comment.