Skip to content

[Console] added ability to add custom colors #19844

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

Closed
Closed
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
60 changes: 60 additions & 0 deletions src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The color code -> The ANSI color code ?

* @param string|int $unset The unset color code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unset color code -> The ANSI code to unset the color ?

*
* @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}))
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be on one line

}

if ($unset === 0) {
$unset = static::${$property}['default']['set'];
}

static::${$property}[$name] = array(
'set' => $value,
'unset' => $unset,
);
}

/**
* Sets style foreground color.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}