Skip to content

[Console] Add bright colors to console. #39976

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

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Add the `Command::$defaultDescription` static property and the `description` attribute
on the `console.command` tag to allow the `list` command to instantiate commands lazily
* Add option `--short` to the `list` command
* Add support for bright colors

5.2.0
-----
Expand Down
31 changes: 23 additions & 8 deletions src/Symfony/Component/Console/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ final class Color
'default' => 9,
];

private const BRIGHT_COLORS = [
'gray' => 0,
'bright-red' => 1,
'bright-green' => 2,
'bright-yellow' => 3,
'bright-blue' => 4,
'bright-magenta' => 5,
'bright-cyan' => 6,
'bright-white' => 7,
];

private const AVAILABLE_OPTIONS = [
'bold' => ['set' => 1, 'unset' => 22],
'underscore' => ['set' => 4, 'unset' => 24],
Expand All @@ -45,7 +56,7 @@ final class Color
public function __construct(string $foreground = '', string $background = '', array $options = [])
{
$this->foreground = $this->parseColor($foreground);
$this->background = $this->parseColor($background);
$this->background = $this->parseColor($background, true);

foreach ($options as $option) {
if (!isset(self::AVAILABLE_OPTIONS[$option])) {
Expand All @@ -65,10 +76,10 @@ public function set(): string
{
$setCodes = [];
if ('' !== $this->foreground) {
$setCodes[] = '3'.$this->foreground;
$setCodes[] = $this->foreground;
}
if ('' !== $this->background) {
$setCodes[] = '4'.$this->background;
$setCodes[] = $this->background;
}
foreach ($this->options as $option) {
$setCodes[] = $option['set'];
Expand Down Expand Up @@ -99,7 +110,7 @@ public function unset(): string
return sprintf("\033[%sm", implode(';', $unsetCodes));
}

private function parseColor(string $color): string
private function parseColor(string $color, bool $background = false): string
{
if ('' === $color) {
return '';
Expand All @@ -116,14 +127,18 @@ private function parseColor(string $color): string
throw new InvalidArgumentException(sprintf('Invalid "%s" color.', $color));
}

return $this->convertHexColorToAnsi(hexdec($color));
return ($background ? '4' : '3').$this->convertHexColorToAnsi(hexdec($color));
}

if (isset(self::COLORS[$color])) {
return ($background ? '4' : '3').self::COLORS[$color];
}

if (!isset(self::COLORS[$color])) {
throw new InvalidArgumentException(sprintf('Invalid "%s" color; expected one of (%s).', $color, implode(', ', array_keys(self::COLORS))));
if (isset(self::BRIGHT_COLORS[$color])) {
return ($background ? '10' : '9').self::BRIGHT_COLORS[$color];
}

return (string) self::COLORS[$color];
throw new InvalidArgumentException(sprintf('Invalid "%s" color; expected one of (%s).', $color, implode(', ', array_merge(array_keys(self::COLORS), array_keys(self::BRIGHT_COLORS)))));
}

private function convertHexColorToAnsi(int $color): string
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Console/Tests/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function testAnsiColors()
$color = new Color('red', 'yellow');
$this->assertSame("\033[31;43m \033[39;49m", $color->apply(' '));

$color = new Color('bright-red', 'bright-yellow');
$this->assertSame("\033[91;103m \033[39;49m", $color->apply(' '));

$color = new Color('red', 'yellow', ['underscore']);
$this->assertSame("\033[31;43;4m \033[39;49;24m", $color->apply(' '));
}
Expand Down