forked from wp-cli/php-cli-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.php
More file actions
133 lines (116 loc) · 3.6 KB
/
Copy pathColor.php
File metadata and controls
133 lines (116 loc) · 3.6 KB
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
<?php
/**
* PHP Console Tools is a console library for PHP
*
* PHP versions 5.3
*
* @category ToolsAndUtilities
* @package Console
* @author James Logsdon <jlogsdon@php.net>
* @copyright 2010 James Logsdon
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://www.github.com/jlogsdon/php-cli-tools
*/
namespace Console;
/**
* ANSI console coloring
*
* @category ToolsAndUtilities
* @package Console
* @author James Logsdon <jlogsdon@php.net>
* @copyright 2010 James Logsdon
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://www.github.com/jlogsdon/php-cli-tools
*/
class Color {
static protected $colorCodes = array(
'color' => array(
'black' => 30,
'red' => 31,
'green' => 32,
'yellow' => 33,
'blue' => 34,
'magenta' => 35,
'cyan' => 36,
'white' => 37
),
'style' => array(
'bright' => 1,
'dim' => 2,
'underscore' => 4,
'blink' => 5,
'reverse' => 7,
'hidden' => 8
),
'background' => array(
'black' => 40,
'red' => 41,
'green' => 42,
'yellow' => 43,
'blue' => 44,
'magenta' => 45,
'cyan' => 46,
'white' => 47
)
);
/**
* Returns a string that initiates a color.
*
* @param array color the color details (color, background, style)
*
* @return string the ansi codes
*/
static public function colorCode($color) {
if (!is_array($color)) {
$color = compact('color');
}
$default = array('color' => null, 'style' => null, 'background' => null);
$color += $default;
if ($color['color'] == 'reset') {
return "\033[0m";
}
$colors = array();
foreach (array_keys($default) as $type) {
$name = $color[$type];
if (isset(static::$colorCodes[$type][$name])) {
$colors[] = static::$colorCodes[$type][$name];
}
}
if (empty($colors)) {
$colors[] = 0;
}
return "\033[" . join(';', $colors) . "m";
}
/**
* Colorizes a string and replaces parameters.
*
* @param string $str the string to colorize
*
* @return string the colorized string
*/
static public function ize($str) {
$pattern = '#{(?P<text>.*?)}';
$pattern.= '\.(?P<color>[a-zA-Z]+)';
$pattern.= '(\((?P<args>[a-zA-Z,]+)\))?';
if (!preg_match_all('/' . $pattern . '/ms', $str, $matches, PREG_SET_ORDER)) {
return $str;
}
foreach ($matches as $match) {
$color = array('color' => $match['color']);
if (!empty($match['args'])) {
$args = strpos($match['args'], ',') ? explode(',', $match['args']) : array($match['args']);
while ($arg = strtolower(trim(array_shift($args)))) {
if (isset(static::$colorCodes['background'][$arg])) {
$color['background'] = $arg;
} else if (isset(static::$colorCodes['style'][$arg])) {
$color['style'] = $arg;
}
}
}
$parsed = static::colorCode($color) . $match['text'] . static::colorCode('reset');
$str = str_replace($match[0], $parsed, $str);
}
return $str;
}
}
?>