comparison vendor/symfony/console/Formatter/OutputFormatterStyle.php @ 0:4c8ae668cc8c

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