| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Color.php
|
|
|
4 |
*
|
|
|
5 |
* PHP version 4
|
|
|
6 |
*
|
|
|
7 |
* Copyright (c) 2007 Stefan Walk
|
|
|
8 |
*
|
|
|
9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
10 |
* of this software and associated documentation files (the "Software"), to
|
|
|
11 |
* deal in the Software without restriction, including without limitation the
|
|
|
12 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
13 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
14 |
* furnished to do so, subject to the following conditions:
|
|
|
15 |
*
|
|
|
16 |
* The above copyright notice and this permission notice shall be included in
|
|
|
17 |
* all copies or substantial portions of the Software.
|
|
|
18 |
*
|
|
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
24 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
25 |
* IN THE SOFTWARE.
|
|
|
26 |
*
|
|
|
27 |
* @category Console
|
|
|
28 |
* @package Console_Color
|
|
|
29 |
* @author Stefan Walk <et@php.net>
|
|
|
30 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
31 |
* @link http://pear.php.net/package/Console_Color
|
|
|
32 |
*/
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
$GLOBALS['_CONSOLE_COLOR_CODES'] = array (
|
|
|
36 |
'color' => array(
|
|
|
37 |
'black' => 30,
|
|
|
38 |
'red' => 31,
|
|
|
39 |
'green' => 32,
|
|
|
40 |
'brown' => 33,
|
|
|
41 |
'blue' => 34,
|
|
|
42 |
'purple' => 35,
|
|
|
43 |
'cyan' => 36,
|
|
|
44 |
'grey' => 37,
|
|
|
45 |
'yellow' => 33
|
|
|
46 |
),
|
|
|
47 |
'style' => array(
|
|
|
48 |
'normal' => 0,
|
|
|
49 |
'bold' => 1,
|
|
|
50 |
'light' => 1,
|
|
|
51 |
'underscore' => 4,
|
|
|
52 |
'underline' => 4,
|
|
|
53 |
'blink' => 5,
|
|
|
54 |
'inverse' => 6,
|
|
|
55 |
'hidden' => 8,
|
|
|
56 |
'concealed' => 8
|
|
|
57 |
),
|
|
|
58 |
'background' => array(
|
|
|
59 |
'black' => 40,
|
|
|
60 |
'red' => 41,
|
|
|
61 |
'green' => 42,
|
|
|
62 |
'brown' => 43,
|
|
|
63 |
'yellow' => 43,
|
|
|
64 |
'blue' => 44,
|
|
|
65 |
'purple' => 45,
|
|
|
66 |
'cyan' => 46,
|
|
|
67 |
'grey' => 47
|
|
|
68 |
)
|
|
|
69 |
);
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* A simple class to use ANSI Colorcodes.
|
|
|
74 |
*
|
|
|
75 |
* Of all the functions, you probably only want to use convert() and escape().
|
|
|
76 |
* They are easier to use. However, if you want to access colorcodes more
|
|
|
77 |
* directly, look into the other functions.
|
|
|
78 |
*
|
|
|
79 |
* @category Console
|
|
|
80 |
* @package Console_Color
|
|
|
81 |
* @author Stefan Walk <et@php.net>
|
|
|
82 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
83 |
* @link http://pear.php.net/package/Console_Color
|
|
|
84 |
*/
|
|
|
85 |
class Console_Color
|
|
|
86 |
{
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Returns an ANSI-Controlcode
|
|
|
90 |
*
|
|
|
91 |
* Takes 1 to 3 Arguments: either 1 to 3 strings containing the name of the
|
|
|
92 |
* FG Color, style and BG color, or one array with the indices color, style
|
|
|
93 |
* or background.
|
|
|
94 |
*
|
|
|
95 |
* @param mixed $color Optional.
|
|
|
96 |
* Either a string with the name of the foreground
|
|
|
97 |
* color, or an array with the indices 'color',
|
|
|
98 |
* 'style', 'background' and corresponding names as
|
|
|
99 |
* values.
|
|
|
100 |
* @param string $style Optional name of the style
|
|
|
101 |
* @param string $background Optional name of the background color
|
|
|
102 |
*
|
|
|
103 |
* @access public
|
|
|
104 |
* @return string
|
|
|
105 |
*/
|
|
|
106 |
function color($color = null, $style = null, $background = null) // {{{
|
|
|
107 |
{
|
|
|
108 |
$colors = &$GLOBALS['_CONSOLE_COLOR_CODES'];
|
|
|
109 |
if (is_array($color)) {
|
|
|
110 |
$style = @$color['style'];
|
|
|
111 |
$background = @$color['background'];
|
|
|
112 |
$color = @$color['color'];
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if ($color == 'reset') {
|
|
|
116 |
return "\033[0m";
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$code = array();
|
|
|
120 |
if (isset($color)) {
|
|
|
121 |
$code[] = $colors['color'][$color];
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if (isset($style)) {
|
|
|
125 |
$code[] = $colors['style'][$style];
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
if (isset($background)) {
|
|
|
129 |
$code[] = $colors['background'][$background];
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
if (empty($code)) {
|
|
|
133 |
$code[] = 0;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
$code = implode(';', $code);
|
|
|
137 |
return "\033[{$code}m";
|
|
|
138 |
} // }}}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Returns a FG color controlcode
|
|
|
142 |
*
|
|
|
143 |
* @param string $name Name of controlcode
|
|
|
144 |
*
|
|
|
145 |
* @access public
|
|
|
146 |
* @return string
|
|
|
147 |
*/
|
|
|
148 |
function fgcolor($name)
|
|
|
149 |
{
|
|
|
150 |
$colors = &$GLOBALS['_CONSOLE_COLOR_CODES'];
|
|
|
151 |
return "\033[".$colors['color'][$name].'m';
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Returns a style controlcode
|
|
|
156 |
*
|
|
|
157 |
* @param string $name Name of controlcode
|
|
|
158 |
*
|
|
|
159 |
* @access public
|
|
|
160 |
* @return string
|
|
|
161 |
*/
|
|
|
162 |
function style($name)
|
|
|
163 |
{
|
|
|
164 |
$colors = &$GLOBALS['_CONSOLE_COLOR_CODES'];
|
|
|
165 |
return "\033[".$colors['style'][$name].'m';
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Returns a BG color controlcode
|
|
|
170 |
*
|
|
|
171 |
* @param string $name Name of controlcode
|
|
|
172 |
*
|
|
|
173 |
* @access public
|
|
|
174 |
* @return string
|
|
|
175 |
*/
|
|
|
176 |
function bgcolor($name)
|
|
|
177 |
{
|
|
|
178 |
$colors = &$GLOBALS['_CONSOLE_COLOR_CODES'];
|
|
|
179 |
return "\033[".$colors['background'][$name].'m';
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Converts colorcodes in the format %y (for yellow) into ansi-control
|
|
|
184 |
* codes. The conversion table is: ('bold' meaning 'light' on some
|
|
|
185 |
* terminals). It's almost the same conversion table irssi uses.
|
|
|
186 |
* <pre>
|
|
|
187 |
* text text background
|
|
|
188 |
* ------------------------------------------------
|
|
|
189 |
* %k %K %0 black dark grey black
|
|
|
190 |
* %r %R %1 red bold red red
|
|
|
191 |
* %g %G %2 green bold green green
|
|
|
192 |
* %y %Y %3 yellow bold yellow yellow
|
|
|
193 |
* %b %B %4 blue bold blue blue
|
|
|
194 |
* %m %M %5 magenta bold magenta magenta
|
|
|
195 |
* %p %P magenta (think: purple)
|
|
|
196 |
* %c %C %6 cyan bold cyan cyan
|
|
|
197 |
* %w %W %7 white bold white white
|
|
|
198 |
*
|
|
|
199 |
* %F Blinking, Flashing
|
|
|
200 |
* %U Underline
|
|
|
201 |
* %8 Reverse
|
|
|
202 |
* %_,%9 Bold
|
|
|
203 |
*
|
|
|
204 |
* %n Resets the color
|
|
|
205 |
* %% A single %
|
|
|
206 |
* </pre>
|
|
|
207 |
* First param is the string to convert, second is an optional flag if
|
|
|
208 |
* colors should be used. It defaults to true, if set to false, the
|
|
|
209 |
* colorcodes will just be removed (And %% will be transformed into %)
|
|
|
210 |
*
|
|
|
211 |
* @param string $string String to convert
|
|
|
212 |
* @param bool $colored Should the string be colored?
|
|
|
213 |
*
|
|
|
214 |
* @access public
|
|
|
215 |
* @return string
|
|
|
216 |
*/
|
|
|
217 |
function convert($string, $colored = true)
|
|
|
218 |
{
|
|
|
219 |
static $conversions = array ( // static so the array doesn't get built
|
|
|
220 |
// everytime
|
|
|
221 |
// %y - yellow, and so on... {{{
|
|
|
222 |
'%y' => array('color' => 'yellow'),
|
|
|
223 |
'%g' => array('color' => 'green' ),
|
|
|
224 |
'%b' => array('color' => 'blue' ),
|
|
|
225 |
'%r' => array('color' => 'red' ),
|
|
|
226 |
'%p' => array('color' => 'purple'),
|
|
|
227 |
'%m' => array('color' => 'purple'),
|
|
|
228 |
'%c' => array('color' => 'cyan' ),
|
|
|
229 |
'%w' => array('color' => 'grey' ),
|
|
|
230 |
'%k' => array('color' => 'black' ),
|
|
|
231 |
'%n' => array('color' => 'reset' ),
|
|
|
232 |
'%Y' => array('color' => 'yellow', 'style' => 'light'),
|
|
|
233 |
'%G' => array('color' => 'green', 'style' => 'light'),
|
|
|
234 |
'%B' => array('color' => 'blue', 'style' => 'light'),
|
|
|
235 |
'%R' => array('color' => 'red', 'style' => 'light'),
|
|
|
236 |
'%P' => array('color' => 'purple', 'style' => 'light'),
|
|
|
237 |
'%M' => array('color' => 'purple', 'style' => 'light'),
|
|
|
238 |
'%C' => array('color' => 'cyan', 'style' => 'light'),
|
|
|
239 |
'%W' => array('color' => 'grey', 'style' => 'light'),
|
|
|
240 |
'%K' => array('color' => 'black', 'style' => 'light'),
|
|
|
241 |
'%N' => array('color' => 'reset', 'style' => 'light'),
|
|
|
242 |
'%3' => array('background' => 'yellow'),
|
|
|
243 |
'%2' => array('background' => 'green' ),
|
|
|
244 |
'%4' => array('background' => 'blue' ),
|
|
|
245 |
'%1' => array('background' => 'red' ),
|
|
|
246 |
'%5' => array('background' => 'purple'),
|
|
|
247 |
'%6' => array('background' => 'cyan' ),
|
|
|
248 |
'%7' => array('background' => 'grey' ),
|
|
|
249 |
'%0' => array('background' => 'black' ),
|
|
|
250 |
// Don't use this, I can't stand flashing text
|
|
|
251 |
'%F' => array('style' => 'blink'),
|
|
|
252 |
'%U' => array('style' => 'underline'),
|
|
|
253 |
'%8' => array('style' => 'inverse'),
|
|
|
254 |
'%9' => array('style' => 'bold'),
|
|
|
255 |
'%_' => array('style' => 'bold')
|
|
|
256 |
// }}}
|
|
|
257 |
);
|
|
|
258 |
|
|
|
259 |
if ($colored) {
|
|
|
260 |
$string = str_replace('%%', '% ', $string);
|
|
|
261 |
foreach ($conversions as $key => $value) {
|
|
|
262 |
$string = str_replace($key, Console_Color::color($value),
|
|
|
263 |
$string);
|
|
|
264 |
}
|
|
|
265 |
$string = str_replace('% ', '%', $string);
|
|
|
266 |
|
|
|
267 |
} else {
|
|
|
268 |
$string = preg_replace('/%((%)|.)/', '$2', $string);
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
return $string;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
/**
|
|
|
275 |
* Escapes % so they don't get interpreted as color codes
|
|
|
276 |
*
|
|
|
277 |
* @param string $string String to escape
|
|
|
278 |
*
|
|
|
279 |
* @access public
|
|
|
280 |
* @return string
|
|
|
281 |
*/
|
|
|
282 |
function escape($string)
|
|
|
283 |
{
|
|
|
284 |
return str_replace('%', '%%', $string);
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
/**
|
|
|
288 |
* Strips ANSI color codes from a string
|
|
|
289 |
*
|
|
|
290 |
* @param string $string String to strip
|
|
|
291 |
*
|
|
|
292 |
* @acess public
|
|
|
293 |
* @return string
|
|
|
294 |
*/
|
|
|
295 |
function strip($string)
|
|
|
296 |
{
|
|
|
297 |
return preg_replace('/\033\[[\d;]+m/', '', $string);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
}
|
|
|
301 |
?>
|