1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colors; |
4
|
|
|
|
5
|
|
|
class Color |
6
|
|
|
{ |
7
|
|
|
const FORMAT_PATTERN = '#<([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)>(.*?)</\\1?>#s'; |
8
|
|
|
/** @link http://www.php.net/manual/en/functions.user-defined.php */ |
9
|
|
|
const STYLE_NAME_PATTERN = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; |
10
|
|
|
|
11
|
|
|
const ESC = "\033["; |
12
|
|
|
const ESC_SEQ_PATTERN = "\033[%sm"; |
13
|
|
|
|
14
|
|
|
protected $initial = ''; |
15
|
|
|
protected $wrapped = ''; |
16
|
|
|
// italic and blink may not work depending of your terminal |
17
|
|
|
protected $styles = array( |
18
|
|
|
'reset' => '0', |
19
|
|
|
'bold' => '1', |
20
|
|
|
'dark' => '2', |
21
|
|
|
'italic' => '3', |
22
|
|
|
'underline' => '4', |
23
|
|
|
'blink' => '5', |
24
|
|
|
'reverse' => '7', |
25
|
|
|
'concealed' => '8', |
26
|
|
|
|
27
|
|
|
'default' => '39', |
28
|
|
|
'black' => '30', |
29
|
|
|
'red' => '31', |
30
|
|
|
'green' => '32', |
31
|
|
|
'yellow' => '33', |
32
|
|
|
'blue' => '34', |
33
|
|
|
'magenta' => '35', |
34
|
|
|
'cyan' => '36', |
35
|
|
|
'light_gray' => '37', |
36
|
|
|
|
37
|
|
|
'dark_gray' => '90', |
38
|
|
|
'light_red' => '91', |
39
|
|
|
'light_green' => '92', |
40
|
|
|
'light_yellow' => '93', |
41
|
|
|
'light_blue' => '94', |
42
|
|
|
'light_magenta' => '95', |
43
|
|
|
'light_cyan' => '96', |
44
|
|
|
'white' => '97', |
45
|
|
|
|
46
|
|
|
'bg_default' => '49', |
47
|
|
|
'bg_black' => '40', |
48
|
|
|
'bg_red' => '41', |
49
|
|
|
'bg_green' => '42', |
50
|
|
|
'bg_yellow' => '43', |
51
|
|
|
'bg_blue' => '44', |
52
|
|
|
'bg_magenta' => '45', |
53
|
|
|
'bg_cyan' => '46', |
54
|
|
|
'bg_light_gray' => '47', |
55
|
|
|
|
56
|
|
|
'bg_dark_gray' => '100', |
57
|
|
|
'bg_light_red' => '101', |
58
|
|
|
'bg_light_green' => '102', |
59
|
|
|
'bg_light_yellow' => '103', |
60
|
|
|
'bg_light_blue' => '104', |
61
|
|
|
'bg_light_magenta' => '105', |
62
|
|
|
'bg_light_cyan' => '106', |
63
|
|
|
'bg_white' => '107', |
64
|
|
|
); |
65
|
|
|
protected $userStyles = array(); |
66
|
|
|
protected $isStyleForced = false; |
67
|
|
|
|
68
|
|
|
public function __construct($string = '') |
69
|
|
|
{ |
70
|
|
|
$this->setInternalState($string); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __invoke($string) |
74
|
|
|
{ |
75
|
|
|
return $this->setInternalState($string); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function __call($method, $args) |
79
|
|
|
{ |
80
|
|
|
if (count($args) >= 1) { |
81
|
|
|
return $this->apply($method, $args[0]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->apply($method); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function __get($name) |
88
|
|
|
{ |
89
|
|
|
return $this->apply($name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function __toString() |
93
|
|
|
{ |
94
|
|
|
return $this->wrapped; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setForceStyle($force) |
98
|
|
|
{ |
99
|
|
|
$this->isStyleForced = (bool) $force; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function isStyleForced() |
103
|
|
|
{ |
104
|
|
|
return $this->isStyleForced; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns true if the stream supports colorization. |
109
|
|
|
* |
110
|
|
|
* Colorization is disabled if not supported by the stream: |
111
|
|
|
* |
112
|
|
|
* - Windows without Ansicon, ConEmu or Babun |
113
|
|
|
* - non tty consoles |
114
|
|
|
* |
115
|
|
|
* @return bool true if the stream supports colorization, false otherwise |
116
|
|
|
* |
117
|
|
|
* @link https://github.com/symfony/Console/blob/master/Output/StreamOutput.php#L94 |
118
|
|
|
* @codeCoverageIgnore |
119
|
|
|
*/ |
120
|
|
|
public function isSupported() |
121
|
|
|
{ |
122
|
|
|
if (DIRECTORY_SEPARATOR === '\\') { |
123
|
|
|
return (function_exists('sapi_windows_vt100_support') |
124
|
|
|
&& @sapi_windows_vt100_support(STDOUT)) |
125
|
|
|
|| false !== getenv('ANSICON') |
126
|
|
|
|| 'ON' === getenv('ConEmuANSI') |
127
|
|
|
|| 'xterm' === getenv('TERM'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (function_exists('stream_isatty')) { |
131
|
|
|
return @stream_isatty(STDOUT); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (function_exists('posix_isatty')) { |
135
|
|
|
return @posix_isatty(STDOUT); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$stat = @fstat($this->stream); |
|
|
|
|
139
|
|
|
// Check if formatted mode is S_IFCHR |
140
|
|
|
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @codeCoverageIgnore |
145
|
|
|
*/ |
146
|
|
|
public function are256ColorsSupported() |
147
|
|
|
{ |
148
|
|
|
return DIRECTORY_SEPARATOR === '/' && false !== strpos(getenv('TERM'), '256color'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function setInternalState($string) |
152
|
|
|
{ |
153
|
|
|
$this->initial = $this->wrapped = (string) $string; |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function stylize($style, $text) |
158
|
|
|
{ |
159
|
|
|
if (!$this->shouldStylize()) { |
160
|
|
|
return $text; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$style = strtolower($style); |
164
|
|
|
|
165
|
|
|
if ($this->isUserStyleExists($style)) { |
166
|
|
|
return $this->applyUserStyle($style, $text); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($this->isStyleExists($style)) { |
170
|
|
|
return $this->applyStyle($style, $text); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (preg_match('/^((?:bg_)?)color\[([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\]$/', $style, $matches)) { |
174
|
|
|
$option = $matches[1] == 'bg_' ? 48 : 38; |
175
|
|
|
return $this->buildEscSeq("{$option};5;{$matches[2]}") . $text . $this->buildEscSeq($this->styles['reset']); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
throw new NoStyleFoundException("Invalid style $style"); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function shouldStylize() |
182
|
|
|
{ |
183
|
|
|
return $this->isStyleForced() || $this->isSupported(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
protected function isStyleExists($style) |
187
|
|
|
{ |
188
|
|
|
return array_key_exists($style, $this->styles); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
protected function applyStyle($style, $text) |
192
|
|
|
{ |
193
|
|
|
return $this->buildEscSeq($this->styles[$style]) . $text . $this->buildEscSeq($this->styles['reset']); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected function buildEscSeq($style) |
197
|
|
|
{ |
198
|
|
|
return sprintf(self::ESC_SEQ_PATTERN, $style); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function isUserStyleExists($style) |
202
|
|
|
{ |
203
|
|
|
return array_key_exists($style, $this->userStyles); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function applyUserStyle($userStyle, $text) |
207
|
|
|
{ |
208
|
|
|
$styles = (array) $this->userStyles[$userStyle]; |
209
|
|
|
|
210
|
|
|
foreach ($styles as $style) { |
211
|
|
|
$text = $this->stylize($style, $text); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $text; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function apply($style, $text = null) |
218
|
|
|
{ |
219
|
|
|
if ($text === null) { |
220
|
|
|
$this->wrapped = $this->stylize($style, $this->wrapped); |
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $this->stylize($style, $text); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function fg($color, $text = null) |
228
|
|
|
{ |
229
|
|
|
return $this->apply($color, $text); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function bg($color, $text = null) |
233
|
|
|
{ |
234
|
|
|
return $this->apply('bg_' . $color, $text); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function highlight($color, $text = null) |
238
|
|
|
{ |
239
|
|
|
return $this->bg($color, $text); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function reset() |
243
|
|
|
{ |
244
|
|
|
$this->wrapped = $this->initial; |
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function center($width = 80, $text = null) |
249
|
|
|
{ |
250
|
|
|
if ($text === null) { |
251
|
|
|
$text = $this->wrapped; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$centered = ''; |
255
|
|
|
foreach (explode(PHP_EOL, $text) as $line) { |
256
|
|
|
$line = trim($line); |
257
|
|
|
$lineWidth = strlen($line) - mb_strlen($line, 'UTF-8') + $width; |
258
|
|
|
$centered .= str_pad($line, $lineWidth, ' ', STR_PAD_BOTH) . PHP_EOL; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$this->setInternalState(trim($centered, PHP_EOL)); |
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
protected function stripColors($text) |
266
|
|
|
{ |
267
|
|
|
return preg_replace('/' . preg_quote(self::ESC) . '\d+m/', '', $text); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function clean($text = null) |
271
|
|
|
{ |
272
|
|
|
if ($text === null) { |
273
|
|
|
$this->wrapped = $this->stripColors($this->wrapped); |
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $this->stripColors($text); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function strip($text = null) |
281
|
|
|
{ |
282
|
|
|
return $this->clean($text); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function isAValidStyleName($name) |
286
|
|
|
{ |
287
|
|
|
return preg_match(self::STYLE_NAME_PATTERN, $name); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @deprecated |
292
|
|
|
* @codeCoverageIgnore |
293
|
|
|
*/ |
294
|
|
|
public function setTheme(array $theme) |
295
|
|
|
{ |
296
|
|
|
return $this->setUserStyles($theme); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function setUserStyles(array $userStyles) |
300
|
|
|
{ |
301
|
|
|
foreach ($userStyles as $name => $styles) { |
302
|
|
|
if (!$this->isAValidStyleName($name)) { |
303
|
|
|
throw new InvalidStyleNameException("$name is not a valid style name"); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
if (in_array($name, (array) $styles)) { |
307
|
|
|
throw new RecursionInUserStylesException('User style cannot reference itself.'); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$this->userStyles = $userStyles; |
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
protected function colorizeText($text) |
316
|
|
|
{ |
317
|
|
|
return preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $text); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function colorize($text = null) |
321
|
|
|
{ |
322
|
|
|
if ($text === null) { |
323
|
|
|
$this->wrapped = $this->colorizeText($this->wrapped); |
324
|
|
|
return $this; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $this->colorizeText($text); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
protected function replaceStyle($matches) |
331
|
|
|
{ |
332
|
|
|
return $this->apply($matches[1], $this->colorize($matches[2])); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.