4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Formatter;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
17 * Formatter style class for defining styles.
19 * @author Konstantin Kudryashov <ever.zet@gmail.com>
21 class OutputFormatterStyle implements OutputFormatterStyleInterface
23 private static $availableForegroundColors = array(
24 'black' => array('set' => 30, 'unset' => 39),
25 'red' => array('set' => 31, 'unset' => 39),
26 'green' => array('set' => 32, 'unset' => 39),
27 'yellow' => array('set' => 33, 'unset' => 39),
28 'blue' => array('set' => 34, 'unset' => 39),
29 'magenta' => array('set' => 35, 'unset' => 39),
30 'cyan' => array('set' => 36, 'unset' => 39),
31 'white' => array('set' => 37, 'unset' => 39),
32 'default' => array('set' => 39, 'unset' => 39),
34 private static $availableBackgroundColors = array(
35 'black' => array('set' => 40, 'unset' => 49),
36 'red' => array('set' => 41, 'unset' => 49),
37 'green' => array('set' => 42, 'unset' => 49),
38 'yellow' => array('set' => 43, 'unset' => 49),
39 'blue' => array('set' => 44, 'unset' => 49),
40 'magenta' => array('set' => 45, 'unset' => 49),
41 'cyan' => array('set' => 46, 'unset' => 49),
42 'white' => array('set' => 47, 'unset' => 49),
43 'default' => array('set' => 49, 'unset' => 49),
45 private static $availableOptions = array(
46 'bold' => array('set' => 1, 'unset' => 22),
47 'underscore' => array('set' => 4, 'unset' => 24),
48 'blink' => array('set' => 5, 'unset' => 25),
49 'reverse' => array('set' => 7, 'unset' => 27),
50 'conceal' => array('set' => 8, 'unset' => 28),
55 private $options = array();
58 * Initializes output formatter style.
60 * @param string|null $foreground The style foreground color name
61 * @param string|null $background The style background color name
62 * @param array $options The style options
64 public function __construct($foreground = null, $background = null, array $options = array())
66 if (null !== $foreground) {
67 $this->setForeground($foreground);
69 if (null !== $background) {
70 $this->setBackground($background);
72 if (\count($options)) {
73 $this->setOptions($options);
78 * Sets style foreground color.
80 * @param string|null $color The color name
82 * @throws InvalidArgumentException When the color name isn't defined
84 public function setForeground($color = null)
86 if (null === $color) {
87 $this->foreground = null;
92 if (!isset(static::$availableForegroundColors[$color])) {
93 throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors))));
96 $this->foreground = static::$availableForegroundColors[$color];
100 * Sets style background color.
102 * @param string|null $color The color name
104 * @throws InvalidArgumentException When the color name isn't defined
106 public function setBackground($color = null)
108 if (null === $color) {
109 $this->background = null;
114 if (!isset(static::$availableBackgroundColors[$color])) {
115 throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
118 $this->background = static::$availableBackgroundColors[$color];
122 * Sets some specific style option.
124 * @param string $option The option name
126 * @throws InvalidArgumentException When the option name isn't defined
128 public function setOption($option)
130 if (!isset(static::$availableOptions[$option])) {
131 throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
134 if (!\in_array(static::$availableOptions[$option], $this->options)) {
135 $this->options[] = static::$availableOptions[$option];
140 * Unsets some specific style option.
142 * @param string $option The option name
144 * @throws InvalidArgumentException When the option name isn't defined
146 public function unsetOption($option)
148 if (!isset(static::$availableOptions[$option])) {
149 throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
152 $pos = array_search(static::$availableOptions[$option], $this->options);
153 if (false !== $pos) {
154 unset($this->options[$pos]);
161 public function setOptions(array $options)
163 $this->options = array();
165 foreach ($options as $option) {
166 $this->setOption($option);
171 * Applies the style to a given text.
173 * @param string $text The text to style
177 public function apply($text)
180 $unsetCodes = array();
182 if (null !== $this->foreground) {
183 $setCodes[] = $this->foreground['set'];
184 $unsetCodes[] = $this->foreground['unset'];
186 if (null !== $this->background) {
187 $setCodes[] = $this->background['set'];
188 $unsetCodes[] = $this->background['unset'];
190 if (\count($this->options)) {
191 foreach ($this->options as $option) {
192 $setCodes[] = $option['set'];
193 $unsetCodes[] = $option['unset'];
197 if (0 === \count($setCodes)) {
201 return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));