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(
94 'Invalid foreground color specified: "%s". Expected one of (%s)',
96 implode(', ', array_keys(static::$availableForegroundColors))
100 $this->foreground = static::$availableForegroundColors[$color];
104 * Sets style background color.
106 * @param string|null $color The color name
108 * @throws InvalidArgumentException When the color name isn't defined
110 public function setBackground($color = null)
112 if (null === $color) {
113 $this->background = null;
118 if (!isset(static::$availableBackgroundColors[$color])) {
119 throw new InvalidArgumentException(sprintf(
120 'Invalid background color specified: "%s". Expected one of (%s)',
122 implode(', ', array_keys(static::$availableBackgroundColors))
126 $this->background = static::$availableBackgroundColors[$color];
130 * Sets some specific style option.
132 * @param string $option The option name
134 * @throws InvalidArgumentException When the option name isn't defined
136 public function setOption($option)
138 if (!isset(static::$availableOptions[$option])) {
139 throw new InvalidArgumentException(sprintf(
140 'Invalid option specified: "%s". Expected one of (%s)',
142 implode(', ', array_keys(static::$availableOptions))
146 if (!in_array(static::$availableOptions[$option], $this->options)) {
147 $this->options[] = static::$availableOptions[$option];
152 * Unsets some specific style option.
154 * @param string $option The option name
156 * @throws InvalidArgumentException When the option name isn't defined
158 public function unsetOption($option)
160 if (!isset(static::$availableOptions[$option])) {
161 throw new InvalidArgumentException(sprintf(
162 'Invalid option specified: "%s". Expected one of (%s)',
164 implode(', ', array_keys(static::$availableOptions))
168 $pos = array_search(static::$availableOptions[$option], $this->options);
169 if (false !== $pos) {
170 unset($this->options[$pos]);
177 public function setOptions(array $options)
179 $this->options = array();
181 foreach ($options as $option) {
182 $this->setOption($option);
187 * Applies the style to a given text.
189 * @param string $text The text to style
193 public function apply($text)
196 $unsetCodes = array();
198 if (null !== $this->foreground) {
199 $setCodes[] = $this->foreground['set'];
200 $unsetCodes[] = $this->foreground['unset'];
202 if (null !== $this->background) {
203 $setCodes[] = $this->background['set'];
204 $unsetCodes[] = $this->background['unset'];
206 if (count($this->options)) {
207 foreach ($this->options as $option) {
208 $setCodes[] = $option['set'];
209 $unsetCodes[] = $option['unset'];
213 if (0 === count($setCodes)) {
217 return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));