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;
15 * Formatter style class for defining styles.
17 * @author Konstantin Kudryashov <ever.zet@gmail.com>
21 class OutputFormatterStyle implements OutputFormatterStyleInterface
23 private static $availableForegroundColors = array(
33 private static $availableBackgroundColors = array(
43 private static $availableOptions = array(
53 private $options = array();
56 * Initializes output formatter style.
58 * @param string $foreground The style foreground color name
59 * @param string $background The style background color name
60 * @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 $color The color name
82 * @throws \InvalidArgumentException When the color name isn't defined
86 public function setForeground($color = null)
88 if (null === $color) {
89 $this->foreground = null;
94 if (!isset(static::$availableForegroundColors[$color])) {
95 throw new \InvalidArgumentException(sprintf(
96 'Invalid foreground color specified: "%s". Expected one of (%s)',
98 implode(', ', array_keys(static::$availableForegroundColors))
102 $this->foreground = static::$availableForegroundColors[$color];
106 * Sets style background color.
108 * @param string $color The color name
110 * @throws \InvalidArgumentException When the color name isn't defined
114 public function setBackground($color = null)
116 if (null === $color) {
117 $this->background = null;
122 if (!isset(static::$availableBackgroundColors[$color])) {
123 throw new \InvalidArgumentException(sprintf(
124 'Invalid background color specified: "%s". Expected one of (%s)',
126 implode(', ', array_keys(static::$availableBackgroundColors))
130 $this->background = static::$availableBackgroundColors[$color];
134 * Sets some specific style option.
136 * @param string $option The option name
138 * @throws \InvalidArgumentException When the option name isn't defined
142 public function setOption($option)
144 if (!isset(static::$availableOptions[$option])) {
145 throw new \InvalidArgumentException(sprintf(
146 'Invalid option specified: "%s". Expected one of (%s)',
148 implode(', ', array_keys(static::$availableOptions))
152 if (false === array_search(static::$availableOptions[$option], $this->options)) {
153 $this->options[] = static::$availableOptions[$option];
158 * Unsets some specific style option.
160 * @param string $option The option name
162 * @throws \InvalidArgumentException When the option name isn't defined
165 public function unsetOption($option)
167 if (!isset(static::$availableOptions[$option])) {
168 throw new \InvalidArgumentException(sprintf(
169 'Invalid option specified: "%s". Expected one of (%s)',
171 implode(', ', array_keys(static::$availableOptions))
175 $pos = array_search(static::$availableOptions[$option], $this->options);
176 if (false !== $pos) {
177 unset($this->options[$pos]);
182 * Sets multiple style options at once.
184 * @param array $options
186 public function setOptions(array $options)
188 $this->options = array();
190 foreach ($options as $option) {
191 $this->setOption($option);
196 * Applies the style to a given text.
198 * @param string $text The text to style
202 public function apply($text)
206 if (null !== $this->foreground) {
207 $codes[] = $this->foreground;
209 if (null !== $this->background) {
210 $codes[] = $this->background;
212 if (count($this->options)) {
213 $codes = array_merge($codes, $this->options);
216 if (0 === count($codes)) {
220 return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);