1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* __ __ _ _ _ _ |
5
|
|
|
* | \/ |(_) ___ | |__ ___ | || | |
6
|
|
|
* | |\/| || |/ __|| '_ \ / _ \| || | |
7
|
|
|
* | | | || |\__ \| | | || __/| || | |
8
|
|
|
* |_| |_||_||___/|_| |_| \___||_||_| |
9
|
|
|
* |
10
|
|
|
* This file is part of Kristuff\Mishell. |
11
|
|
|
* (c) Kristuff <[email protected]> |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
* |
16
|
|
|
* @version 1.1.0 |
17
|
|
|
* @copyright 2017-2020 Kristuff |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Kristuff\Mishell; |
21
|
|
|
|
22
|
|
|
abstract class ShellColoredPrinter extends \Kristuff\Mishell\ShellPrinter |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Foreground colors constants |
26
|
|
|
* |
27
|
|
|
* @access public |
28
|
|
|
* @static var |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected static $foregroundColors = array( |
32
|
|
|
'normal' => '39', // your default color |
33
|
|
|
'black' => '30', |
34
|
|
|
'grey' => '1;30', |
35
|
|
|
'lightgrey' => '37', |
36
|
|
|
'white' => '1;37', |
37
|
|
|
'blue' => '34', |
38
|
|
|
'lightblue' => '1;34', |
39
|
|
|
'green' => '32', |
40
|
|
|
'lightgreen' => '1;32', |
41
|
|
|
'cyan' => '36', |
42
|
|
|
'lightcyan' => '1;36', |
43
|
|
|
'red' => '31', |
44
|
|
|
'lightred' => '1;31', |
45
|
|
|
'magenta' => '35', |
46
|
|
|
'lightmagenta' => '1;35', |
47
|
|
|
'brown' => '33', |
48
|
|
|
'yellow' => '1;33', |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Background colors constants |
53
|
|
|
* |
54
|
|
|
* @access public |
55
|
|
|
* @static var |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected static $backgroundColors = array( |
59
|
|
|
'black' => '40', |
60
|
|
|
'red' => '41', |
61
|
|
|
'green' => '42', |
62
|
|
|
'yellow' => '43', |
63
|
|
|
'blue' => '44', |
64
|
|
|
'magenta' => '45', |
65
|
|
|
'cyan' => '46', |
66
|
|
|
'white' => '47', |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Text styles constants |
71
|
|
|
* |
72
|
|
|
* @access public |
73
|
|
|
* @static var |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected static $options = array( |
77
|
|
|
'none' => '0', // reset all styles |
78
|
|
|
'bold' => '1', // |
79
|
|
|
'underline' => '4', |
80
|
|
|
'blink' => '5', |
81
|
|
|
'reverse' => '7', // reverse foreground/background color |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get an array of all available styles |
86
|
|
|
* |
87
|
|
|
* @access public |
88
|
|
|
* @static method |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public static function getStyles() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
'foregrounds' => self::$foregroundColors, |
96
|
|
|
'backgrounds' => self::$backgroundColors, |
97
|
|
|
'options' => self::$options |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Internal method dispatcher |
103
|
|
|
* |
104
|
|
|
* @access protected |
105
|
|
|
* @static method |
106
|
|
|
* @param string $command The command name string |
107
|
|
|
* @param string $args The command arguments |
108
|
|
|
* |
109
|
|
|
* @return mixed|void |
110
|
|
|
*/ |
111
|
|
|
protected static function cmd($command, array $args) |
112
|
|
|
{ |
113
|
|
|
// ouptut string is always the first argument (in any) |
114
|
|
|
$str = !empty($args) ? $args[0] : ''; |
115
|
|
|
|
116
|
|
|
// others (if any) are options |
117
|
|
|
array_shift($args); |
118
|
|
|
|
119
|
|
|
// |
120
|
|
|
switch($command){ |
121
|
|
|
|
122
|
|
|
// **************************************** |
123
|
|
|
// Get methods (return string) |
124
|
|
|
// **************************************** |
125
|
|
|
|
126
|
|
|
case'text': |
127
|
|
|
return self::getCliString($str, $args); // get formated text |
128
|
|
|
|
129
|
|
|
// **************************************** |
130
|
|
|
// Print methods (echo and return null) |
131
|
|
|
// **************************************** |
132
|
|
|
|
133
|
|
|
case'print': |
134
|
|
|
echo (self::getCliString($str, $args)); // print text |
135
|
|
|
break; |
136
|
|
|
case'log': |
137
|
|
|
echo (self::getCliString($str, $args) . self::$EOF ); // print text + newline |
138
|
|
|
break; |
139
|
|
|
case'relog': |
140
|
|
|
echo (self::getCliString($str ."\r", $args)); // overwrite current line |
141
|
|
|
break; |
142
|
|
|
|
143
|
|
|
// ***************************************** |
144
|
|
|
// Print+Question methods (return something) |
145
|
|
|
// ***************************************** |
146
|
|
|
|
147
|
|
|
case'ask': |
148
|
|
|
echo (self::getCliString($str, $args)); // print question |
149
|
|
|
return trim(fgets(STDIN)); // reads and return one line from STDIN |
150
|
|
|
|
151
|
|
|
case'askPassword': |
152
|
|
|
self::hideInput(); // hide |
153
|
|
|
echo (self::getCliString($str, $args)); // print question |
154
|
|
|
$line= trim(fgets(STDIN)); // reads one line from STDIN |
155
|
|
|
self::restoreInput(); // restore |
156
|
|
|
return $line; // return line |
157
|
|
|
|
158
|
|
|
case 'askInt': |
159
|
|
|
echo (self::getCliString($str, $args)); // print question |
160
|
|
|
fscanf(STDIN, "%d\n", $number); // reads number from STDIN |
161
|
|
|
return (is_int($number) ? $number : false); // return int value or false |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// nothing found |
165
|
|
|
return null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get a formated cli string to output in the console |
170
|
|
|
* |
171
|
|
|
* @access protected |
172
|
|
|
* @static method |
173
|
|
|
* @param string $str The text to output |
174
|
|
|
* @param string $arguments The command arguments |
175
|
|
|
* |
176
|
|
|
* @return mixed|void |
177
|
|
|
*/ |
178
|
|
|
protected static function getCliString($str, array $arguments = []) |
179
|
|
|
{ |
180
|
|
|
if (empty($arguments)){ |
181
|
|
|
return $str; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$coloredString = ""; |
185
|
|
|
$hasColor = false; |
186
|
|
|
$hasBackColor = false; |
187
|
|
|
$cliArgs = []; |
188
|
|
|
|
189
|
|
|
// parse arguments |
190
|
|
|
foreach ($arguments as $argument) { |
191
|
|
|
|
192
|
|
|
// it's a color? |
193
|
|
|
if(!$hasColor && isset(self::$foregroundColors[$argument])){ |
194
|
|
|
$cliArgs[] = self::$foregroundColors[$argument]; |
195
|
|
|
$hasColor = true; |
196
|
|
|
|
197
|
|
|
// it's a backcolor? |
198
|
|
|
} elseif ($hasColor && !$hasBackColor && isset(self::$backgroundColors[$argument])){ |
199
|
|
|
$cliArgs[] = self::$backgroundColors[$argument]; |
200
|
|
|
$hasBackColor = true; |
201
|
|
|
|
202
|
|
|
// or it's an option? |
203
|
|
|
} elseif (isset(self::$options[$argument])){ |
204
|
|
|
$cliArgs[] = self::$options[$argument]; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Add string and end coloring |
209
|
|
|
$coloredString .= "\033[" . implode(';',$cliArgs) .'m'; |
210
|
|
|
$coloredString .= $str . "\033[0m"; |
211
|
|
|
return $coloredString; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get a formated string to be returned in console. |
216
|
|
|
* |
217
|
|
|
* @access public |
218
|
|
|
* @static method |
219
|
|
|
* @param string [$str] The string to output |
220
|
|
|
* @param string [$color] The text color for the wall line |
221
|
|
|
* @param string [$bgcolor] The back color for the wall line |
222
|
|
|
* @param string [$option]+... The text styles for the wall line |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public static function text() |
227
|
|
|
{ |
228
|
|
|
return self::cmd('text', func_get_args()); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Print a formated string in console. |
233
|
|
|
* |
234
|
|
|
* @access public |
235
|
|
|
* @static method |
236
|
|
|
* @param string [$str] The string to print |
237
|
|
|
* @param string [$color] The text color for the wall line |
238
|
|
|
* @param string [$bgcolor] The back color for the wall line |
239
|
|
|
* @param string [$option]+... The text styles for the wall line |
240
|
|
|
* |
241
|
|
|
* @return void |
242
|
|
|
*/ |
243
|
|
|
public static function print() |
244
|
|
|
{ |
245
|
|
|
return self::cmd('write', func_get_args()); |
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Prints a formatted string in the console then waits for a user input. |
250
|
|
|
* |
251
|
|
|
* @access public |
252
|
|
|
* @static method |
253
|
|
|
* @param string [$str] The string to print |
254
|
|
|
* @param string [$color] The text color for the wall line |
255
|
|
|
* @param string [$bgcolor] The back color for the wall line |
256
|
|
|
* @param string [$option]+... The text styles for the wall line |
257
|
|
|
* |
258
|
|
|
* @return string|null |
259
|
|
|
*/ |
260
|
|
|
public static function ask() |
261
|
|
|
{ |
262
|
|
|
return self::cmd('ask', func_get_args()); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Print a formated string in the console and wait for an integer input. |
267
|
|
|
* |
268
|
|
|
* @access public |
269
|
|
|
* @static method |
270
|
|
|
* @param string [$str] The string to print |
271
|
|
|
* @param string [$color] The text color for the wall line |
272
|
|
|
* @param string [$bgcolor] The back color for the wall line |
273
|
|
|
* @param string [$option]+... The text styles for the wall line |
274
|
|
|
* |
275
|
|
|
* @return int|bool |
276
|
|
|
*/ |
277
|
|
|
public static function askInt() |
278
|
|
|
{ |
279
|
|
|
return self::cmd('askInt', func_get_args()); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Prints a formatted string in the console then waits for a user input (returns but does not displays that user's input). |
284
|
|
|
* |
285
|
|
|
* @param string [$str] The string to print |
286
|
|
|
* @param string [$color] The text color for the wall line |
287
|
|
|
* @param string [$bgcolor] The back color for the wall line |
288
|
|
|
* @param string [$option]+... The text styles for the wall line |
289
|
|
|
* |
290
|
|
|
* @return string|null |
291
|
|
|
*/ |
292
|
|
|
public static function askPassword() |
293
|
|
|
{ |
294
|
|
|
return self::cmd('askPassword',func_get_args()); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Print a string to console and go to new line |
299
|
|
|
* |
300
|
|
|
* @access public |
301
|
|
|
* @static method |
302
|
|
|
* @param string [$str] The string to print |
303
|
|
|
* @param string [$color] The text color for the wall line |
304
|
|
|
* @param string [$bgcolor] The back color for the wall line |
305
|
|
|
* @param string [$option]+... The text styles for the wall line |
306
|
|
|
* |
307
|
|
|
* @return void |
308
|
|
|
*/ |
309
|
|
|
public static function log() |
310
|
|
|
{ |
311
|
|
|
self::cmd('log',func_get_args()); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Overwrite the current line in console. |
316
|
|
|
* |
317
|
|
|
* @access public |
318
|
|
|
* @static method |
319
|
|
|
* @param string [$str] The string to print |
320
|
|
|
* @param string [$color] The text color for the wall line |
321
|
|
|
* @param string [$bgcolor] The back color for the wall line |
322
|
|
|
* @param string [$option]+... The text styles for the wall line |
323
|
|
|
* |
324
|
|
|
* @return void |
325
|
|
|
*/ |
326
|
|
|
public static function relog() |
327
|
|
|
{ |
328
|
|
|
self::cmd('relog',func_get_args()); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* A cli version of str_pad() that takes care of not printable ANSI chars |
333
|
|
|
* |
334
|
|
|
* @access protected |
335
|
|
|
* @static method |
336
|
|
|
* @param string $input The input text |
337
|
|
|
* @param int $padLenght The pad length. Default is 0 (no pad) |
338
|
|
|
* @param string $padString The pad string. Default is blank char. |
339
|
|
|
* @param int $padType The pad type (STR_PAD_LEFT, STR_PAD_RIGHT or STR_PAD_BOTH). Default is STR_PAD_RIGHT. |
340
|
|
|
* |
341
|
|
|
* @return mixed|void |
342
|
|
|
*/ |
343
|
|
|
public function pad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT) |
344
|
|
|
{ |
345
|
|
|
$diff = $padLength - strlen(preg_replace('#\\033\[[[0-9;*]{1,}m#', '', $input)); |
346
|
|
|
|
347
|
|
|
if ($diff > 0){ |
348
|
|
|
switch ($padType){ |
349
|
|
|
|
350
|
|
|
case STR_PAD_RIGHT: |
351
|
|
|
return $input . str_repeat($padString, $diff); |
352
|
|
|
|
353
|
|
|
case STR_PAD_LEFT: |
354
|
|
|
return str_repeat($padString, $diff ) . $input; |
355
|
|
|
|
356
|
|
|
case STR_PAD_BOTH: |
357
|
|
|
$padLeft = round(($diff)/2); |
358
|
|
|
return str_repeat($padString, $padLeft) . $input . str_repeat($padString, $diff - $padLeft); |
|
|
|
|
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
return $input; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.