|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Utils; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Terminal |
|
6
|
|
|
* @package PHPDaemon\Utils |
|
7
|
|
|
* @author Vasily Zorin <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
class Terminal { |
|
10
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
|
11
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var boolean Is color allowed in terminal? |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $enableColor = false; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var integer Maximum terminal width |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $columns = 80; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct() { |
|
27
|
|
|
$this->columns = $this->getMaxColumns(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Read a line from STDIN |
|
32
|
|
|
* @return string Line |
|
33
|
|
|
*/ |
|
34
|
|
|
public function readln() { |
|
35
|
|
|
return fgets(STDIN); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Enables/disable color |
|
40
|
|
|
* @param boolean $bool Enable? |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function enableColor($bool = true) { |
|
44
|
|
|
$this->enableColor = $bool; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Clear the terminal with CLR |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function clearScreen() { |
|
52
|
|
|
echo "\x0c"; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set text style |
|
57
|
|
|
* @param string $c Style |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setStyle($c) { |
|
61
|
|
|
if ($this->enableColor) { |
|
62
|
|
|
echo "\033[" . $c . 'm'; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Reset style to default |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
|
|
public function resetStyle() { |
|
71
|
|
|
if ($this->enableColor) { |
|
72
|
|
|
echo "\033[0m"; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Counting terminal char width |
|
78
|
|
|
* @return integer |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getMaxColumns() { |
|
81
|
|
|
if ( |
|
82
|
|
|
preg_match_all("/columns.([0-9]+);/", strtolower(@exec('stty -a | grep columns')), $output) |
|
83
|
|
|
&& sizeof($output) === 2 |
|
84
|
|
|
) { |
|
85
|
|
|
return $output[1][0]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return 80; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Draw param (like in man) |
|
93
|
|
|
* @param string $name Param name |
|
94
|
|
|
* @param string $description Param description |
|
95
|
|
|
* @param array $values Param allowed values |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function drawParam($name, $description, $values = '') { |
|
99
|
|
|
$paramw = round($this->columns / 3); |
|
100
|
|
|
|
|
101
|
|
|
echo "\n"; |
|
102
|
|
|
|
|
103
|
|
|
$leftcolumn = []; |
|
104
|
|
|
|
|
105
|
|
|
$valstr = is_array($values) ? implode('|', array_keys($values)) : $values; |
|
106
|
|
|
|
|
107
|
|
|
if ('' !== $valstr) { |
|
108
|
|
|
$valstr = '=[' . $valstr . ']'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$paramstr = " \033[1m--" . $name . $valstr . "\033[0m"; |
|
112
|
|
|
|
|
113
|
|
|
$pl =mb_orig_strlen$paramstr); |
|
|
|
|
|
|
114
|
|
|
if ($pl + 2 >= $paramw) { |
|
115
|
|
|
$paramw = $pl + 3; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$descw = $this->columns - $paramw; |
|
119
|
|
|
|
|
120
|
|
|
$leftcolumn[] = $paramstr; |
|
121
|
|
|
|
|
122
|
|
|
if (is_array($values)) { |
|
123
|
|
|
foreach ($values as $key => $value) { |
|
124
|
|
|
$leftcolumn[] = ' ' . $key . ' - ' . $value; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (strlen($description) <= $descw) { |
|
129
|
|
|
$rightcolumn[] = $description; |
|
130
|
|
|
} |
|
131
|
|
|
else { |
|
132
|
|
|
$m = explode(' ', $description); |
|
133
|
|
|
|
|
134
|
|
|
$descstr = ''; |
|
135
|
|
|
|
|
136
|
|
|
while (sizeof($m) > 0) { |
|
137
|
|
|
$el = array_shift($m); |
|
138
|
|
|
|
|
139
|
|
|
if (strlen($descstr) +mb_orig_strlen$el) >= $descw) { |
|
140
|
|
|
$rightcolumn[] = $descstr; |
|
141
|
|
|
$descstr = ''; |
|
142
|
|
|
} |
|
143
|
|
|
else { |
|
144
|
|
|
$descstr .= ' '; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$descstr .= $el; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if ('' !== $descstr) { |
|
151
|
|
|
$rightcolumn[] = $descstr; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
while ( |
|
156
|
|
|
sizeof($leftcolumn) > 0 |
|
157
|
|
|
|| sizeof($rightcolumn) > 0 |
|
158
|
|
|
) { |
|
159
|
|
|
if ($l = array_shift($leftcolumn)) { |
|
160
|
|
|
echo str_pad($l, $paramw, ' '); |
|
161
|
|
|
} |
|
162
|
|
|
else { |
|
163
|
|
|
echo str_repeat(' ', $paramw - 7); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if ($r = array_shift($rightcolumn)) { |
|
167
|
|
|
echo $r; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
echo "\n"; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Draw a table |
|
176
|
|
|
* @param array Array of table's rows |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function drawTable($rows) { |
|
180
|
|
|
$pad = []; |
|
181
|
|
|
|
|
182
|
|
|
foreach ($rows as $row) { |
|
183
|
|
|
foreach ($row as $k => $v) { |
|
184
|
|
|
if (substr($k, 0, 1) === '_') { |
|
185
|
|
|
continue; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if (!isset($pad[$k]) || (strlen($v) > $pad[$k])) { |
|
189
|
|
|
$pad[$k] =mb_orig_strlen$v); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
foreach ($rows as $row) { |
|
195
|
|
|
if (isset($row['_color'])) { |
|
196
|
|
|
$this->setStyle($row['_color']); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (isset($row['_bold'])) { |
|
200
|
|
|
$this->setStyle('1'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if (isset($row['_'])) { |
|
204
|
|
|
echo $row['_']; |
|
205
|
|
|
} |
|
206
|
|
|
else { |
|
207
|
|
|
$i = 0; |
|
208
|
|
|
|
|
209
|
|
|
foreach ($row as $k => $v) { |
|
210
|
|
|
if (substr($k, 0, 1) === '_') { |
|
211
|
|
|
continue; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
if ($i > 0) { |
|
215
|
|
|
echo "\t"; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
echo str_pad($v, $pad[$k]); |
|
219
|
|
|
++$i; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$this->resetStyle(); |
|
224
|
|
|
echo "\n"; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|