Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console\Formatter;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Formatter style class for defining styles.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
20 */
|
Chris@0
|
21 class OutputFormatterStyle implements OutputFormatterStyleInterface
|
Chris@0
|
22 {
|
Chris@0
|
23 private static $availableForegroundColors = array(
|
Chris@0
|
24 'black' => array('set' => 30, 'unset' => 39),
|
Chris@0
|
25 'red' => array('set' => 31, 'unset' => 39),
|
Chris@0
|
26 'green' => array('set' => 32, 'unset' => 39),
|
Chris@0
|
27 'yellow' => array('set' => 33, 'unset' => 39),
|
Chris@0
|
28 'blue' => array('set' => 34, 'unset' => 39),
|
Chris@0
|
29 'magenta' => array('set' => 35, 'unset' => 39),
|
Chris@0
|
30 'cyan' => array('set' => 36, 'unset' => 39),
|
Chris@0
|
31 'white' => array('set' => 37, 'unset' => 39),
|
Chris@0
|
32 'default' => array('set' => 39, 'unset' => 39),
|
Chris@0
|
33 );
|
Chris@0
|
34 private static $availableBackgroundColors = array(
|
Chris@0
|
35 'black' => array('set' => 40, 'unset' => 49),
|
Chris@0
|
36 'red' => array('set' => 41, 'unset' => 49),
|
Chris@0
|
37 'green' => array('set' => 42, 'unset' => 49),
|
Chris@0
|
38 'yellow' => array('set' => 43, 'unset' => 49),
|
Chris@0
|
39 'blue' => array('set' => 44, 'unset' => 49),
|
Chris@0
|
40 'magenta' => array('set' => 45, 'unset' => 49),
|
Chris@0
|
41 'cyan' => array('set' => 46, 'unset' => 49),
|
Chris@0
|
42 'white' => array('set' => 47, 'unset' => 49),
|
Chris@0
|
43 'default' => array('set' => 49, 'unset' => 49),
|
Chris@0
|
44 );
|
Chris@0
|
45 private static $availableOptions = array(
|
Chris@0
|
46 'bold' => array('set' => 1, 'unset' => 22),
|
Chris@0
|
47 'underscore' => array('set' => 4, 'unset' => 24),
|
Chris@0
|
48 'blink' => array('set' => 5, 'unset' => 25),
|
Chris@0
|
49 'reverse' => array('set' => 7, 'unset' => 27),
|
Chris@0
|
50 'conceal' => array('set' => 8, 'unset' => 28),
|
Chris@0
|
51 );
|
Chris@0
|
52
|
Chris@0
|
53 private $foreground;
|
Chris@0
|
54 private $background;
|
Chris@0
|
55 private $options = array();
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Initializes output formatter style.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param string|null $foreground The style foreground color name
|
Chris@0
|
61 * @param string|null $background The style background color name
|
Chris@0
|
62 * @param array $options The style options
|
Chris@0
|
63 */
|
Chris@0
|
64 public function __construct($foreground = null, $background = null, array $options = array())
|
Chris@0
|
65 {
|
Chris@0
|
66 if (null !== $foreground) {
|
Chris@0
|
67 $this->setForeground($foreground);
|
Chris@0
|
68 }
|
Chris@0
|
69 if (null !== $background) {
|
Chris@0
|
70 $this->setBackground($background);
|
Chris@0
|
71 }
|
Chris@0
|
72 if (count($options)) {
|
Chris@0
|
73 $this->setOptions($options);
|
Chris@0
|
74 }
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Sets style foreground color.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @param string|null $color The color name
|
Chris@0
|
81 *
|
Chris@0
|
82 * @throws InvalidArgumentException When the color name isn't defined
|
Chris@0
|
83 */
|
Chris@0
|
84 public function setForeground($color = null)
|
Chris@0
|
85 {
|
Chris@0
|
86 if (null === $color) {
|
Chris@0
|
87 $this->foreground = null;
|
Chris@0
|
88
|
Chris@0
|
89 return;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 if (!isset(static::$availableForegroundColors[$color])) {
|
Chris@0
|
93 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
94 'Invalid foreground color specified: "%s". Expected one of (%s)',
|
Chris@0
|
95 $color,
|
Chris@0
|
96 implode(', ', array_keys(static::$availableForegroundColors))
|
Chris@0
|
97 ));
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 $this->foreground = static::$availableForegroundColors[$color];
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Sets style background color.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @param string|null $color The color name
|
Chris@0
|
107 *
|
Chris@0
|
108 * @throws InvalidArgumentException When the color name isn't defined
|
Chris@0
|
109 */
|
Chris@0
|
110 public function setBackground($color = null)
|
Chris@0
|
111 {
|
Chris@0
|
112 if (null === $color) {
|
Chris@0
|
113 $this->background = null;
|
Chris@0
|
114
|
Chris@0
|
115 return;
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 if (!isset(static::$availableBackgroundColors[$color])) {
|
Chris@0
|
119 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
120 'Invalid background color specified: "%s". Expected one of (%s)',
|
Chris@0
|
121 $color,
|
Chris@0
|
122 implode(', ', array_keys(static::$availableBackgroundColors))
|
Chris@0
|
123 ));
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 $this->background = static::$availableBackgroundColors[$color];
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 /**
|
Chris@0
|
130 * Sets some specific style option.
|
Chris@0
|
131 *
|
Chris@0
|
132 * @param string $option The option name
|
Chris@0
|
133 *
|
Chris@0
|
134 * @throws InvalidArgumentException When the option name isn't defined
|
Chris@0
|
135 */
|
Chris@0
|
136 public function setOption($option)
|
Chris@0
|
137 {
|
Chris@0
|
138 if (!isset(static::$availableOptions[$option])) {
|
Chris@0
|
139 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
140 'Invalid option specified: "%s". Expected one of (%s)',
|
Chris@0
|
141 $option,
|
Chris@0
|
142 implode(', ', array_keys(static::$availableOptions))
|
Chris@0
|
143 ));
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 if (!in_array(static::$availableOptions[$option], $this->options)) {
|
Chris@0
|
147 $this->options[] = static::$availableOptions[$option];
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * Unsets some specific style option.
|
Chris@0
|
153 *
|
Chris@0
|
154 * @param string $option The option name
|
Chris@0
|
155 *
|
Chris@0
|
156 * @throws InvalidArgumentException When the option name isn't defined
|
Chris@0
|
157 */
|
Chris@0
|
158 public function unsetOption($option)
|
Chris@0
|
159 {
|
Chris@0
|
160 if (!isset(static::$availableOptions[$option])) {
|
Chris@0
|
161 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
162 'Invalid option specified: "%s". Expected one of (%s)',
|
Chris@0
|
163 $option,
|
Chris@0
|
164 implode(', ', array_keys(static::$availableOptions))
|
Chris@0
|
165 ));
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 $pos = array_search(static::$availableOptions[$option], $this->options);
|
Chris@0
|
169 if (false !== $pos) {
|
Chris@0
|
170 unset($this->options[$pos]);
|
Chris@0
|
171 }
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 /**
|
Chris@0
|
175 * Sets multiple style options at once.
|
Chris@0
|
176 *
|
Chris@0
|
177 * @param array $options
|
Chris@0
|
178 */
|
Chris@0
|
179 public function setOptions(array $options)
|
Chris@0
|
180 {
|
Chris@0
|
181 $this->options = array();
|
Chris@0
|
182
|
Chris@0
|
183 foreach ($options as $option) {
|
Chris@0
|
184 $this->setOption($option);
|
Chris@0
|
185 }
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * Applies the style to a given text.
|
Chris@0
|
190 *
|
Chris@0
|
191 * @param string $text The text to style
|
Chris@0
|
192 *
|
Chris@0
|
193 * @return string
|
Chris@0
|
194 */
|
Chris@0
|
195 public function apply($text)
|
Chris@0
|
196 {
|
Chris@0
|
197 $setCodes = array();
|
Chris@0
|
198 $unsetCodes = array();
|
Chris@0
|
199
|
Chris@0
|
200 if (null !== $this->foreground) {
|
Chris@0
|
201 $setCodes[] = $this->foreground['set'];
|
Chris@0
|
202 $unsetCodes[] = $this->foreground['unset'];
|
Chris@0
|
203 }
|
Chris@0
|
204 if (null !== $this->background) {
|
Chris@0
|
205 $setCodes[] = $this->background['set'];
|
Chris@0
|
206 $unsetCodes[] = $this->background['unset'];
|
Chris@0
|
207 }
|
Chris@0
|
208 if (count($this->options)) {
|
Chris@0
|
209 foreach ($this->options as $option) {
|
Chris@0
|
210 $setCodes[] = $option['set'];
|
Chris@0
|
211 $unsetCodes[] = $option['unset'];
|
Chris@0
|
212 }
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 if (0 === count($setCodes)) {
|
Chris@0
|
216 return $text;
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
|
Chris@0
|
220 }
|
Chris@0
|
221 }
|