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
|
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Cli;
use Icinga\Cli\Screen;
use Icinga\Exception\IcingaException;
// @see http://en.wikipedia.org/wiki/ANSI_escape_code
class AnsiScreen extends Screen
{
protected $fgColors = array(
'black' => '30',
'darkgray' => '1;30',
'red' => '31',
'lightred' => '1;31',
'green' => '32',
'lightgreen' => '1;32',
'brown' => '33',
'yellow' => '1;33',
'blue' => '34',
'lightblue' => '1;34',
'purple' => '35',
'lightpurple' => '1;35',
'cyan' => '36',
'lightcyan' => '1;36',
'lightgray' => '37',
'white' => '1;37',
);
protected $bgColors = array(
'black' => '40',
'red' => '41',
'green' => '42',
'brown' => '43',
'blue' => '44',
'purple' => '45',
'cyan' => '46',
'lightgray' => '47',
);
public function strlen($string)
{
return strlen($this->stripAnsiCodes($string));
}
public function stripAnsiCodes($string)
{
return preg_replace('/\e\[?.*?[\@-~]/', '', $string);
}
public function clear()
{
return "\033[2J" // Clear the whole screen
. "\033[1;1H" // Move the cursor to row 1, column 1
. "\033[1S"; // Scroll whole page up by 1 line (why?)
}
public function underline($text)
{
return "\033[4m"
. $text
. "\033[0m"; // Reset color codes
}
public function colorize($text, $fgColor = null, $bgColor = null)
{
return $this->startColor($fgColor, $bgColor)
. $text
. "\033[0m"; // Reset color codes
}
protected function fgColor($color)
{
if (! array_key_exists($color, $this->fgColors)) {
throw new IcingaException(
'There is no such foreground color: %s',
$color
);
}
return $this->fgColors[$color];
}
protected function bgColor($color)
{
if (! array_key_exists($color, $this->bgColors)) {
throw new IcingaException(
'There is no such background color: %s',
$color
);
}
return $this->bgColors[$color];
}
protected function startColor($fgColor = null, $bgColor = null)
{
$escape = "ESC[";
$parts = array();
if ($fgColor !== null
&& $bgColor !== null
&& ! array_key_exists($bgColor, $this->bgColors)
&& array_key_exists($bgColor, $this->fgColors)
&& array_key_exists($fgColor, $this->bgColors)
) {
$parts[] = '7'; // reverse video, negative image
$parts[] = $this->bgColor($fgColor);
$parts[] = $this->fgColor($bgColor);
} else {
if ($fgColor !== null) {
$parts[] = $this->fgColor($fgColor);
}
if ($bgColor !== null) {
$parts[] = $this->bgColor($bgColor);
}
}
if (empty($parts)) {
return '';
}
return "\033[" . implode(';', $parts) . 'm';
}
}
|