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
291
292
|
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2014 Jose Miguel Pérez Ruiz [@twoixter]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @package ansicolors
* @author Jose Miguel Pérez Ruiz <josemiguel@perezruiz.com>
* @copyright 2014 Jose Miguel
* @license http://opensource.org/licenses/MIT The MIT License
*/
/**
* Define STDOUT constant. STDOUT as constant should be automatically defined
* in CLI mode but is not in the interactive mode of the php executable by
* using "php -a" in the shell.
*
* Despite the huge amount of comments elsewhere, the "stdout" standard output
* stream is just the file handle of "1". "php://stdout" is just syntactic
* sugar to open the same, but file handle 1 should be already opened.
*
*/
if (!defined("STDOUT")) define("STDOUT", 1);
/**
* Main "ansi" class. Defined in the root namespace to be able to use it as
* static class. Just imagine it is a namespace...
*
* This class is mainly static. An example of usage follows:
*
* <?
* // Outputs the ANSI codes for red background but DOES NOT reset it.
* // Color remains red until reset.
* echo ansi::red();
*
* // Reset whatever color was in effect
* echo ansi::reset();
*
* // Outputs the string in blue color, auto resets color after the string.
* echo ansi::blue("This is colored blue"):
*
* ?>
*
* Normal colors vs. bright colors.
* --------------------------------
*
* By default, color names in lowercase are faint, normal, or NOT bright. To
* output the bright color version use the name in all UPPER or Camel Case.
*
* Examples: ansi::BLUE(), ansi::Blue() ==> Bright blue
*
*
* Backgrounds
* -----------
*
* Concatenate colors by "_on_" or "_in_" like in "Black_on_white". This
* outputs text in a foreground black color over a white background (reversed).
*
*
*/
class ansi
{
/**
* Main color table. Index the color names to their binary values
*/
static private $colors = array(
"black" => 0, "red" => 1, "green" => 2,
"yellow" => 3, "blue" => 4, "magenta" => 5,
"cyan" => 6, "white" => 7
);
/**
* Same for styles. Most are not yet used, only "bold" for bright
* foreground colors.
*/
static private $styles = array(
"bold" => 1, "normal" => 2, "underline" => 4,
"blink" => 5, "reverse" => 7, "hidden" => 8
);
/**
* Variable that holds wether we are in a TTY or piped to a stream.
* If we are piped, no ANSI color codes are returned.
*
* Hopefully being static also works as a cache, in order to prevent all
* those calls to "posix_isatty" everytime we need to output a color.
*/
static private $piped;
/**
* Color aliases. Dictionary containing the new configured color aliases.
*/
static private $aliases = array();
/**
* The color stack. Here we will be updating the color values in a FIFO
* such that we can restore previous colors.
*/
static private $color_stack = array();
/**
* Static "catch all" method, used for color names and foreground and
* background combinations.
*
* @param string $method The method to call
* @param mixed[] $args Original parameters as array
* @return string|null Returns the ANSI codes, or null if not a color
* @throws BadMethodCallException
*/
static public function __callStatic($method, $args)
{
// Don't output ANSI codes if not a TTY.
// Using a static var for caching the posix_isatty call.
static::$piped = !posix_isatty(STDOUT);
if (static::$piped) return implode(" ", array_map("strval", $args));
// Manage aliases
if (array_key_exists($method, static::$aliases)) {
return static::__callStatic(static::$aliases[$method], $args);
}
// Return if this doesn't seem like a color.
if (($color_code = static::parse_color($method)) === null) {
throw new BadMethodCallException("Color name '".$method."' not found.");
}
// In case of arguments, try to print the passed string, preserving
// the current color, if any.
if (count($args)) {
return implode("", array(
static::code_to_ansi($color_code),
implode(" ", array_map("strval", $args)),
count(static::$color_stack) ? static::code_to_ansi(end(static::$color_stack)) : "\033[0m"
));
}
array_push(static::$color_stack, $color_code);
return static::code_to_ansi($color_code);
}
/**
* Non-static "catch all" method, just in case we are called from
* an instance instead of the static interface.
*
* @param string $method The method to call
* @param mixed[] $args Original parameters as array
* @return string|null Returns the ANSI codes, or null if not a color
*/
public function __call($method, $args)
{
return static::__callStatic($method, $args);
}
/**
* Restore the previous color, or "reset" if none was previously active.
*
* @return string The ANSI string to restore de color or reset.
*/
static function restore()
{
// Don't output ANSI codes if not a TTY.
// Using a static var for caching the posix_isatty call.
static::$piped = !posix_isatty(STDOUT);
if (static::$piped) return "";
// Ok, what I'm going to do is counter intuitive. The color on the
// top of the stack is the _current_ color, so in order to restore
// the previous color, we must remove it from the stack.
array_pop(static::$color_stack);
return count(static::$color_stack)
? static::code_to_ansi(end(static::$color_stack))
: static::reset();
}
/**
* Resets the colors and restores the color codes stack.
*
* @return string The ANSI reset string
*/
static function reset()
{
// Don't output ANSI codes if not a TTY.
// Using a static var for caching the posix_isatty call.
static::$piped = !posix_isatty(STDOUT);
if (static::$piped) return "";
static::$color_stack = array();
return "\033[0m";
}
/**
* Defines a new named color. Useful to configure new semantic color names
* for specific combinations. For example:
*
* ansi::define("error", "Red_on_white");
*
* This example defines "error" (use like ansi::error(), etc) to be like
* the call to "ansi::Red_on_white()".
*
* Definitions are recursive, that is, aliases can be nested. Example:
*
* ansi::define("error", "Red_on_white");
* ansi::define("default", "error");
*
* Now, unsing ansi::default() will use "ansi::error", which in turn is
* "Red_on_white".
*
* @param string $name The name of the new color
* @param string $color The color combination to use
*/
static function define($name, $color)
{
static::$aliases[$name] = $color;
}
/**
* Parses a color string, either a single color or a combination of colors
* with format [color]_in_[color]
*
* @param string $color The color string to parse.
* @return integer|null Returns the color code or null if not a color.
*/
private static function parse_color($color)
{
if (strpos($color, "_") === false) return static::parse_single_color($color);
if (!preg_match("/^([^_]+)_(?:in|on)_([^_]+)$/i", $color, $m)) return null;
list($_, $fore, $back) = $m;
$fore = static::parse_single_color($fore);
$back = static::parse_single_color($back);
if ($fore === null || $back === null) return null;
return ($back * 0x100) + $fore;
}
/**
* Parses a single color, looking into the color array and detecting if
* the color must be bright.
*
* @param string $color The color to parse. Must be a single color.
* @return integer|null Returns the color code or null if not a color.
*/
private static function parse_single_color($color)
{
$isupper = ctype_upper($color[0]);
$colower = strtolower($color);
if (array_key_exists($colower, static::$colors)) {
return static::$colors[$colower] + ($isupper ? 0x10 : 0);
}
// Not a color...
return null;
}
/**
* Converts the color code bits into an ANSI representation.
* Bits 0-3 are foreground color (0x007)
* Bit 4 is brightness indicator (0x010)
* Bits 8-11 are background color (0x700)
*
* @param integer $code The color code to convert to ANSI string
* @return string Returns the ANSI string ready to print
*/
private static function code_to_ansi($code)
{
$codes = array();
if ($code & 0xF00) $codes[] = 40 + (($code & 0xF00) >> 8);
if ($code & 0x010) $codes[] = static::$styles["bold"];
$codes[] = 30 + ($code & 0xF);
return "\033[".implode(";", $codes)."m";
}
}
|