1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\helpers; |
9
|
|
|
|
10
|
|
|
use yii\console\Markdown as ConsoleMarkdown; |
11
|
|
|
use yii\base\Model; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* BaseConsole provides concrete implementation for [[Console]]. |
15
|
|
|
* |
16
|
|
|
* Do not use BaseConsole. Use [[Console]] instead. |
17
|
|
|
* |
18
|
|
|
* @author Carsten Brandt <[email protected]> |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
class BaseConsole |
22
|
|
|
{ |
23
|
|
|
// foreground color control codes |
24
|
|
|
const FG_BLACK = 30; |
25
|
|
|
const FG_RED = 31; |
26
|
|
|
const FG_GREEN = 32; |
27
|
|
|
const FG_YELLOW = 33; |
28
|
|
|
const FG_BLUE = 34; |
29
|
|
|
const FG_PURPLE = 35; |
30
|
|
|
const FG_CYAN = 36; |
31
|
|
|
const FG_GREY = 37; |
32
|
|
|
// background color control codes |
33
|
|
|
const BG_BLACK = 40; |
34
|
|
|
const BG_RED = 41; |
35
|
|
|
const BG_GREEN = 42; |
36
|
|
|
const BG_YELLOW = 43; |
37
|
|
|
const BG_BLUE = 44; |
38
|
|
|
const BG_PURPLE = 45; |
39
|
|
|
const BG_CYAN = 46; |
40
|
|
|
const BG_GREY = 47; |
41
|
|
|
// fonts style control codes |
42
|
|
|
const RESET = 0; |
43
|
|
|
const NORMAL = 0; |
44
|
|
|
const BOLD = 1; |
45
|
|
|
const ITALIC = 3; |
46
|
|
|
const UNDERLINE = 4; |
47
|
|
|
const BLINK = 5; |
48
|
|
|
const NEGATIVE = 7; |
49
|
|
|
const CONCEALED = 8; |
50
|
|
|
const CROSSED_OUT = 9; |
51
|
|
|
const FRAMED = 51; |
52
|
|
|
const ENCIRCLED = 52; |
53
|
|
|
const OVERLINED = 53; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Moves the terminal cursor up by sending ANSI control code CUU to the terminal. |
58
|
|
|
* If the cursor is already at the edge of the screen, this has no effect. |
59
|
|
|
* @param int $rows number of rows the cursor should be moved up |
60
|
|
|
*/ |
61
|
1 |
|
public static function moveCursorUp($rows = 1) |
62
|
|
|
{ |
63
|
1 |
|
echo "\033[" . (int) $rows . 'A'; |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Moves the terminal cursor down by sending ANSI control code CUD to the terminal. |
68
|
|
|
* If the cursor is already at the edge of the screen, this has no effect. |
69
|
|
|
* @param int $rows number of rows the cursor should be moved down |
70
|
|
|
*/ |
71
|
1 |
|
public static function moveCursorDown($rows = 1) |
72
|
|
|
{ |
73
|
1 |
|
echo "\033[" . (int) $rows . 'B'; |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Moves the terminal cursor forward by sending ANSI control code CUF to the terminal. |
78
|
|
|
* If the cursor is already at the edge of the screen, this has no effect. |
79
|
|
|
* @param int $steps number of steps the cursor should be moved forward |
80
|
|
|
*/ |
81
|
1 |
|
public static function moveCursorForward($steps = 1) |
82
|
|
|
{ |
83
|
1 |
|
echo "\033[" . (int) $steps . 'C'; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Moves the terminal cursor backward by sending ANSI control code CUB to the terminal. |
88
|
|
|
* If the cursor is already at the edge of the screen, this has no effect. |
89
|
|
|
* @param int $steps number of steps the cursor should be moved backward |
90
|
|
|
*/ |
91
|
1 |
|
public static function moveCursorBackward($steps = 1) |
92
|
|
|
{ |
93
|
1 |
|
echo "\033[" . (int) $steps . 'D'; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Moves the terminal cursor to the beginning of the next line by sending ANSI control code CNL to the terminal. |
98
|
|
|
* @param int $lines number of lines the cursor should be moved down |
99
|
|
|
*/ |
100
|
1 |
|
public static function moveCursorNextLine($lines = 1) |
101
|
|
|
{ |
102
|
1 |
|
echo "\033[" . (int) $lines . 'E'; |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Moves the terminal cursor to the beginning of the previous line by sending ANSI control code CPL to the terminal. |
107
|
|
|
* @param int $lines number of lines the cursor should be moved up |
108
|
|
|
*/ |
109
|
1 |
|
public static function moveCursorPrevLine($lines = 1) |
110
|
|
|
{ |
111
|
1 |
|
echo "\033[" . (int) $lines . 'F'; |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Moves the cursor to an absolute position given as column and row by sending ANSI control code CUP or CHA to the terminal. |
116
|
|
|
* @param int $column 1-based column number, 1 is the left edge of the screen. |
117
|
|
|
* @param int|null $row 1-based row number, 1 is the top edge of the screen. if not set, will move cursor only in current line. |
118
|
|
|
*/ |
119
|
1 |
|
public static function moveCursorTo($column, $row = null) |
120
|
|
|
{ |
121
|
1 |
|
if ($row === null) { |
122
|
1 |
|
echo "\033[" . (int) $column . 'G'; |
123
|
|
|
} else { |
124
|
1 |
|
echo "\033[" . (int) $row . ';' . (int) $column . 'H'; |
125
|
|
|
} |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Scrolls whole page up by sending ANSI control code SU to the terminal. |
130
|
|
|
* New lines are added at the bottom. This is not supported by ANSI.SYS used in windows. |
131
|
|
|
* @param int $lines number of lines to scroll up |
132
|
|
|
*/ |
133
|
1 |
|
public static function scrollUp($lines = 1) |
134
|
|
|
{ |
135
|
1 |
|
echo "\033[" . (int) $lines . 'S'; |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Scrolls whole page down by sending ANSI control code SD to the terminal. |
140
|
|
|
* New lines are added at the top. This is not supported by ANSI.SYS used in windows. |
141
|
|
|
* @param int $lines number of lines to scroll down |
142
|
|
|
*/ |
143
|
1 |
|
public static function scrollDown($lines = 1) |
144
|
|
|
{ |
145
|
1 |
|
echo "\033[" . (int) $lines . 'T'; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Saves the current cursor position by sending ANSI control code SCP to the terminal. |
150
|
|
|
* Position can then be restored with [[restoreCursorPosition()]]. |
151
|
|
|
*/ |
152
|
1 |
|
public static function saveCursorPosition() |
153
|
|
|
{ |
154
|
1 |
|
echo "\033[s"; |
155
|
1 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Restores the cursor position saved with [[saveCursorPosition()]] by sending ANSI control code RCP to the terminal. |
159
|
|
|
*/ |
160
|
1 |
|
public static function restoreCursorPosition() |
161
|
|
|
{ |
162
|
1 |
|
echo "\033[u"; |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Hides the cursor by sending ANSI DECTCEM code ?25l to the terminal. |
167
|
|
|
* Use [[showCursor()]] to bring it back. |
168
|
|
|
* Do not forget to show cursor when your application exits. Cursor might stay hidden in terminal after exit. |
169
|
|
|
*/ |
170
|
1 |
|
public static function hideCursor() |
171
|
|
|
{ |
172
|
1 |
|
echo "\033[?25l"; |
173
|
1 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Will show a cursor again when it has been hidden by [[hideCursor()]] by sending ANSI DECTCEM code ?25h to the terminal. |
177
|
|
|
*/ |
178
|
1 |
|
public static function showCursor() |
179
|
|
|
{ |
180
|
1 |
|
echo "\033[?25h"; |
181
|
1 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Clears entire screen content by sending ANSI control code ED with argument 2 to the terminal. |
185
|
|
|
* Cursor position will not be changed. |
186
|
|
|
* **Note:** ANSI.SYS implementation used in windows will reset cursor position to upper left corner of the screen. |
187
|
|
|
*/ |
188
|
1 |
|
public static function clearScreen() |
189
|
|
|
{ |
190
|
1 |
|
echo "\033[2J"; |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Clears text from cursor to the beginning of the screen by sending ANSI control code ED with argument 1 to the terminal. |
195
|
|
|
* Cursor position will not be changed. |
196
|
|
|
*/ |
197
|
1 |
|
public static function clearScreenBeforeCursor() |
198
|
|
|
{ |
199
|
1 |
|
echo "\033[1J"; |
200
|
1 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Clears text from cursor to the end of the screen by sending ANSI control code ED with argument 0 to the terminal. |
204
|
|
|
* Cursor position will not be changed. |
205
|
|
|
*/ |
206
|
1 |
|
public static function clearScreenAfterCursor() |
207
|
|
|
{ |
208
|
1 |
|
echo "\033[0J"; |
209
|
1 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Clears the line, the cursor is currently on by sending ANSI control code EL with argument 2 to the terminal. |
213
|
|
|
* Cursor position will not be changed. |
214
|
|
|
*/ |
215
|
1 |
|
public static function clearLine() |
216
|
|
|
{ |
217
|
1 |
|
echo "\033[2K"; |
218
|
1 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Clears text from cursor position to the beginning of the line by sending ANSI control code EL with argument 1 to the terminal. |
222
|
|
|
* Cursor position will not be changed. |
223
|
|
|
*/ |
224
|
1 |
|
public static function clearLineBeforeCursor() |
225
|
|
|
{ |
226
|
1 |
|
echo "\033[1K"; |
227
|
1 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Clears text from cursor position to the end of the line by sending ANSI control code EL with argument 0 to the terminal. |
231
|
|
|
* Cursor position will not be changed. |
232
|
|
|
*/ |
233
|
1 |
|
public static function clearLineAfterCursor() |
234
|
|
|
{ |
235
|
1 |
|
echo "\033[0K"; |
236
|
1 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns the ANSI format code. |
240
|
|
|
* |
241
|
|
|
* @param array $format An array containing formatting values. |
242
|
|
|
* You can pass any of the `FG_*`, `BG_*` and `TEXT_*` constants |
243
|
|
|
* and also [[xtermFgColor]] and [[xtermBgColor]] to specify a format. |
244
|
|
|
* @return string The ANSI format code according to the given formatting constants. |
245
|
|
|
*/ |
246
|
3 |
|
public static function ansiFormatCode($format) |
247
|
|
|
{ |
248
|
3 |
|
return "\033[" . implode(';', $format) . 'm'; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Echoes an ANSI format code that affects the formatting of any text that is printed afterwards. |
253
|
|
|
* |
254
|
|
|
* @param array $format An array containing formatting values. |
255
|
|
|
* You can pass any of the `FG_*`, `BG_*` and `TEXT_*` constants |
256
|
|
|
* and also [[xtermFgColor]] and [[xtermBgColor]] to specify a format. |
257
|
|
|
* @see ansiFormatCode() |
258
|
|
|
* @see endAnsiFormat() |
259
|
|
|
*/ |
260
|
1 |
|
public static function beginAnsiFormat($format) |
261
|
|
|
{ |
262
|
1 |
|
echo "\033[" . implode(';', $format) . 'm'; |
263
|
1 |
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Resets any ANSI format set by previous method [[beginAnsiFormat()]] |
267
|
|
|
* Any output after this will have default text format. |
268
|
|
|
* This is equal to calling. |
269
|
|
|
* |
270
|
|
|
* ```php |
271
|
|
|
* echo Console::ansiFormatCode([Console::RESET]) |
272
|
|
|
* ``` |
273
|
|
|
*/ |
274
|
1 |
|
public static function endAnsiFormat() |
275
|
|
|
{ |
276
|
1 |
|
echo "\033[0m"; |
277
|
1 |
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Will return a string formatted with the given ANSI style. |
281
|
|
|
* |
282
|
|
|
* @param string $string the string to be formatted |
283
|
|
|
* @param array $format An array containing formatting values. |
284
|
|
|
* You can pass any of the `FG_*`, `BG_*` and `TEXT_*` constants |
285
|
|
|
* and also [[xtermFgColor]] and [[xtermBgColor]] to specify a format. |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
30 |
|
public static function ansiFormat($string, $format = []) |
289
|
|
|
{ |
290
|
30 |
|
$code = implode(';', $format); |
291
|
|
|
|
292
|
30 |
|
return "\033[0m" . ($code !== '' ? "\033[" . $code . 'm' : '') . $string . "\033[0m"; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Returns the ansi format code for xterm foreground color. |
297
|
|
|
* |
298
|
|
|
* You can pass the return value of this to one of the formatting methods: |
299
|
|
|
* [[ansiFormat]], [[ansiFormatCode]], [[beginAnsiFormat]]. |
300
|
|
|
* |
301
|
|
|
* @param int $colorCode xterm color code |
302
|
|
|
* @return string |
303
|
|
|
* @see http://en.wikipedia.org/wiki/Talk:ANSI_escape_code#xterm-256colors |
304
|
|
|
*/ |
305
|
1 |
|
public static function xtermFgColor($colorCode) |
306
|
|
|
{ |
307
|
1 |
|
return '38;5;' . $colorCode; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Returns the ansi format code for xterm background color. |
312
|
|
|
* |
313
|
|
|
* You can pass the return value of this to one of the formatting methods: |
314
|
|
|
* [[ansiFormat]], [[ansiFormatCode]], [[beginAnsiFormat]]. |
315
|
|
|
* |
316
|
|
|
* @param int $colorCode xterm color code |
317
|
|
|
* @return string |
318
|
|
|
* @see http://en.wikipedia.org/wiki/Talk:ANSI_escape_code#xterm-256colors |
319
|
|
|
*/ |
320
|
1 |
|
public static function xtermBgColor($colorCode) |
321
|
|
|
{ |
322
|
1 |
|
return '48;5;' . $colorCode; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Strips ANSI control codes from a string. |
327
|
|
|
* |
328
|
|
|
* @param string $string String to strip |
329
|
|
|
* @return string |
330
|
|
|
*/ |
331
|
10 |
|
public static function stripAnsiFormat($string) |
332
|
|
|
{ |
333
|
10 |
|
return preg_replace('/\033\[[\d;?]*\w/', '', $string); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Returns the length of the string without ANSI color codes. |
338
|
|
|
* @param string $string the string to measure |
339
|
|
|
* @return int the length of the string not counting ANSI format characters |
340
|
|
|
*/ |
341
|
|
|
public static function ansiStrlen($string) |
342
|
|
|
{ |
343
|
|
|
return mb_strlen(static::stripAnsiFormat($string)); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Converts an ANSI formatted string to HTML. |
348
|
|
|
* |
349
|
|
|
* Note: xTerm 256 bit colors are currently not supported. |
350
|
|
|
* |
351
|
|
|
* @param string $string the string to convert. |
352
|
|
|
* @param array $styleMap an optional mapping of ANSI control codes such as |
353
|
|
|
* FG\_*COLOR* or [[BOLD]] to a set of css style definitions. |
354
|
|
|
* The CSS style definitions are represented as an array where the array keys correspond |
355
|
|
|
* to the css style attribute names and the values are the css values. |
356
|
|
|
* values may be arrays that will be merged and imploded with `' '` when rendered. |
357
|
|
|
* @return string HTML representation of the ANSI formatted string |
358
|
|
|
*/ |
359
|
15 |
|
public static function ansiToHtml($string, $styleMap = []) |
360
|
|
|
{ |
361
|
|
|
$styleMap = [ |
362
|
|
|
// http://www.w3.org/TR/CSS2/syndata.html#value-def-color |
363
|
15 |
|
self::FG_BLACK => ['color' => 'black'], |
364
|
15 |
|
self::FG_BLUE => ['color' => 'blue'], |
365
|
15 |
|
self::FG_CYAN => ['color' => 'aqua'], |
366
|
15 |
|
self::FG_GREEN => ['color' => 'lime'], |
367
|
15 |
|
self::FG_GREY => ['color' => 'silver'], |
368
|
|
|
// http://meyerweb.com/eric/thoughts/2014/06/19/rebeccapurple/ |
369
|
|
|
// http://dev.w3.org/csswg/css-color/#valuedef-rebeccapurple |
370
|
15 |
|
self::FG_PURPLE => ['color' => 'rebeccapurple'], |
371
|
15 |
|
self::FG_RED => ['color' => 'red'], |
372
|
15 |
|
self::FG_YELLOW => ['color' => 'yellow'], |
373
|
15 |
|
self::BG_BLACK => ['background-color' => 'black'], |
374
|
15 |
|
self::BG_BLUE => ['background-color' => 'blue'], |
375
|
15 |
|
self::BG_CYAN => ['background-color' => 'aqua'], |
376
|
15 |
|
self::BG_GREEN => ['background-color' => 'lime'], |
377
|
15 |
|
self::BG_GREY => ['background-color' => 'silver'], |
378
|
15 |
|
self::BG_PURPLE => ['background-color' => 'rebeccapurple'], |
379
|
15 |
|
self::BG_RED => ['background-color' => 'red'], |
380
|
15 |
|
self::BG_YELLOW => ['background-color' => 'yellow'], |
381
|
15 |
|
self::BOLD => ['font-weight' => 'bold'], |
382
|
15 |
|
self::ITALIC => ['font-style' => 'italic'], |
383
|
15 |
|
self::UNDERLINE => ['text-decoration' => ['underline']], |
384
|
15 |
|
self::OVERLINED => ['text-decoration' => ['overline']], |
385
|
15 |
|
self::CROSSED_OUT => ['text-decoration' => ['line-through']], |
386
|
15 |
|
self::BLINK => ['text-decoration' => ['blink']], |
387
|
15 |
|
self::CONCEALED => ['visibility' => 'hidden'], |
388
|
15 |
|
] + $styleMap; |
389
|
|
|
|
390
|
15 |
|
$tags = 0; |
391
|
15 |
|
$result = preg_replace_callback( |
392
|
15 |
|
'/\033\[([\d;]+)m/', |
393
|
|
|
function ($ansi) use (&$tags, $styleMap) { |
394
|
14 |
|
$style = []; |
395
|
14 |
|
$reset = false; |
396
|
14 |
|
$negative = false; |
397
|
14 |
|
foreach (explode(';', $ansi[1]) as $controlCode) { |
398
|
14 |
|
if ($controlCode == 0) { |
399
|
14 |
|
$style = []; |
400
|
14 |
|
$reset = true; |
401
|
11 |
|
} elseif ($controlCode == self::NEGATIVE) { |
402
|
2 |
|
$negative = true; |
403
|
10 |
|
} elseif (isset($styleMap[$controlCode])) { |
404
|
10 |
|
$style[] = $styleMap[$controlCode]; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
408
|
14 |
|
$return = ''; |
409
|
14 |
|
while ($reset && $tags > 0) { |
410
|
10 |
|
$return .= '</span>'; |
411
|
10 |
|
$tags--; |
412
|
|
|
} |
413
|
14 |
|
if (empty($style)) { |
414
|
14 |
|
return $return; |
415
|
|
|
} |
416
|
|
|
|
417
|
10 |
|
$currentStyle = []; |
418
|
10 |
|
foreach ($style as $content) { |
419
|
10 |
|
$currentStyle = ArrayHelper::merge($currentStyle, $content); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
// if negative is set, invert background and foreground |
423
|
10 |
|
if ($negative) { |
424
|
1 |
|
if (isset($currentStyle['color'])) { |
425
|
1 |
|
$fgColor = $currentStyle['color']; |
426
|
1 |
|
unset($currentStyle['color']); |
427
|
|
|
} |
428
|
1 |
|
if (isset($currentStyle['background-color'])) { |
429
|
1 |
|
$bgColor = $currentStyle['background-color']; |
430
|
1 |
|
unset($currentStyle['background-color']); |
431
|
|
|
} |
432
|
1 |
|
if (isset($fgColor)) { |
433
|
1 |
|
$currentStyle['background-color'] = $fgColor; |
434
|
|
|
} |
435
|
1 |
|
if (isset($bgColor)) { |
436
|
1 |
|
$currentStyle['color'] = $bgColor; |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
440
|
10 |
|
$styleString = ''; |
441
|
10 |
|
foreach ($currentStyle as $name => $value) { |
442
|
10 |
|
if (is_array($value)) { |
443
|
1 |
|
$value = implode(' ', $value); |
444
|
|
|
} |
445
|
10 |
|
$styleString .= "$name: $value;"; |
446
|
|
|
} |
447
|
10 |
|
$tags++; |
448
|
10 |
|
return "$return<span style=\"$styleString\">"; |
449
|
15 |
|
}, |
450
|
|
|
$string |
451
|
|
|
); |
452
|
15 |
|
while ($tags > 0) { |
453
|
|
|
$result .= '</span>'; |
454
|
|
|
$tags--; |
455
|
|
|
} |
456
|
|
|
|
457
|
15 |
|
return $result; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Converts Markdown to be better readable in console environments by applying some ANSI format. |
462
|
|
|
* @param string $markdown the markdown string. |
463
|
|
|
* @return string the parsed result as ANSI formatted string. |
464
|
|
|
*/ |
465
|
2 |
|
public static function markdownToAnsi($markdown) |
466
|
|
|
{ |
467
|
2 |
|
$parser = new ConsoleMarkdown(); |
468
|
2 |
|
return $parser->parse($markdown); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Converts a string to ansi formatted by replacing patterns like %y (for yellow) with ansi control codes. |
473
|
|
|
* |
474
|
|
|
* Uses almost the same syntax as https://github.com/pear/Console_Color2/blob/master/Console/Color2.php |
475
|
|
|
* The conversion table is: ('bold' meaning 'light' on some |
476
|
|
|
* terminals). It's almost the same conversion table irssi uses. |
477
|
|
|
* <pre> |
478
|
|
|
* text text background |
479
|
|
|
* ------------------------------------------------ |
480
|
|
|
* %k %K %0 black dark grey black |
481
|
|
|
* %r %R %1 red bold red red |
482
|
|
|
* %g %G %2 green bold green green |
483
|
|
|
* %y %Y %3 yellow bold yellow yellow |
484
|
|
|
* %b %B %4 blue bold blue blue |
485
|
|
|
* %m %M %5 magenta bold magenta magenta |
486
|
|
|
* %p %P magenta (think: purple) |
487
|
|
|
* %c %C %6 cyan bold cyan cyan |
488
|
|
|
* %w %W %7 white bold white white |
489
|
|
|
* |
490
|
|
|
* %F Blinking, Flashing |
491
|
|
|
* %U Underline |
492
|
|
|
* %8 Reverse |
493
|
|
|
* %_,%9 Bold |
494
|
|
|
* |
495
|
|
|
* %n Resets the color |
496
|
|
|
* %% A single % |
497
|
|
|
* </pre> |
498
|
|
|
* First param is the string to convert, second is an optional flag if |
499
|
|
|
* colors should be used. It defaults to true, if set to false, the |
500
|
|
|
* color codes will just be removed (And %% will be transformed into %) |
501
|
|
|
* |
502
|
|
|
* @param string $string String to convert |
503
|
|
|
* @param bool $colored Should the string be colored? |
504
|
|
|
* @return string |
505
|
|
|
*/ |
506
|
3 |
|
public static function renderColoredString($string, $colored = true) |
507
|
|
|
{ |
508
|
|
|
// TODO rework/refactor according to https://github.com/yiisoft/yii2/issues/746 |
509
|
3 |
|
static $conversions = [ |
510
|
|
|
'%y' => [self::FG_YELLOW], |
511
|
|
|
'%g' => [self::FG_GREEN], |
512
|
|
|
'%b' => [self::FG_BLUE], |
513
|
|
|
'%r' => [self::FG_RED], |
514
|
|
|
'%p' => [self::FG_PURPLE], |
515
|
|
|
'%m' => [self::FG_PURPLE], |
516
|
|
|
'%c' => [self::FG_CYAN], |
517
|
|
|
'%w' => [self::FG_GREY], |
518
|
|
|
'%k' => [self::FG_BLACK], |
519
|
|
|
'%n' => [0], // reset |
520
|
|
|
'%Y' => [self::FG_YELLOW, self::BOLD], |
521
|
|
|
'%G' => [self::FG_GREEN, self::BOLD], |
522
|
|
|
'%B' => [self::FG_BLUE, self::BOLD], |
523
|
|
|
'%R' => [self::FG_RED, self::BOLD], |
524
|
|
|
'%P' => [self::FG_PURPLE, self::BOLD], |
525
|
|
|
'%M' => [self::FG_PURPLE, self::BOLD], |
526
|
|
|
'%C' => [self::FG_CYAN, self::BOLD], |
527
|
|
|
'%W' => [self::FG_GREY, self::BOLD], |
528
|
|
|
'%K' => [self::FG_BLACK, self::BOLD], |
529
|
|
|
'%N' => [0, self::BOLD], |
530
|
|
|
'%3' => [self::BG_YELLOW], |
531
|
|
|
'%2' => [self::BG_GREEN], |
532
|
|
|
'%4' => [self::BG_BLUE], |
533
|
|
|
'%1' => [self::BG_RED], |
534
|
|
|
'%5' => [self::BG_PURPLE], |
535
|
|
|
'%6' => [self::BG_CYAN], |
536
|
|
|
'%7' => [self::BG_GREY], |
537
|
|
|
'%0' => [self::BG_BLACK], |
538
|
|
|
'%F' => [self::BLINK], |
539
|
|
|
'%U' => [self::UNDERLINE], |
540
|
|
|
'%8' => [self::NEGATIVE], |
541
|
|
|
'%9' => [self::BOLD], |
542
|
|
|
'%_' => [self::BOLD], |
543
|
|
|
]; |
544
|
|
|
|
545
|
3 |
|
if ($colored) { |
546
|
3 |
|
$string = str_replace('%%', '% ', $string); |
547
|
3 |
|
foreach ($conversions as $key => $value) { |
548
|
3 |
|
$string = str_replace( |
549
|
3 |
|
$key, |
550
|
3 |
|
static::ansiFormatCode($value), |
551
|
|
|
$string |
552
|
|
|
); |
553
|
|
|
} |
554
|
3 |
|
$string = str_replace('% ', '%', $string); |
555
|
|
|
} else { |
556
|
1 |
|
$string = preg_replace('/%((%)|.)/', '$2', $string); |
557
|
|
|
} |
558
|
|
|
|
559
|
3 |
|
return $string; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Escapes % so they don't get interpreted as color codes when |
564
|
|
|
* the string is parsed by [[renderColoredString]]. |
565
|
|
|
* |
566
|
|
|
* @param string $string String to escape |
567
|
|
|
* |
568
|
|
|
* @return string |
569
|
|
|
*/ |
570
|
|
|
public static function escape($string) |
571
|
|
|
{ |
572
|
|
|
// TODO rework/refactor according to https://github.com/yiisoft/yii2/issues/746 |
573
|
|
|
return str_replace('%', '%%', $string); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
/** |
577
|
|
|
* Returns true if the stream supports colorization. ANSI colors are disabled if not supported by the stream. |
578
|
|
|
* |
579
|
|
|
* - windows without ansicon |
580
|
|
|
* - not tty consoles |
581
|
|
|
* |
582
|
|
|
* @param mixed $stream |
583
|
|
|
* @return bool true if the stream supports ANSI colors, otherwise false. |
584
|
|
|
*/ |
585
|
6 |
|
public static function streamSupportsAnsiColors($stream) |
586
|
|
|
{ |
587
|
6 |
|
return DIRECTORY_SEPARATOR === '\\' |
588
|
|
|
? getenv('ANSICON') !== false || getenv('ConEmuANSI') === 'ON' |
589
|
6 |
|
: function_exists('posix_isatty') && @posix_isatty($stream); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Returns true if the console is running on windows. |
594
|
|
|
* @return bool |
595
|
|
|
*/ |
596
|
1 |
|
public static function isRunningOnWindows() |
597
|
|
|
{ |
598
|
1 |
|
return DIRECTORY_SEPARATOR === '\\'; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Returns terminal screen size. |
603
|
|
|
* |
604
|
|
|
* Usage: |
605
|
|
|
* |
606
|
|
|
* ```php |
607
|
|
|
* list($width, $height) = ConsoleHelper::getScreenSize(); |
608
|
|
|
* ``` |
609
|
|
|
* |
610
|
|
|
* @param bool $refresh whether to force checking and not re-use cached size value. |
611
|
|
|
* This is useful to detect changing window size while the application is running but may |
612
|
|
|
* not get up to date values on every terminal. |
613
|
|
|
* @return array|bool An array of ($width, $height) or false when it was not able to determine size. |
614
|
|
|
*/ |
615
|
2 |
|
public static function getScreenSize($refresh = false) |
616
|
|
|
{ |
617
|
2 |
|
static $size; |
618
|
2 |
|
if ($size !== null && !$refresh) { |
619
|
2 |
|
return $size; |
620
|
|
|
} |
621
|
|
|
|
622
|
1 |
|
if (static::isRunningOnWindows()) { |
623
|
|
|
$output = []; |
624
|
|
|
exec('mode con', $output); |
625
|
|
|
if (isset($output[1]) && strpos($output[1], 'CON') !== false) { |
626
|
|
|
return $size = [(int) preg_replace('~\D~', '', $output[4]), (int) preg_replace('~\D~', '', $output[3])]; |
627
|
|
|
} |
628
|
|
|
} else { |
629
|
|
|
// try stty if available |
630
|
1 |
|
$stty = []; |
631
|
1 |
|
if (exec('stty -a 2>&1', $stty)) { |
632
|
1 |
|
$stty = implode(' ', $stty); |
633
|
|
|
|
634
|
|
|
// Linux stty output |
635
|
1 |
|
if (preg_match('/rows\s+(\d+);\s*columns\s+(\d+);/mi', $stty, $matches)) { |
636
|
1 |
|
return $size = [(int) $matches[2], (int) $matches[1]]; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
// MacOS stty output |
640
|
|
|
if (preg_match('/(\d+)\s+rows;\s*(\d+)\s+columns;/mi', $stty, $matches)) { |
641
|
|
|
return $size = [(int) $matches[2], (int) $matches[1]]; |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
// fallback to tput, which may not be updated on terminal resize |
646
|
|
|
if (($width = (int) exec('tput cols 2>&1')) > 0 && ($height = (int) exec('tput lines 2>&1')) > 0) { |
647
|
|
|
return $size = [$width, $height]; |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
// fallback to ENV variables, which may not be updated on terminal resize |
651
|
|
|
if (($width = (int) getenv('COLUMNS')) > 0 && ($height = (int) getenv('LINES')) > 0) { |
652
|
|
|
return $size = [$width, $height]; |
653
|
|
|
} |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
return $size = false; |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Word wrap text with indentation to fit the screen size. |
661
|
|
|
* |
662
|
|
|
* If screen size could not be detected, or the indentation is greater than the screen size, the text will not be wrapped. |
663
|
|
|
* |
664
|
|
|
* The first line will **not** be indented, so `Console::wrapText("Lorem ipsum dolor sit amet.", 4)` will result in the |
665
|
|
|
* following output, given the screen width is 16 characters: |
666
|
|
|
* |
667
|
|
|
* ``` |
668
|
|
|
* Lorem ipsum |
669
|
|
|
* dolor sit |
670
|
|
|
* amet. |
671
|
|
|
* ``` |
672
|
|
|
* |
673
|
|
|
* @param string $text the text to be wrapped |
674
|
|
|
* @param int $indent number of spaces to use for indentation. |
675
|
|
|
* @param bool $refresh whether to force refresh of screen size. |
676
|
|
|
* This will be passed to [[getScreenSize()]]. |
677
|
|
|
* @return string the wrapped text. |
678
|
|
|
* @since 2.0.4 |
679
|
|
|
*/ |
680
|
2 |
|
public static function wrapText($text, $indent = 0, $refresh = false) |
681
|
|
|
{ |
682
|
2 |
|
$size = static::getScreenSize($refresh); |
683
|
2 |
|
if ($size === false || $size[0] <= $indent) { |
684
|
|
|
return $text; |
685
|
|
|
} |
686
|
2 |
|
$pad = str_repeat(' ', $indent); |
687
|
2 |
|
$lines = explode("\n", wordwrap($text, $size[0] - $indent, "\n")); |
688
|
2 |
|
$first = true; |
689
|
2 |
|
foreach ($lines as $i => $line) { |
690
|
2 |
|
if ($first) { |
691
|
2 |
|
$first = false; |
692
|
2 |
|
continue; |
693
|
|
|
} |
694
|
2 |
|
$lines[$i] = $pad . $line; |
695
|
|
|
} |
696
|
|
|
|
697
|
2 |
|
return implode("\n", $lines); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* Gets input from STDIN and returns a string right-trimmed for EOLs. |
702
|
|
|
* |
703
|
|
|
* @param bool $raw If set to true, returns the raw string without trimming |
704
|
|
|
* @return string the string read from stdin |
705
|
|
|
*/ |
706
|
|
|
public static function stdin($raw = false) |
707
|
|
|
{ |
708
|
|
|
return $raw ? fgets(\STDIN) : rtrim(fgets(\STDIN), PHP_EOL); |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
/** |
712
|
|
|
* Prints a string to STDOUT. |
713
|
|
|
* |
714
|
|
|
* @param string $string the string to print |
715
|
|
|
* @return int|bool Number of bytes printed or false on error |
716
|
|
|
*/ |
717
|
|
|
public static function stdout($string) |
718
|
|
|
{ |
719
|
|
|
return fwrite(\STDOUT, $string); |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Prints a string to STDERR. |
724
|
|
|
* |
725
|
|
|
* @param string $string the string to print |
726
|
|
|
* @return int|bool Number of bytes printed or false on error |
727
|
|
|
*/ |
728
|
|
|
public static function stderr($string) |
729
|
|
|
{ |
730
|
|
|
return fwrite(\STDERR, $string); |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Asks the user for input. Ends when the user types a carriage return (PHP_EOL). Optionally, It also provides a |
735
|
|
|
* prompt. |
736
|
|
|
* |
737
|
|
|
* @param string $prompt the prompt to display before waiting for input (optional) |
738
|
|
|
* @return string the user's input |
739
|
|
|
*/ |
740
|
|
|
public static function input($prompt = null) |
741
|
|
|
{ |
742
|
|
|
if (isset($prompt)) { |
743
|
|
|
static::stdout($prompt); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
return static::stdin(); |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Prints text to STDOUT appended with a carriage return (PHP_EOL). |
751
|
|
|
* |
752
|
|
|
* @param string $string the text to print |
753
|
|
|
* @return int|bool number of bytes printed or false on error. |
754
|
|
|
*/ |
755
|
|
|
public static function output($string = null) |
756
|
|
|
{ |
757
|
|
|
return static::stdout($string . PHP_EOL); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* Prints text to STDERR appended with a carriage return (PHP_EOL). |
762
|
|
|
* |
763
|
|
|
* @param string $string the text to print |
764
|
|
|
* @return int|bool number of bytes printed or false on error. |
765
|
|
|
*/ |
766
|
|
|
public static function error($string = null) |
767
|
|
|
{ |
768
|
|
|
return static::stderr($string . PHP_EOL); |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* Prompts the user for input and validates it. |
773
|
|
|
* |
774
|
|
|
* @param string $text prompt string |
775
|
|
|
* @param array $options the options to validate the input: |
776
|
|
|
* |
777
|
|
|
* - `required`: whether it is required or not |
778
|
|
|
* - `default`: default value if no input is inserted by the user |
779
|
|
|
* - `pattern`: regular expression pattern to validate user input |
780
|
|
|
* - `validator`: a callable function to validate input. The function must accept two parameters: |
781
|
|
|
* - `input`: the user input to validate |
782
|
|
|
* - `error`: the error value passed by reference if validation failed. |
783
|
|
|
* |
784
|
|
|
* @return string the user input |
785
|
|
|
*/ |
786
|
|
|
public static function prompt($text, $options = []) |
787
|
|
|
{ |
788
|
|
|
$options = ArrayHelper::merge( |
789
|
|
|
[ |
790
|
|
|
'required' => false, |
791
|
|
|
'default' => null, |
792
|
|
|
'pattern' => null, |
793
|
|
|
'validator' => null, |
794
|
|
|
'error' => 'Invalid input.', |
795
|
|
|
], |
796
|
|
|
$options |
797
|
|
|
); |
798
|
|
|
$error = null; |
799
|
|
|
|
800
|
|
|
top: |
801
|
|
|
$input = $options['default'] |
802
|
|
|
? static::input("$text [" . $options['default'] . '] ') |
803
|
|
|
: static::input("$text "); |
804
|
|
|
|
805
|
|
|
if ($input === '') { |
806
|
|
|
if (isset($options['default'])) { |
807
|
|
|
$input = $options['default']; |
808
|
|
|
} elseif ($options['required']) { |
809
|
|
|
static::output($options['error']); |
810
|
|
|
goto top; |
811
|
|
|
} |
812
|
|
|
} elseif ($options['pattern'] && !preg_match($options['pattern'], $input)) { |
813
|
|
|
static::output($options['error']); |
814
|
|
|
goto top; |
815
|
|
|
} elseif ($options['validator'] && |
816
|
|
|
!call_user_func_array($options['validator'], [$input, &$error]) |
817
|
|
|
) { |
818
|
|
|
static::output(isset($error) ? $error : $options['error']); |
819
|
|
|
goto top; |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
return $input; |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
/** |
826
|
|
|
* Asks user to confirm by typing y or n. |
827
|
|
|
* |
828
|
|
|
* A typical usage looks like the following: |
829
|
|
|
* |
830
|
|
|
* ```php |
831
|
|
|
* if (Console::confirm("Are you sure?")) { |
832
|
|
|
* echo "user typed yes\n"; |
833
|
|
|
* } else { |
834
|
|
|
* echo "user typed no\n"; |
835
|
|
|
* } |
836
|
|
|
* ``` |
837
|
|
|
* |
838
|
|
|
* @param string $message to print out before waiting for user input |
839
|
|
|
* @param bool $default this value is returned if no selection is made. |
840
|
|
|
* @return bool whether user confirmed |
841
|
|
|
*/ |
842
|
|
|
public static function confirm($message, $default = false) |
843
|
|
|
{ |
844
|
|
|
while (true) { |
845
|
|
|
static::stdout($message . ' (yes|no) [' . ($default ? 'yes' : 'no') . ']:'); |
846
|
|
|
$input = trim(static::stdin()); |
847
|
|
|
|
848
|
|
|
if (empty($input)) { |
849
|
|
|
return $default; |
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
if (!strcasecmp($input, 'y') || !strcasecmp($input, 'yes')) { |
853
|
|
|
return true; |
854
|
|
|
} |
855
|
|
|
|
856
|
|
|
if (!strcasecmp($input, 'n') || !strcasecmp($input, 'no')) { |
857
|
|
|
return false; |
858
|
|
|
} |
859
|
|
|
} |
860
|
|
|
} |
861
|
|
|
|
862
|
|
|
/** |
863
|
|
|
* Gives the user an option to choose from. Giving '?' as an input will show |
864
|
|
|
* a list of options to choose from and their explanations. |
865
|
|
|
* |
866
|
|
|
* @param string $prompt the prompt message |
867
|
|
|
* @param array $options Key-value array of options to choose from. Key is what is inputed and used, value is |
868
|
|
|
* what's displayed to end user by help command. |
869
|
|
|
* |
870
|
|
|
* @return string An option character the user chose |
871
|
|
|
*/ |
872
|
|
|
public static function select($prompt, $options = []) |
873
|
|
|
{ |
874
|
|
|
top: |
875
|
|
|
static::stdout("$prompt [" . implode(',', array_keys($options)) . ',?]: '); |
876
|
|
|
$input = static::stdin(); |
877
|
|
|
if ($input === '?') { |
878
|
|
|
foreach ($options as $key => $value) { |
879
|
|
|
static::output(" $key - $value"); |
880
|
|
|
} |
881
|
|
|
static::output(' ? - Show help'); |
882
|
|
|
goto top; |
883
|
|
|
} elseif (!array_key_exists($input, $options)) { |
884
|
|
|
goto top; |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
return $input; |
888
|
|
|
} |
889
|
|
|
|
890
|
|
|
private static $_progressStart; |
891
|
|
|
private static $_progressWidth; |
892
|
|
|
private static $_progressPrefix; |
893
|
|
|
private static $_progressEta; |
894
|
|
|
private static $_progressEtaLastDone = 0; |
895
|
|
|
private static $_progressEtaLastUpdate; |
896
|
|
|
|
897
|
|
|
/** |
898
|
|
|
* Starts display of a progress bar on screen. |
899
|
|
|
* |
900
|
|
|
* This bar will be updated by [[updateProgress()]] and may be ended by [[endProgress()]]. |
901
|
|
|
* |
902
|
|
|
* The following example shows a simple usage of a progress bar: |
903
|
|
|
* |
904
|
|
|
* ```php |
905
|
|
|
* Console::startProgress(0, 1000); |
906
|
|
|
* for ($n = 1; $n <= 1000; $n++) { |
907
|
|
|
* usleep(1000); |
908
|
|
|
* Console::updateProgress($n, 1000); |
909
|
|
|
* } |
910
|
|
|
* Console::endProgress(); |
911
|
|
|
* ``` |
912
|
|
|
* |
913
|
|
|
* Git clone like progress (showing only status information): |
914
|
|
|
* |
915
|
|
|
* ```php |
916
|
|
|
* Console::startProgress(0, 1000, 'Counting objects: ', false); |
917
|
|
|
* for ($n = 1; $n <= 1000; $n++) { |
918
|
|
|
* usleep(1000); |
919
|
|
|
* Console::updateProgress($n, 1000); |
920
|
|
|
* } |
921
|
|
|
* Console::endProgress("done." . PHP_EOL); |
922
|
|
|
* ``` |
923
|
|
|
* |
924
|
|
|
* @param int $done the number of items that are completed. |
925
|
|
|
* @param int $total the total value of items that are to be done. |
926
|
|
|
* @param string $prefix an optional string to display before the progress bar. |
927
|
|
|
* Default to empty string which results in no prefix to be displayed. |
928
|
|
|
* @param int|bool $width optional width of the progressbar. This can be an integer representing |
929
|
|
|
* the number of characters to display for the progress bar or a float between 0 and 1 representing the |
930
|
|
|
* percentage of screen with the progress bar may take. It can also be set to false to disable the |
931
|
|
|
* bar and only show progress information like percent, number of items and ETA. |
932
|
|
|
* If not set, the bar will be as wide as the screen. Screen size will be detected using [[getScreenSize()]]. |
933
|
|
|
* @see startProgress |
934
|
|
|
* @see updateProgress |
935
|
|
|
* @see endProgress |
936
|
|
|
*/ |
937
|
|
|
public static function startProgress($done, $total, $prefix = '', $width = null) |
938
|
|
|
{ |
939
|
|
|
self::$_progressStart = time(); |
940
|
|
|
self::$_progressWidth = $width; |
941
|
|
|
self::$_progressPrefix = $prefix; |
942
|
|
|
self::$_progressEta = null; |
943
|
|
|
self::$_progressEtaLastDone = 0; |
944
|
|
|
self::$_progressEtaLastUpdate = time(); |
945
|
|
|
|
946
|
|
|
static::updateProgress($done, $total); |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
/** |
950
|
|
|
* Updates a progress bar that has been started by [[startProgress()]]. |
951
|
|
|
* |
952
|
|
|
* @param int $done the number of items that are completed. |
953
|
|
|
* @param int $total the total value of items that are to be done. |
954
|
|
|
* @param string $prefix an optional string to display before the progress bar. |
955
|
|
|
* Defaults to null meaning the prefix specified by [[startProgress()]] will be used. |
956
|
|
|
* If prefix is specified it will update the prefix that will be used by later calls. |
957
|
|
|
* @see startProgress |
958
|
|
|
* @see endProgress |
959
|
|
|
*/ |
960
|
|
|
public static function updateProgress($done, $total, $prefix = null) |
961
|
|
|
{ |
962
|
|
|
if ($prefix === null) { |
963
|
|
|
$prefix = self::$_progressPrefix; |
964
|
|
|
} else { |
965
|
|
|
self::$_progressPrefix = $prefix; |
966
|
|
|
} |
967
|
|
|
$width = static::getProgressbarWidth($prefix); |
968
|
|
|
$percent = ($total == 0) ? 1 : $done / $total; |
969
|
|
|
$info = sprintf('%d%% (%d/%d)', $percent * 100, $done, $total); |
970
|
|
|
self::setETA($done, $total); |
971
|
|
|
$info .= self::$_progressEta === null ? ' ETA: n/a' : sprintf(' ETA: %d sec.', self::$_progressEta); |
972
|
|
|
|
973
|
|
|
// Number extra characters outputted. These are opening [, closing ], and space before info |
974
|
|
|
// Since Windows uses \r\n\ for line endings, there's one more in the case |
975
|
|
|
$extraChars = static::isRunningOnWindows() ? 4 : 3; |
976
|
|
|
$width -= $extraChars + static::ansiStrlen($info); |
977
|
|
|
// skipping progress bar on very small display or if forced to skip |
978
|
|
|
if ($width < 5) { |
979
|
|
|
static::stdout("\r$prefix$info "); |
980
|
|
|
} else { |
981
|
|
|
if ($percent < 0) { |
982
|
|
|
$percent = 0; |
983
|
|
|
} elseif ($percent > 1) { |
984
|
|
|
$percent = 1; |
985
|
|
|
} |
986
|
|
|
$bar = floor($percent * $width); |
987
|
|
|
$status = str_repeat('=', $bar); |
|
|
|
|
988
|
|
|
if ($bar < $width) { |
989
|
|
|
$status .= '>'; |
990
|
|
|
$status .= str_repeat(' ', $width - $bar - 1); |
991
|
|
|
} |
992
|
|
|
static::stdout("\r$prefix" . "[$status] $info"); |
993
|
|
|
} |
994
|
|
|
flush(); |
995
|
|
|
} |
996
|
|
|
|
997
|
|
|
/** |
998
|
|
|
* Return width of the progressbar |
999
|
|
|
* @param string $prefix an optional string to display before the progress bar. |
1000
|
|
|
* @see updateProgress |
1001
|
|
|
* @return int screen width |
1002
|
|
|
* @since 2.0.14 |
1003
|
|
|
*/ |
1004
|
|
|
private static function getProgressbarWidth($prefix) |
1005
|
|
|
{ |
1006
|
|
|
$width = self::$_progressWidth; |
1007
|
|
|
|
1008
|
|
|
if ($width === false) { |
1009
|
|
|
return 0; |
1010
|
|
|
} |
1011
|
|
|
|
1012
|
|
|
$screenSize = static::getScreenSize(true); |
1013
|
|
|
if ($screenSize === false && $width < 1) { |
|
|
|
|
1014
|
|
|
return 0; |
1015
|
|
|
} |
1016
|
|
|
|
1017
|
|
|
if ($width === null) { |
1018
|
|
|
$width = $screenSize[0]; |
1019
|
|
|
} elseif ($width > 0 && $width < 1) { |
1020
|
|
|
$width = floor($screenSize[0] * $width); |
1021
|
|
|
} |
1022
|
|
|
|
1023
|
|
|
$width -= static::ansiStrlen($prefix); |
1024
|
|
|
|
1025
|
|
|
return $width; |
1026
|
|
|
} |
1027
|
|
|
|
1028
|
|
|
/** |
1029
|
|
|
* Calculate $_progressEta, $_progressEtaLastUpdate and $_progressEtaLastDone |
1030
|
|
|
* @param int $done the number of items that are completed. |
1031
|
|
|
* @param int $total the total value of items that are to be done. |
1032
|
|
|
* @see updateProgress |
1033
|
|
|
* @since 2.0.14 |
1034
|
|
|
*/ |
1035
|
|
|
private static function setETA($done, $total) |
1036
|
|
|
{ |
1037
|
|
|
if ($done > $total || $done == 0) { |
1038
|
|
|
self::$_progressEta = null; |
1039
|
|
|
self::$_progressEtaLastUpdate = time(); |
1040
|
|
|
return; |
1041
|
|
|
} |
1042
|
|
|
|
1043
|
|
|
if ($done < $total && (time() - self::$_progressEtaLastUpdate > 1 && $done > self::$_progressEtaLastDone)) { |
1044
|
|
|
$rate = (time() - (self::$_progressEtaLastUpdate ?: self::$_progressStart)) / ($done - self::$_progressEtaLastDone); |
1045
|
|
|
self::$_progressEta = $rate * ($total - $done); |
1046
|
|
|
self::$_progressEtaLastUpdate = time(); |
1047
|
|
|
self::$_progressEtaLastDone = $done; |
1048
|
|
|
} |
1049
|
|
|
} |
1050
|
|
|
|
1051
|
|
|
/** |
1052
|
|
|
* Ends a progress bar that has been started by [[startProgress()]]. |
1053
|
|
|
* |
1054
|
|
|
* @param string|bool $remove This can be `false` to leave the progress bar on screen and just print a newline. |
1055
|
|
|
* If set to `true`, the line of the progress bar will be cleared. This may also be a string to be displayed instead |
1056
|
|
|
* of the progress bar. |
1057
|
|
|
* @param bool $keepPrefix whether to keep the prefix that has been specified for the progressbar when progressbar |
1058
|
|
|
* gets removed. Defaults to true. |
1059
|
|
|
* @see startProgress |
1060
|
|
|
* @see updateProgress |
1061
|
|
|
*/ |
1062
|
|
|
public static function endProgress($remove = false, $keepPrefix = true) |
1063
|
|
|
{ |
1064
|
|
|
if ($remove === false) { |
1065
|
|
|
static::stdout(PHP_EOL); |
1066
|
|
|
} else { |
1067
|
|
|
if (static::streamSupportsAnsiColors(STDOUT)) { |
1068
|
|
|
static::clearLine(); |
1069
|
|
|
} |
1070
|
|
|
static::stdout("\r" . ($keepPrefix ? self::$_progressPrefix : '') . (is_string($remove) ? $remove : '')); |
1071
|
|
|
} |
1072
|
|
|
flush(); |
1073
|
|
|
|
1074
|
|
|
self::$_progressStart = null; |
1075
|
|
|
self::$_progressWidth = null; |
1076
|
|
|
self::$_progressPrefix = ''; |
1077
|
|
|
self::$_progressEta = null; |
1078
|
|
|
self::$_progressEtaLastDone = 0; |
1079
|
|
|
self::$_progressEtaLastUpdate = null; |
1080
|
|
|
} |
1081
|
|
|
|
1082
|
|
|
/** |
1083
|
|
|
* Generates a summary of the validation errors. |
1084
|
|
|
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
1085
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
1086
|
|
|
* |
1087
|
|
|
* - showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise |
1088
|
|
|
* only the first error message for each attribute will be shown. Defaults to `false`. |
1089
|
|
|
* |
1090
|
|
|
* @return string the generated error summary |
1091
|
|
|
* @since 2.0.14 |
1092
|
|
|
*/ |
1093
|
1 |
|
public static function errorSummary($models, $options = []) |
1094
|
|
|
{ |
1095
|
1 |
|
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false); |
1096
|
1 |
|
$lines = self::collectErrors($models, $showAllErrors); |
1097
|
|
|
|
1098
|
1 |
|
return implode(PHP_EOL, $lines); |
1099
|
|
|
} |
1100
|
|
|
|
1101
|
|
|
/** |
1102
|
|
|
* Return array of the validation errors |
1103
|
|
|
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
1104
|
|
|
* @param $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise |
1105
|
|
|
* only the first error message for each attribute will be shown. |
1106
|
|
|
* @return array of the validation errors |
1107
|
|
|
* @since 2.0.14 |
1108
|
|
|
*/ |
1109
|
1 |
|
private static function collectErrors($models, $showAllErrors) |
1110
|
|
|
{ |
1111
|
1 |
|
$lines = []; |
1112
|
1 |
|
if (!is_array($models)) { |
1113
|
1 |
|
$models = [$models]; |
1114
|
|
|
} |
1115
|
|
|
|
1116
|
1 |
|
foreach ($models as $model) { |
1117
|
1 |
|
$lines = array_unique(array_merge($lines, $model->getErrorSummary($showAllErrors))); |
1118
|
|
|
} |
1119
|
|
|
|
1120
|
1 |
|
return $lines; |
1121
|
|
|
} |
1122
|
|
|
} |
1123
|
|
|
|