Skip to content

Commit 039e2ec

Browse files
committed
ANSI color support
1 parent 8841067 commit 039e2ec

4 files changed

Lines changed: 102 additions & 60 deletions

File tree

ansi.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

example.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'notify_spinner' => 'cli\notify\Spinner Example',
1616
'progress_bar' => 'cli\progress\Bar Example',
1717
'table' => 'cli\Table Example',
18+
'colors' => 'cli\Colors example',
1819
'quit' => 'Quit',
1920
);
2021
$headers = array('First Name', 'Last Name', 'City', 'State');
@@ -97,6 +98,9 @@ function test_notify(\cli\Notify $notify, $cycle = 1000000, $sleep = null) {
9798
$table->setRows($data);
9899
$table->display();
99100
break;
101+
case 'colors':
102+
\cli\line(' %C%5All output is run through %Y%6\cli\Colors::colorize%C%5 before display%n');
103+
break;
100104
}
101105

102106
\cli\line();

lib/cli/Colors.php

Lines changed: 95 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,63 +18,115 @@
1818
* Reference: http://graphcomp.com/info/specs/ansi_col.html#colors
1919
*/
2020
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+
)
5050
);
5151

5252
/**
5353
* Set the color.
5454
*
5555
* @param string $color The name of the color or style to set.
5656
*/
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');
6060
}
6161

62-
$colors = array(static::$_foreground[$fore]);
62+
$color += array('color' => null, 'style' => null, 'background' => null);
6363

64-
if (isset(static::$_background[$back])) {
65-
$colors[] = static::$_background[$back];
64+
if ($color['color'] == 'reset') {
65+
return "\033[0m";
6666
}
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;
6978
}
7079

71-
\cli\out("\033[%sm", join(';', $colors));
80+
return "\033[" . join(';', $colors) . "m";
7281
}
7382

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);
79131
}
80132
}

lib/cli/cli.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ function render($msg) {
4545

4646
// No string replacement is needed
4747
if (count($args) == 1) {
48-
return $msg;
48+
return Colors::colorize($msg);
4949
}
5050

5151
// If the first argument is not an array just pass to sprintf
5252
if (!is_array($args[1])) {
53-
return call_user_func_array('sprintf', $args);
53+
return Colors::colorize(call_user_func_array('sprintf', $args));
5454
}
5555

5656
// Here we do named replacement so formatting strings are more understandable
5757
foreach ($args[1] as $key => $value) {
5858
$msg = str_replace('{:' . $key . '}', $value, $msg);
5959
}
60-
return $msg;
60+
return Colors::colorize($msg);
6161
}
6262

6363
/**

0 commit comments

Comments
 (0)