diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index cf4610f2f35c9..aa2b0aaedacd2 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Console/Color.php b/src/Symfony/Component/Console/Color.php index b45f4523b9d25..22a4ce9ffbbb9 100644 --- a/src/Symfony/Component/Console/Color.php +++ b/src/Symfony/Component/Console/Color.php @@ -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], @@ -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])) { @@ -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']; @@ -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 ''; @@ -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 diff --git a/src/Symfony/Component/Console/Tests/ColorTest.php b/src/Symfony/Component/Console/Tests/ColorTest.php index 571963cfce788..c9615aa8d6133 100644 --- a/src/Symfony/Component/Console/Tests/ColorTest.php +++ b/src/Symfony/Component/Console/Tests/ColorTest.php @@ -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(' ')); }