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@17
|
23 private static $availableForegroundColors = [
|
Chris@17
|
24 'black' => ['set' => 30, 'unset' => 39],
|
Chris@17
|
25 'red' => ['set' => 31, 'unset' => 39],
|
Chris@17
|
26 'green' => ['set' => 32, 'unset' => 39],
|
Chris@17
|
27 'yellow' => ['set' => 33, 'unset' => 39],
|
Chris@17
|
28 'blue' => ['set' => 34, 'unset' => 39],
|
Chris@17
|
29 'magenta' => ['set' => 35, 'unset' => 39],
|
Chris@17
|
30 'cyan' => ['set' => 36, 'unset' => 39],
|
Chris@17
|
31 'white' => ['set' => 37, 'unset' => 39],
|
Chris@17
|
32 'default' => ['set' => 39, 'unset' => 39],
|
Chris@17
|
33 ];
|
Chris@17
|
34 private static $availableBackgroundColors = [
|
Chris@17
|
35 'black' => ['set' => 40, 'unset' => 49],
|
Chris@17
|
36 'red' => ['set' => 41, 'unset' => 49],
|
Chris@17
|
37 'green' => ['set' => 42, 'unset' => 49],
|
Chris@17
|
38 'yellow' => ['set' => 43, 'unset' => 49],
|
Chris@17
|
39 'blue' => ['set' => 44, 'unset' => 49],
|
Chris@17
|
40 'magenta' => ['set' => 45, 'unset' => 49],
|
Chris@17
|
41 'cyan' => ['set' => 46, 'unset' => 49],
|
Chris@17
|
42 'white' => ['set' => 47, 'unset' => 49],
|
Chris@17
|
43 'default' => ['set' => 49, 'unset' => 49],
|
Chris@17
|
44 ];
|
Chris@17
|
45 private static $availableOptions = [
|
Chris@17
|
46 'bold' => ['set' => 1, 'unset' => 22],
|
Chris@17
|
47 'underscore' => ['set' => 4, 'unset' => 24],
|
Chris@17
|
48 'blink' => ['set' => 5, 'unset' => 25],
|
Chris@17
|
49 'reverse' => ['set' => 7, 'unset' => 27],
|
Chris@17
|
50 'conceal' => ['set' => 8, 'unset' => 28],
|
Chris@17
|
51 ];
|
Chris@0
|
52
|
Chris@0
|
53 private $foreground;
|
Chris@0
|
54 private $background;
|
Chris@17
|
55 private $options = [];
|
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@17
|
64 public function __construct($foreground = null, $background = null, array $options = [])
|
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@17
|
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@17
|
93 throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors))));
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 $this->foreground = static::$availableForegroundColors[$color];
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Sets style background color.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @param string|null $color The color name
|
Chris@0
|
103 *
|
Chris@0
|
104 * @throws InvalidArgumentException When the color name isn't defined
|
Chris@0
|
105 */
|
Chris@0
|
106 public function setBackground($color = null)
|
Chris@0
|
107 {
|
Chris@0
|
108 if (null === $color) {
|
Chris@0
|
109 $this->background = null;
|
Chris@0
|
110
|
Chris@0
|
111 return;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 if (!isset(static::$availableBackgroundColors[$color])) {
|
Chris@17
|
115 throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 $this->background = static::$availableBackgroundColors[$color];
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * Sets some specific style option.
|
Chris@0
|
123 *
|
Chris@0
|
124 * @param string $option The option name
|
Chris@0
|
125 *
|
Chris@0
|
126 * @throws InvalidArgumentException When the option name isn't defined
|
Chris@0
|
127 */
|
Chris@0
|
128 public function setOption($option)
|
Chris@0
|
129 {
|
Chris@0
|
130 if (!isset(static::$availableOptions[$option])) {
|
Chris@17
|
131 throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@17
|
134 if (!\in_array(static::$availableOptions[$option], $this->options)) {
|
Chris@0
|
135 $this->options[] = static::$availableOptions[$option];
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Unsets some specific style option.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @param string $option The option name
|
Chris@0
|
143 *
|
Chris@0
|
144 * @throws InvalidArgumentException When the option name isn't defined
|
Chris@0
|
145 */
|
Chris@0
|
146 public function unsetOption($option)
|
Chris@0
|
147 {
|
Chris@0
|
148 if (!isset(static::$availableOptions[$option])) {
|
Chris@17
|
149 throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 $pos = array_search(static::$availableOptions[$option], $this->options);
|
Chris@0
|
153 if (false !== $pos) {
|
Chris@0
|
154 unset($this->options[$pos]);
|
Chris@0
|
155 }
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@14
|
159 * {@inheritdoc}
|
Chris@0
|
160 */
|
Chris@0
|
161 public function setOptions(array $options)
|
Chris@0
|
162 {
|
Chris@17
|
163 $this->options = [];
|
Chris@0
|
164
|
Chris@0
|
165 foreach ($options as $option) {
|
Chris@0
|
166 $this->setOption($option);
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * Applies the style to a given text.
|
Chris@0
|
172 *
|
Chris@0
|
173 * @param string $text The text to style
|
Chris@0
|
174 *
|
Chris@0
|
175 * @return string
|
Chris@0
|
176 */
|
Chris@0
|
177 public function apply($text)
|
Chris@0
|
178 {
|
Chris@17
|
179 $setCodes = [];
|
Chris@17
|
180 $unsetCodes = [];
|
Chris@0
|
181
|
Chris@0
|
182 if (null !== $this->foreground) {
|
Chris@0
|
183 $setCodes[] = $this->foreground['set'];
|
Chris@0
|
184 $unsetCodes[] = $this->foreground['unset'];
|
Chris@0
|
185 }
|
Chris@0
|
186 if (null !== $this->background) {
|
Chris@0
|
187 $setCodes[] = $this->background['set'];
|
Chris@0
|
188 $unsetCodes[] = $this->background['unset'];
|
Chris@0
|
189 }
|
Chris@17
|
190 if (\count($this->options)) {
|
Chris@0
|
191 foreach ($this->options as $option) {
|
Chris@0
|
192 $setCodes[] = $option['set'];
|
Chris@0
|
193 $unsetCodes[] = $option['unset'];
|
Chris@0
|
194 }
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@17
|
197 if (0 === \count($setCodes)) {
|
Chris@0
|
198 return $text;
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
|
Chris@0
|
202 }
|
Chris@0
|
203 }
|