/
Color.php
290 lines (268 loc) · 8.12 KB
/
Color.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Copyright 2003-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL
* @package Cli
*/
/**
* Horde_Cli API for basic command-line functionality/checks.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2003-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL
* @package Cli
* @since Horde_Cli 2.2.0
*/
class Horde_Cli_Color
{
/**
* No color formatting.
*/
const FORMAT_NONE = 0;
/**
* xterm compatible color formatting.
*/
const FORMAT_XTERM = 1;
/**
* VT100 compatible color formatting.
*/
const FORMAT_VT100 = 2;
/**
* HTML compatible color formatting.
*/
const FORMAT_HTML = 3;
/**
* The color formatting being used.
*
* One of the FORMAT_* constants.
*/
protected $_format = self::FORMAT_NONE;
/**
* Constructor.
*
* @param integer $format The color format to use. One of the FORMAT_*
* constants. Automatically detected by default.
*/
public function __construct($format = null)
{
if (!is_null($format)) {
$this->_format = $format;
return;
}
if (Horde_Cli::runningFromCli()) {
$term = getenv('TERM');
if ($term) {
if (preg_match('/^(xterm|vt220|linux)/', $term)) {
$this->_format = self::FORMAT_XTERM;
} elseif (preg_match('/^vt100/', $term)) {
$this->_format = self::FORMAT_VT100;
}
}
} else {
$this->_format = self::FORMAT_HTML;
}
}
/**
* Returns a bold version of $text.
*
* @param string $text The text to bold.
*
* @return string The bolded text.
*/
public function bold($text)
{
$bold_start = $bold_end = '';
switch ($this->_format) {
case self::FORMAT_XTERM:
case self::FORMAT_VT100:
$bold_start = "\x1b[1m";
$bold_end = "\x1b[0m";
break;
case self::FORMAT_HTML:
$bold_start = '<strong>';
$bold_end = '</strong>';
break;
}
return $bold_start . $text . $bold_end;
}
/**
* Returns a colored version of $text.
*
* @param string $color The color to use. One of:
* - normal
* - black
* - bold
* - red
* - green
* - brown
* - blue
* - magenta
* - cyan
* - lightgray
* - white
* - darkgray
* - lightred
* - lightgreen
* - yellow
* - lightblue
* - lightmagenta
* - lightcyan
* @param string $text The text to print in this color.
*
* @return string The colored text.
*/
public function color($color, $text)
{
if ($this->_format == self::FORMAT_HTML) {
return '<span style="color:' . $color . '">' . $text . '</span>';
}
if ($this->_format != self::FORMAT_XTERM) {
return $text;
}
$colors = $this->_foregroundColors();
if (isset($colors[$color])) {
return $colors[$color] . $text . "\x1b[39m";
}
return $text;
}
/**
* Returns a colored version of $text, with the method name specifying the
* color:
*
* <code>
* echo $cliColor->lightred("Foo");
* </code>
*
* @param string $text The text to print in color.
*
* @return string The colored text.
*/
public function __call($color, $args)
{
return $this->color($color, $args[0]);
}
/**
* Returns a version of $text with a colored background.
*
* @param string $color The background color to use.
* @param string $text The text to print on this background.
*
* @return string The text with background.
*/
public function background($color, $text)
{
if ($this->_format == self::FORMAT_HTML) {
return '<span style="background-color:' . $color . '">'
. $text . '</span>';
}
if ($this->_format != self::FORMAT_XTERM) {
return $text;
}
$colors = $this->_backgroundColors();
if (isset($colors[$color])) {
return $colors[$color] . $text . "\x1b[49m";
}
return $text;
}
/**
* Returns a version of $text with a colored foreground and background.
*
* @param string $foreground The foreground color to use.
* @param string $background The background color to use.
* @param string $text The text to print in color.
*
* @return string The colored text.
*/
public function format($foreground, $background, $text)
{
return $this->background($background, $this->color($foreground, $text));
}
/**
* Removes all color formatting from a text.
*
* @param string $text A colored text.
*
* @return string The text with all coloring markup removed.
*/
public function remove($text)
{
if ($this->_format == self::FORMAT_HTML) {
$text = str_replace('</span>', '', $text);
foreach (array_keys(array_merge($this->_foregroundColors(), $this->_backgroundColors())) as $color) {
$text = str_replace(
array(
'<span style="color:' . $color . '">',
'<span style="background-color:' . $color . '">',
),
'',
$text
);
}
return $text;
}
return str_replace(
array_merge(
array_values($this->_foregroundColors()),
array_values($this->_backgroundColors())
),
'',
$text
);
}
/**
* Returns the xterm-compatible foreground color sequences.
*
* @return array Color sequences.
*/
protected function _foregroundColors()
{
return array(
'bold' => "\x1b[1m",
'normal' => "\x1b[39m",
'black' => "\x1b[30m",
'red' => "\x1b[31m",
'green' => "\x1b[32m",
'brown' => "\x1b[33m",
'blue' => "\x1b[34m",
'magenta' => "\x1b[35m",
'cyan' => "\x1b[36m",
'lightgray' => "\x1b[37m",
'darkgray' => "\x1b[1m\x1b[30m",
'lightred' => "\x1b[1m\x1b[31m",
'lightgreen' => "\x1b[1m\x1b[32m",
'yellow' => "\x1b[1m\x1b[33m",
'lightblue' => "\x1b[1m\x1b[34m",
'lightmagenta' => "\x1b[1m\x1b[35m",
'lightcyan' => "\x1b[1m\x1b[36m",
'white' => "\x1b[1m\x1b[37m",
);
}
/**
* Returns the xterm-compatible background color sequences.
*
* @return array Color sequences.
*/
protected function _backgroundColors()
{
return array(
'normal' => "\x1b[49m",
'black' => "\x1b[40m",
'red' => "\x1b[41m",
'green' => "\x1b[42m",
'brown' => "\x1b[43m",
'blue' => "\x1b[44m",
'magenta' => "\x1b[45m",
'cyan' => "\x1b[46m",
'lightgray' => "\x1b[47m",
);
}
}