|
18 | 18 | * Reference: http://graphcomp.com/info/specs/ansi_col.html#colors |
19 | 19 | */ |
20 | 20 | class Colors { |
21 | | - static protected $_styles = array( |
22 | | - 'bright' => 1, |
23 | | - 'dim' => 2, |
24 | | - 'underscore' => 4, |
25 | | - 'blink' => 5, |
26 | | - 'reverse' => 7, |
27 | | - 'hidden' => 8, |
28 | | - ); |
29 | | - |
30 | | - static protected $_foreground = array( |
31 | | - 'black' => 30, |
32 | | - 'red' => 31, |
33 | | - 'green' => 32, |
34 | | - 'yellow' => 33, |
35 | | - 'blue' => 34, |
36 | | - 'magenta' => 35, |
37 | | - 'cyan' => 36, |
38 | | - 'white' => 37, |
39 | | - ); |
40 | | - |
41 | | - static protected $_background = array( |
42 | | - 'black' => 40, |
43 | | - 'red' => 41, |
44 | | - 'green' => 42, |
45 | | - 'yellow' => 43, |
46 | | - 'blue' => 44, |
47 | | - 'magenta' => 45, |
48 | | - 'cyan' => 46, |
49 | | - 'white' => 47, |
| 21 | + static protected $_colors = array( |
| 22 | + 'color' => array( |
| 23 | + 'black' => 30, |
| 24 | + 'red' => 31, |
| 25 | + 'green' => 32, |
| 26 | + 'yellow' => 33, |
| 27 | + 'blue' => 34, |
| 28 | + 'magenta' => 35, |
| 29 | + 'cyan' => 36, |
| 30 | + 'white' => 37 |
| 31 | + ), |
| 32 | + 'style' => array( |
| 33 | + 'bright' => 1, |
| 34 | + 'dim' => 2, |
| 35 | + 'underscore' => 4, |
| 36 | + 'blink' => 5, |
| 37 | + 'reverse' => 7, |
| 38 | + 'hidden' => 8 |
| 39 | + ), |
| 40 | + 'background' => array( |
| 41 | + 'black' => 40, |
| 42 | + 'red' => 41, |
| 43 | + 'green' => 42, |
| 44 | + 'yellow' => 43, |
| 45 | + 'blue' => 44, |
| 46 | + 'magenta' => 45, |
| 47 | + 'cyan' => 46, |
| 48 | + 'white' => 47 |
| 49 | + ) |
50 | 50 | ); |
51 | 51 |
|
52 | 52 | /** |
53 | 53 | * Set the color. |
54 | 54 | * |
55 | 55 | * @param string $color The name of the color or style to set. |
56 | 56 | */ |
57 | | - static public function set($fore, $back = null, $style = null) { |
58 | | - if (!isset(static::$_foreground[$fore])) { |
59 | | - return; |
| 57 | + static public function color($color) { |
| 58 | + if (!is_array($color)) { |
| 59 | + $color = compact('color'); |
60 | 60 | } |
61 | 61 |
|
62 | | - $colors = array(static::$_foreground[$fore]); |
| 62 | + $color += array('color' => null, 'style' => null, 'background' => null); |
63 | 63 |
|
64 | | - if (isset(static::$_background[$back])) { |
65 | | - $colors[] = static::$_background[$back]; |
| 64 | + if ($color['color'] == 'reset') { |
| 65 | + return "\033[0m"; |
66 | 66 | } |
67 | | - if (isset(static::$_styles[$style])) { |
68 | | - $colors[] = static::$_styles[$style]; |
| 67 | + |
| 68 | + $colors = array(); |
| 69 | + foreach (array('color', 'style', 'background') as $type) { |
| 70 | + $code = @$color[$type]; |
| 71 | + if (isset(self::$_colors[$type][$code])) { |
| 72 | + $colors[] = self::$_colors[$type][$code]; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (empty($colors)) { |
| 77 | + $colors[] = 0; |
69 | 78 | } |
70 | 79 |
|
71 | | - \cli\out("\033[%sm", join(';', $colors)); |
| 80 | + return "\033[" . join(';', $colors) . "m"; |
72 | 81 | } |
73 | 82 |
|
74 | | - /** |
75 | | - * Resets the color back to the default. |
76 | | - */ |
77 | | - static public function reset() { |
78 | | - \cli\out("\033[0m"); |
| 83 | + static public function colorize($string, $colored = true) { |
| 84 | + static $conversions = array( |
| 85 | + '%y' => array('color' => 'yellow'), |
| 86 | + '%g' => array('color' => 'green'), |
| 87 | + '%b' => array('color' => 'blue'), |
| 88 | + '%r' => array('color' => 'red'), |
| 89 | + '%p' => array('color' => 'magenta'), |
| 90 | + '%m' => array('color' => 'magenta'), |
| 91 | + '%c' => array('color' => 'cyan'), |
| 92 | + '%w' => array('color' => 'grey'), |
| 93 | + '%k' => array('color' => 'black'), |
| 94 | + '%n' => array('color' => 'reset'), |
| 95 | + '%Y' => array('color' => 'yellow', 'style' => 'light'), |
| 96 | + '%G' => array('color' => 'green', 'style' => 'light'), |
| 97 | + '%B' => array('color' => 'blue', 'style' => 'light'), |
| 98 | + '%R' => array('color' => 'red', 'style' => 'light'), |
| 99 | + '%P' => array('color' => 'magenta', 'style' => 'light'), |
| 100 | + '%M' => array('color' => 'magenta', 'style' => 'light'), |
| 101 | + '%C' => array('color' => 'cyan', 'style' => 'light'), |
| 102 | + '%W' => array('color' => 'grey', 'style' => 'light'), |
| 103 | + '%K' => array('color' => 'black', 'style' => 'light'), |
| 104 | + '%N' => array('color' => 'reset', 'style' => 'light'), |
| 105 | + '%3' => array('background' => 'yellow'), |
| 106 | + '%2' => array('background' => 'green'), |
| 107 | + '%4' => array('background' => 'blue'), |
| 108 | + '%1' => array('background' => 'red'), |
| 109 | + '%5' => array('background' => 'magenta'), |
| 110 | + '%6' => array('background' => 'cyan'), |
| 111 | + '%7' => array('background' => 'grey'), |
| 112 | + '%0' => array('background' => 'black'), |
| 113 | + '%F' => array('style' => 'blink'), |
| 114 | + '%U' => array('style' => 'underline'), |
| 115 | + '%8' => array('style' => 'inverse'), |
| 116 | + '%9' => array('style' => 'bold'), |
| 117 | + '%_' => array('style' => 'bold') |
| 118 | + ); |
| 119 | + |
| 120 | + if (!$colored) { |
| 121 | + return preg_replace('/%((%)|.)/', '$2', $string); |
| 122 | + } |
| 123 | + |
| 124 | + $string = str_replace('%%', '% ', $string); |
| 125 | + |
| 126 | + foreach ($conversions as $key => $value) { |
| 127 | + $string = str_replace($key, self::color($value), $string); |
| 128 | + } |
| 129 | + |
| 130 | + return str_replace('% ', '%', $string); |
79 | 131 | } |
80 | 132 | } |
0 commit comments