Debian Codegraph @

vendor/jfcherng/php-color-output/src/CliColor.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jfcherng\Utility;
6
7 /**
8 * Make your PHP command-line application colorful.
9 *
10 * @see https://en.wikipedia.org/wiki/ANSI_escape_code
11 *
12 * @author Jack Cherng <[email protected]>
13 */
14 final class CliColor
15 {
16 const COLOR_BEGIN = "\033[";
17 const COLOR_END = 'm';
18
19 const COLOR_BEGIN_REGEX = "\033\\[";
20 const COLOR_END_REGEX = 'm';
21
22 /**
23 * @var array the color map
24 */
25 private static $colorMap = [
26 // background
27 'b_black' => '40',
28 'b_blue' => '44',
29 'b_cyan' => '46',
30 'b_green' => '42',
31 'b_light_gray' => '47',
32 'b_magenta' => '45',
33 'b_red' => '41',
34 'b_yellow' => '43',
35
36 // foreground
37 'f_black' => '30',
38 'f_blue' => '34',
39 'f_brown' => '33',
40 'f_cyan' => '36',
41 'f_green' => '32',
42 'f_light_gray' => '37',
43 'f_normal' => '39',
44 'f_purple' => '35',
45 'f_red' => '31',
46
47 // compound
48 'f_dark_gray' => '1;30',
49 'f_light_blue' => '1;34',
50 'f_light_cyan' => '1;36',
51 'f_light_green' => '1;32',
52 'f_light_purple' => '1;35',
53 'f_light_red' => '1;31',
54 'f_white' => '1;37',
55 'f_yellow' => '1;33',
56
57 // special
58 'blink' => '5',
59 'bold' => '1',
60 'dim' => '2',
61 'hidden' => '8',
62 'reset' => '0',
63 'reverse' => '7',
64 'underline' => '4',
65
66 // alias
67 'b' => 'bold',
68 'blk' => 'blink',
69 'h' => 'hidden',
70 'rev' => 'reverse',
71 'rst' => 'reset',
72 'u' => 'underline',
73
74 // regex for color codes
75 'regex_any' => '(?:[0-9]++;?)++',
76 ];
77
78 /**
79 * Get the color map.
80 *
81 * @return array the color map
82 */
83 public static function getColorMap(): array
84 {
85 return self::$colorMap;
86 }
87
88 /**
89 * Make a string colorful.
90 *
91 * @param string $str the string
92 * @param string|string[] $colors the colors
93 * @param bool $reset reset color at the end of the string?
94 *
95 * @return string the colored string
96 */
97 public static function color(string $str, $colors = [], bool $reset = true): string
98 {
99 // always convert $colors into an array
100 if (\is_string($colors)) {
101 $colors = \explode(',', $colors);
102 }
103
104 $colored = self::getColorCode($colors) . $str;
105
106 if ($reset) {
107 $colored .= self::getColorCode(['reset']);
108 }
109
110 return self::simplifyColoredString($colored);
111 }
112
113 /**
114 * Remove all colors from a string.
115 *
116 * @param string $str the string
117 *
118 * @return string the string without colors
119 */
120 public static function noColor(string $str): string
121 {
122 return \preg_replace(
123 '~' . self::getColorCode(['regex_any'], true) . '~uS',
124 '',
125 $str
126 );
127 }
128
129 /**
130 * Get the color code from given colors.
131 *
132 * @param array $colors the colors
133 * @param bool $returnRegex return as an regex segment
134 *
135 * @return string the color code
136 */
137 private static function getColorCode(array $colors, bool $returnRegex = false): string
138 {
139 $colors = self::sanitizeColors($colors);
140
141 if (empty($colors)) {
142 return '';
143 }
144
145 // convert color into color code
146 $colorCodes = \array_map(
147 function (string $color): string {
148 // resolve color alias
149 while (isset(self::$colorMap[$color])) {
150 $color = self::$colorMap[$color];
151 }
152
153 return $color;
154 },
155 $colors
156 );
157
158 $closures = $returnRegex
159 ? [self::COLOR_BEGIN_REGEX, self::COLOR_END_REGEX]
160 : [self::COLOR_BEGIN, self::COLOR_END];
161
162 return $closures[0] . \implode(';', $colorCodes) . $closures[1];
163 }
164
165 /**
166 * Sanitize colors.
167 *
168 * @param array $colors the colors
169 *
170 * @return array the sanitized colors
171 */
172 private static function sanitizeColors(array $colors): array
173 {
174 return self::listUnique(\array_filter(
175 \array_map('trim', $colors),
176 function (string $color): bool {
177 return isset(self::$colorMap[$color]);
178 }
179 ));
180 }
181
182 /**
183 * Simplify the colored string.
184 *
185 * @param string $str the colored string
186 *
187 * @return string the simplified colored string
188 */
189 private static function simplifyColoredString(string $str): string
190 {
191 // replace multiple consecutive resets with a single reset
192 $str = \preg_replace(
193 '~(' . self::getColorCode(['reset'], true) . '){2,}~uS',
194 '$1',
195 $str
196 );
197
198 // remove colors for an emtpy string
199 $str = \preg_replace(
200 (
201 '~' .
202 '(' . self::getColorCode(['regex_any'], true) . ')' .
203 '(' . self::getColorCode(['reset'], true) . ')' .
204 '~uS'
205 ),
206 '$2',
207 $str
208 );
209
210 return $str;
211 }
212
213 /**
214 * The fastest array_unique() implementation for a non-associative array AFAIK.
215 *
216 * @see https://stackoverflow.com/questions/8321620/array-unique-vs-array-flip
217 *
218 * @param array $array the array
219 */
220 private static function listUnique(array $array): array
221 {
222 return \array_keys(\array_count_values($array));
223 }
224 }