8000 feature #39976 [Console] Add bright colors to console. (CupOfTea696) · symfony/symfony@0f4c905 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f4c905

Browse files
committed
feature #39976 [Console] Add bright colors to console. (CupOfTea696)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Console] Add bright colors to console. | Q | A | ------------- | --- | Branch? | 5.x <!-- see below --> | Bug fix? |no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? |no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #39869 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#14884 <!-- required for new features --> Add the "bright" ANSI colours to symfony/console. This adds ANSI escape codes 90-97 and 100-107. <!-- Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. --> Commits ------- dbb9452 [Console] Add bright colors to console.
2 parents 02cbb3a + dbb9452 commit 0f4c905

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Add the `Command::$defaultDescription` static property and the `description` attribute
1010
on the `console.command` tag to allow the `list` command to instantiate commands lazily
1111
* Add option `--short` to the `list` command
12+
* Add support for bright colors
1213

1314
5.2.0
1415
-----

src/Symfony/Component/Console/Color.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ final class Color
3030
'default' => 9,
3131
];
3232

33+
private const BRIGHT_COLORS = [
34+
'gray' => 0,
35+
'bright-red' => 1,
36+
'bright-green' => 2,
37+
'bright-yellow' => 3,
38+
'bright-blue' => 4,
39+
'bright-magenta' => 5,
40+
'bright-cyan' => 6,
41+
'bright-white' => 7,
42+
];
43+
3344
private const AVAILABLE_OPTIONS = [
3445
'bold' => ['set' => 1, 'unset' => 22],
3546
'underscore' => ['set' => 4, 'unset' => 24],
@@ -45,7 +56,7 @@ final class Color
4556
public function __construct(string $foreground = '', string $background = '', array $options = [])
4657
{
4758
$this->foreground = $this->parseColor($foreground);
48-
$this->background = $this->parseColor($background);
59+
$this->background = $this->parseColor($background, true);
4960

5061
foreach ($options as $option) {
5162
if (!isset(self::AVAILABLE_OPTIONS[$option])) {
@@ -65,10 +76,10 @@ public function set(): string
6576
{
6677
$setCodes = [];
6778
if ('' !== $this->foreground) {
68-
$setCodes[] = '3'.$this->foreground;
79+
$setCodes[] = $this->foreground;
6980
}
7081
if ('' !== $this->background) {
71-
$setCodes[] = '4'.$this->background;
82+
$setCodes[] = $this->background;
7283
}
7384
foreach ($this->options as $option) {
7485
$setCodes[] = $option['set'];
@@ -99,7 +110,7 @@ public function unset(): string
99110
return sprintf("\033[%sm", implode(';', $unsetCodes));
100111
}
101112

102-
private function parseColor(string $color): string
113+
private function parseColor(string $color, bool $background = false): string
103114
{
104115
if ('' === $color) {
105116
return '';
@@ -116,14 +127,18 @@ private function parseColor(string $color): string
116127
throw new InvalidArgumentException(sprintf('Invalid "%s" color.', $color));
117128
}
118129

119-
return $this->convertHexColorToAnsi(hexdec($color));
130+
return ($background ? '4' : '3').$this->convertHexColorToAnsi(hexdec($color));
131+
}
132+
133+
if (isset(self::COLORS[$color])) {
134+
return ($background ? '4' : '3').self::COLORS[$color];
120135
}
121136

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

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

129144
private function convertHexColorToAnsi(int $color): string

src/Symfony/Component/Console/Tests/ColorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public function testAnsiColors()
2424
$color = new Color('red', 'yellow');
2525
$this->assertSame("\033[31;43m \033[39;49m", $color->apply(' '));
2626

27+
$color = new Color('bright-red', 'bright-yellow');
28+
$this->assertSame("\033[91;103m \033[39;49m", $color->apply(' '));
29+
2730
$color = new Color('red', 'yellow', ['underscore']);
2831
$this->assertSame("\033[31;43;4m \033[39;49;24m", $color->apply(' '));
2932
}

0 commit comments

Comments
 (0)
0