From 2839e3271a5bf97b4fb4e4f541a82d912edb7300 Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Sun, 4 Sep 2016 12:04:15 +0200 Subject: [PATCH] [Console] added ability to add custom colors --- .../Formatter/OutputFormatterStyle.php | 60 +++++++++++++++++++ .../Formatter/OutputFormatterStyleTest.php | 47 +++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php b/src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php index c7c6b4a019198..c555bf55f48f7 100644 --- a/src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php +++ b/src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php @@ -74,6 +74,66 @@ public function __construct($foreground = null, $background = null, array $optio } } + /** + * Add additional foreground color option. + * + * @param string $name The color name + * @param string|int $color The color code + * @param string|int $unset The unset color code + * + * @throws InvalidArgumentException When the color name is already defined + */ + public static function addForegroundColor($name, $value, $unset = 0) + { + static::addColor('foreground', $name, (int) $value, (int) $unset); + } + + /** + * Add additional background color option. + * + * @param string $name The color name + * @param string|int $color The color code + * @param string|int $unset The unset color code + * + * @throws InvalidArgumentException When the color name is already defined + */ + public static function addBackgroundColor($name, $value, $unset = 0) + { + static::addColor('background', $name, (int) $value, (int) $unset); + } + + /** + * Add additional color option. + * + * @param string $color The color type + * @param int $color The color code + * @param int $unset The unset color code + * + * @throws InvalidArgumentException When the color name is already defined + */ + private static function addColor($type, $name, $value, $unset) + { + $property = 'available'.ucfirst($type).'Colors'; + + if (isset(static::${$property}[$name])) { + throw new InvalidArgumentException(sprintf( + 'The specified %s color "%s" is already defined. Expected a different name than (%s)', + $type, + $name, + implode(', ', array_keys(static::${$property})) + )); + } + + if ($unset === 0) { + $unset = static::${$property}['default']['set']; + } + + static::${$property}[$name] = array( + 'set' => $value, + 'unset' => $unset, + ); + } + /** * Sets style foreground color. * diff --git a/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterStyleTest.php b/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterStyleTest.php index 0abfb3ce27559..9835554d56642 100644 --- a/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterStyleTest.php +++ b/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterStyleTest.php @@ -96,4 +96,51 @@ public function testOptions() $this->assertContains('Invalid option specified: "foo"', $e->getMessage(), '->unsetOption() throws an \InvalidArgumentException when the option does not exist in the available options'); } } + + public function testAddColorForeground() + { + OutputFormatterStyle::addForegroundColor('light_gray', 37); + OutputFormatterStyle::addForegroundColor('dark_gray', 90, 91); + $style = new OutputFormatterStyle(); + + $style->setForeground('black'); + $this->assertEquals("\033[30mfoo\033[39m", $style->apply('foo')); + + $style->setForeground('light_gray'); + $this->assertEquals("\033[37mfoo\033[39m", $style->apply('foo')); + + $style->setForeground('dark_gray'); + $this->assertEquals("\033[90mfoo\033[91m", $style->apply('foo')); + + $this->setExpectedException('InvalidArgumentException'); + $style->setBackground('undefined-color'); + } + + public function testAddColorBackground() + { + OutputFormatterStyle::addBackgroundColor('light_gray', 47); + OutputFormatterStyle::addBackgroundColor('dark_gray', 100, 101); + $style = new OutputFormatterStyle(); + + $style->setBackground('black'); + $this->assertEquals("\033[40mfoo\033[49m", $style->apply('foo')); + + $style->setBackground('light_gray'); + $this->assertEquals("\033[47mfoo\033[49m", $style->apply('foo')); + + $style->setBackground('dark_gray'); + $this->assertEquals("\033[100mfoo\033[101m", $style->apply('foo')); + + $this->setExpectedException('InvalidArgumentException'); + $style->setBackground('undefined-color'); + } + + public function testAddColorExisting() + { + $this->setExpectedException('InvalidArgumentException'); + OutputFormatterStyle::addForegroundColor('cyan', 60); + + $this->setExpectedException('InvalidArgumentException'); + OutputFormatterStyle::addBackgroundColor('cyan', 70); + } }