-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console.php
269 lines (245 loc) · 8.18 KB
/
Console.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php namespace Mreschke\Helpers;
/**
* Command line concole helpers.
* @copyright 2014 Matthew Reschke
* @license http://mreschke.com/license/mit
* @author Matthew Reschke <mail@mreschke.com>
*/
class Console
{
private $foregroundColors;
private $backgroundColors;
private $noColor;
private $log;
private $quiet;
/**
* Create a new Console instance
* @param string $log = null
* @param boolean $quiet = false
*/
public function __construct($log = null, $quiet = false)
{
$this->foregroundColors['default'] = '0;0';
$this->foregroundColors['black'] = '0;30';
$this->foregroundColors['dark_gray'] = '1;30';
$this->foregroundColors['blue'] = '0;34';
$this->foregroundColors['light_blue'] = '1;34';
$this->foregroundColors['green'] = '0;32';
$this->foregroundColors['light_green'] = '1;32';
$this->foregroundColors['cyan'] = '0;36';
$this->foregroundColors['light_cyan'] = '1;36';
$this->foregroundColors['red'] = '0;31';
$this->foregroundColors['light_red'] = '1;31';
$this->foregroundColors['purple'] = '0;35';
$this->foregroundColors['light_purple'] = '1;35';
$this->foregroundColors['brown'] = '0;33';
$this->foregroundColors['yellow'] = '1;33';
$this->foregroundColors['light_gray'] = '0;37';
$this->foregroundColors['white'] = '1;37';
$this->backgroundColors['black'] = '40';
$this->backgroundColors['red'] = '41';
$this->backgroundColors['green'] = '42';
$this->backgroundColors['yellow'] = '43';
$this->backgroundColors['blue'] = '44';
$this->backgroundColors['magenta'] = '45';
$this->backgroundColors['cyan'] = '46';
$this->backgroundColors['light_gray'] = '47';
$this->log = $log;
$this->quiet = $quiet;
}
/**
* Write log file if enabled
* @param string $output
* @param string $summary = 'Main'
* @param string $type = 'log'
* @param string $action = 'next'
* @return void
*/
public function writeLog($output, $summary = 'Main', $type = 'log', $action = 'next')
{
// Only write log if logging to file is enabled
if (isset($this->log)) {
$this->log->write($output, $summary, $type, $action);
}
}
/**
* Output a line to the console and/or the log.
* @param string $output
* @param string $summary = 'Main'
* @param string $type = 'log'
* @param string $action = 'next'
* @return void
*/
public function out($output, $summary = 'Main', $type = 'log', $action = 'next')
{
if (!$this->quiet) {
echo $output, PHP_EOL;
}
$this->writeLog($output, $summary, $type, $action);
}
/**
* Output a header line to the console and/or the log file
* @param string $output
* @param string $summary = 'Main'
* @param string $type = 'log'
* @param string $action = 'next'
* @return void
*/
public function header($output, $summary = 'Main', $type = 'log', $action = 'next')
{
if (!$this->quiet) {
echo $this->color(":: ", "yellow");
echo $this->color($output, "light_green");
echo $this->color(" ::", "yellow");
echo PHP_EOL;
}
$this->writeLog($output, $summary, $type, $action);
}
/**
* Output a line item with optional indentation to the console and/or the log file
* @param string $output
* @param integer $indent = 0
* @param string $summary = 'Main'
* @param string $type = 'log'
* @param string $action = 'next'
* @return void
*/
public function item($output, $indent = 0, $summary = 'Main', $type = 'log', $action = 'next')
{
if (!$this->quiet) {
$color = 'green';
if ($indent > 0) {
$indent = ($indent * 2);
$color = 'blue';
}
echo $this->color(str_repeat(" ", $indent)."* ", $color) . $output . PHP_EOL;
}
$this->writeLog(str_repeat(" ", $indent).$output, $summary, $type, $action);
}
/**
* Output a notice line to the console and/or the log file
* @param string $output
* @param string $summary = 'Main'
* @param string $type = 'unusual'
* @param string $action = 'next'
* @return void
*/
public function notice($output, $summary = 'Main', $type = 'unusual', $action = 'next')
{
if (!$this->quiet) {
echo $this->color($output, "yellow") . PHP_EOL;
}
$this->writeLog($output, $summary, $type, $action);
}
/**
* Output an error line to the console and/or the log file
* @param string $output
* @param string $summary = 'Main'
* @param string $action = 'next'
* @return void
*/
public function error($output, $summary = 'Main', $action = 'next')
{
if (!$this->quiet) {
file_put_contents('php://stderr', $this->color($output, "red") . PHP_EOL);
}
$this->writeLog($output, $summary, 'error', $action);
}
/**
* Output one or more lines with defined character (ex: $c->separator(1, '#', 'blue', 2))
* @param int $count = 1
* @param string $char = ''
* @param string $color = 'default'
* @param int $linesBeforeAfter = 0
* @return void
*/
public function separator($count = 1, $char = '', $color = 'default', $linesBeforeAfter = 0)
{
$width = $this->screenWidth();
if ($linesBeforeAfter) echo str_repeat(PHP_EOL, $linesBeforeAfter);
for ($i=1; $i <= $count; $i++) {
if ($char) {
echo $this->color(str_repeat($char, $width), $color) . PHP_EOL;
} else {
echo PHP_EOL;
}
}
if ($linesBeforeAfter) echo str_repeat(PHP_EOL, $linesBeforeAfter);
}
/**
* Execute a bash command.
* @param string $cmd
* @param boolean $outputArray
* @param string $outputSeparator = PHP_EOL
* @return mixed
*/
public static function exec($cmd, $outputArray = false, $outputSeparator = PHP_EOL)
{
exec("$cmd", $output);
if ($outputArray) {
return $output;
} else {
return implode($outputSeparator, $output);
}
}
/**
* Get terminal screen width
* @return int
*/
public function screenWidth()
{
return exec('tput cols');
#$this->settings['screen']['height'] = exec('tput lines')
}
/**
* Get terminal screen height
* @return int
*/
public function screenHeight()
{
return exec('tput lines');
}
/**
* Get a terminal colored string
* @param string $string
* @param string $foreground_color
* @param string $background_color
* @return string
*/
public function color($string, $foreground_color = null, $background_color = null)
{
$coloredString = "";
// Check if given foreground color found
if (isset($this->foregroundColors[$foreground_color])) {
$coloredString .= "\033[" . $this->foregroundColors[$foreground_color] . "m";
}
// Check if given background color found
if (isset($this->backgroundColors[$background_color])) {
$coloredString .= "\033[" . $this->backgroundColors[$background_color] . "m";
}
// Add string and end coloring
$coloredString .= $string . "\033[0m";
return $coloredString;
}
/**
* Alias to color
*/
public function getColoredString($string, $foreground_color = null, $background_color = null)
{
return $this->color($string, $foreground_color, $background_color);
}
// Returns all foreground color names
public function getForegroundColors()
{
return array_keys($this->foregroundColors);
}
// Returns all background color names
public function getBackgroundColors()
{
return array_keys($this->backgroundColors);
}
public function inlineSed($search, $replace, $file)
{
$this->exec("sed -i 's`$search`$replace`g' $file");
}
}