1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Console |
4
|
|
|
* |
5
|
|
|
* @link https://www.icy2003.com/ |
6
|
|
|
* @author icy2003 <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
8
|
|
|
*/ |
9
|
|
|
namespace icy2003\php\ihelpers; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* 控制台类 |
15
|
|
|
* |
16
|
|
|
* 文字输出和命令执行 |
17
|
|
|
*/ |
18
|
|
|
class Console |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* 字体颜色控制码:黑色 |
23
|
|
|
*/ |
24
|
|
|
const FG_BLACK = 30; |
25
|
|
|
/** |
26
|
|
|
* 字体颜色控制码:红色 |
27
|
|
|
*/ |
28
|
|
|
const FG_RED = 31; |
29
|
|
|
/** |
30
|
|
|
* 字体颜色控制码:绿色 |
31
|
|
|
*/ |
32
|
|
|
const FG_GREEN = 32; |
33
|
|
|
/** |
34
|
|
|
* 字体颜色控制码:黄色 |
35
|
|
|
*/ |
36
|
|
|
const FG_YELLOW = 33; |
37
|
|
|
/** |
38
|
|
|
* 字体颜色控制码:蓝色 |
39
|
|
|
*/ |
40
|
|
|
const FG_BLUE = 34; |
41
|
|
|
/** |
42
|
|
|
* 字体颜色控制码:紫色 |
43
|
|
|
*/ |
44
|
|
|
const FG_PURPLE = 35; |
45
|
|
|
/** |
46
|
|
|
* 字体颜色控制码:青色 |
47
|
|
|
*/ |
48
|
|
|
const FG_CYAN = 36; |
49
|
|
|
/** |
50
|
|
|
* 字体颜色控制码:灰色 |
51
|
|
|
*/ |
52
|
|
|
const FG_GREY = 37; |
53
|
|
|
/** |
54
|
|
|
* 背景色控制码:黑色 |
55
|
|
|
*/ |
56
|
|
|
const BG_BLACK = 40; |
57
|
|
|
/** |
58
|
|
|
* 背景色控制码:红色 |
59
|
|
|
*/ |
60
|
|
|
const BG_RED = 41; |
61
|
|
|
/** |
62
|
|
|
* 背景色控制码:绿色 |
63
|
|
|
*/ |
64
|
|
|
const BG_GREEN = 42; |
65
|
|
|
/** |
66
|
|
|
* 背景色控制码:黄色 |
67
|
|
|
*/ |
68
|
|
|
const BG_YELLOW = 43; |
69
|
|
|
/** |
70
|
|
|
* 背景色控制码:蓝色 |
71
|
|
|
*/ |
72
|
|
|
const BG_BLUE = 44; |
73
|
|
|
/** |
74
|
|
|
* 背景色控制码:紫色 |
75
|
|
|
*/ |
76
|
|
|
const BG_PURPLE = 45; |
77
|
|
|
/** |
78
|
|
|
* 背景色控制码:青色 |
79
|
|
|
*/ |
80
|
|
|
const BG_CYAN = 46; |
81
|
|
|
/** |
82
|
|
|
* 背景色控制码:灰色 |
83
|
|
|
*/ |
84
|
|
|
const BG_GREY = 47; |
85
|
|
|
/** |
86
|
|
|
* 字体样式控制码:重置 |
87
|
|
|
*/ |
88
|
|
|
const RESET = 0; |
89
|
|
|
/** |
90
|
|
|
* 字体样式控制码:普通 |
91
|
|
|
*/ |
92
|
|
|
const NORMAL = 0; |
93
|
|
|
/** |
94
|
|
|
* 字体样式控制码:加粗 |
95
|
|
|
*/ |
96
|
|
|
const BOLD = 1; |
97
|
|
|
/** |
98
|
|
|
* 字体样式控制码:斜体 |
99
|
|
|
*/ |
100
|
|
|
const ITALIC = 3; |
101
|
|
|
/** |
102
|
|
|
* 字体样式控制码:下划线 |
103
|
|
|
*/ |
104
|
|
|
const UNDERLINE = 4; |
105
|
|
|
/** |
106
|
|
|
* 字体样式控制码:闪烁 |
107
|
|
|
*/ |
108
|
|
|
const BLINK = 5; |
109
|
|
|
/** |
110
|
|
|
* 字体样式控制码: |
111
|
|
|
*/ |
112
|
|
|
const NEGATIVE = 7; |
113
|
|
|
/** |
114
|
|
|
* 字体样式控制码:隐藏 |
115
|
|
|
*/ |
116
|
|
|
const CONCEALED = 8; |
117
|
|
|
/** |
118
|
|
|
* 字体样式控制码:交叉输出 |
119
|
|
|
*/ |
120
|
|
|
const CROSSED_OUT = 9; |
121
|
|
|
/** |
122
|
|
|
* 字体样式控制码:边框 |
123
|
|
|
*/ |
124
|
|
|
const FRAMED = 51; |
125
|
|
|
/** |
126
|
|
|
* 字体样式控制码:环绕 |
127
|
|
|
*/ |
128
|
|
|
const ENCIRCLED = 52; |
129
|
|
|
/** |
130
|
|
|
* 字体样式控制码: |
131
|
|
|
*/ |
132
|
|
|
const OVERLINED = 53; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* 标准命令行输出 |
136
|
|
|
* |
137
|
|
|
* @param string $string 输出文字 |
138
|
|
|
* |
139
|
|
|
* @return integer|false |
140
|
|
|
*/ |
141
|
|
|
public static function stdout($string) |
142
|
|
|
{ |
143
|
|
|
return fwrite(\STDOUT, $string); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* 标准命令行输入 |
148
|
|
|
* |
149
|
|
|
* @return string|false |
150
|
|
|
*/ |
151
|
|
|
public static function stdin() |
152
|
|
|
{ |
153
|
|
|
return fgets(\STDIN); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* 标准命令行错误输出 |
158
|
|
|
* |
159
|
|
|
* @param string $string 错误文字 |
160
|
|
|
* |
161
|
|
|
* @return integer|false |
162
|
|
|
*/ |
163
|
|
|
public static function stderr($string) |
164
|
|
|
{ |
165
|
|
|
return fwrite(\STDERR, $string); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* 输入提示 |
170
|
|
|
* |
171
|
|
|
* @param string $prompt 输入提示 |
172
|
|
|
* |
173
|
|
|
* @return string|false |
174
|
|
|
*/ |
175
|
|
|
public static function input($prompt = null) |
176
|
|
|
{ |
177
|
|
|
if (isset($prompt)) { |
178
|
|
|
self::output($prompt); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return self::stdin(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* 输出提示 |
186
|
|
|
* |
187
|
|
|
* @param string $string 提示文字 |
188
|
|
|
* |
189
|
|
|
* @return integer|false |
190
|
|
|
*/ |
191
|
|
|
public static function output($string = null) |
192
|
|
|
{ |
193
|
|
|
return self::stdout($string . PHP_EOL); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* ANSI 格式化 |
198
|
|
|
* |
199
|
|
|
* @param string $string ANSI 格式文本 |
200
|
|
|
* @param array $format 格式代码数组 |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public static function ansiFormat($string, $format = []) |
205
|
|
|
{ |
206
|
|
|
$code = implode(';', $format); |
207
|
|
|
|
208
|
|
|
return "\033[0m" . ($code !== '' ? "\033[" . $code . 'm' : '') . $string . "\033[0m"; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* 向终端发送 ANSI 控制代码 CUU,上移光标 n 行 |
213
|
|
|
* |
214
|
|
|
* 如果光标已经在屏幕边缘,则此操作无效 |
215
|
|
|
* |
216
|
|
|
* @param integer $rows 光标应向上移动的行数 |
217
|
|
|
* |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public static function moveCursorUp($rows = 1) |
221
|
|
|
{ |
222
|
|
|
echo '\033[' . (int) $rows . 'A'; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* 通过向终端发送 ANSI 控制代码 CUD,向下移动终端光标 |
227
|
|
|
* |
228
|
|
|
* 如果光标已经在屏幕边缘,则此操作无效 |
229
|
|
|
* |
230
|
|
|
* @param integer $rows 光标应向下移动的行数 |
231
|
|
|
* |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
public static function moveCursorDown($rows = 1) |
235
|
|
|
{ |
236
|
|
|
echo '\033[' . (int) $rows . 'B'; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* 通过向终端发送 ANSI 控制代码 CUF,向前移动终端光标 |
241
|
|
|
* |
242
|
|
|
* 如果光标已经在屏幕边缘,则此操作无效 |
243
|
|
|
* |
244
|
|
|
* @param integer $steps 光标应向前移动的步数 |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
public static function moveCursorForward($steps = 1) |
249
|
|
|
{ |
250
|
|
|
echo '\033[' . (int) $steps . 'C'; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* 通过向终端发送 ANSI 控制代码 CUB,向后移动终端光标 |
255
|
|
|
* |
256
|
|
|
* 如果光标已经在屏幕边缘,则此操作无效 |
257
|
|
|
* |
258
|
|
|
* @param integer $steps 光标应向后移动的步数 |
259
|
|
|
* |
260
|
|
|
* @return void |
261
|
|
|
*/ |
262
|
|
|
public static function moveCursorBackward($steps = 1) |
263
|
|
|
{ |
264
|
|
|
echo '\033[' . (int) $steps . 'D'; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* 向终端发送 ANSI 控制代码 CNL,让光标移到下 n 行的开头 |
269
|
|
|
* |
270
|
|
|
* @param integer $lines 光标应向下移动的行数 |
271
|
|
|
* |
272
|
|
|
* @return void |
273
|
|
|
*/ |
274
|
|
|
public static function moveCursorNextLine($lines = 1) |
275
|
|
|
{ |
276
|
|
|
echo '\033[' . (int) $lines . 'E'; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* 向终端发送 ANSI 控制代码 CPL,让光标移到上 n 行的开头 |
281
|
|
|
* |
282
|
|
|
* @param integer $lines 光标应向上移动的行数 |
283
|
|
|
* |
284
|
|
|
* @return void |
285
|
|
|
*/ |
286
|
|
|
public static function moveCursorPrevLine($lines = 1) |
287
|
|
|
{ |
288
|
|
|
echo '\033[' . (int) $lines . 'F'; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* 通过向终端发送 ANSI 控制代码 CUP 或 CPA,将光标移动到给定行和列的绝对位置上 |
293
|
|
|
* |
294
|
|
|
* @param int $column 基于 1 的列号,1 是屏幕的左边缘 |
295
|
|
|
* @param int $row 行 基于 1 的行数,1 是屏幕的上边缘。如果未设置,将只在当前行中移动光标 |
296
|
|
|
* |
297
|
|
|
* @return void |
298
|
|
|
*/ |
299
|
|
|
public static function moveCursorTo($column, $row = null) |
300
|
|
|
{ |
301
|
|
|
if ($row === null) { |
302
|
|
|
echo '\033[' . (int) $column . 'G'; |
303
|
|
|
} else { |
304
|
|
|
echo '\033[' . (int) $row . ';' . (int) $column . 'H'; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* 通过向终端发送 ANSI 控制代码 SU 来向上滚动整页 |
310
|
|
|
* |
311
|
|
|
* 在底部添加新行。在 Windows 中使用的 ANSI.SYS 不支持此操作 |
312
|
|
|
* |
313
|
|
|
* @param integer $lines 要向上滚动的行数 |
314
|
|
|
* |
315
|
|
|
* @return void |
316
|
|
|
*/ |
317
|
|
|
public static function scrollUp($lines = 1) |
318
|
|
|
{ |
319
|
|
|
echo '\033[' . (int) $lines . 'S'; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* 通过向终端发送 ANSI 控制代码 SD,向下滚动整页 |
324
|
|
|
* |
325
|
|
|
* 在顶部添加新行。在 Windows 中使用的 ANSI.SYS 不支持此操作 |
326
|
|
|
* |
327
|
|
|
* @param integer $lines 要向下滚动的行数 |
328
|
|
|
* |
329
|
|
|
* @return void |
330
|
|
|
*/ |
331
|
|
|
public static function scrollDown($lines = 1) |
332
|
|
|
{ |
333
|
|
|
echo '\033[' . (int) $lines . 'T'; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* 通过向终端发送 ANSI 控制代码 SCP 来保存当前光标位置 |
338
|
|
|
* |
339
|
|
|
* 然后可以使用 RestoreCursorPosition 恢复位置 |
340
|
|
|
* |
341
|
|
|
* @return void |
342
|
|
|
*/ |
343
|
|
|
public static function saveCursorPosition() |
344
|
|
|
{ |
345
|
|
|
echo '\033[s'; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* 通过向终端发送 ANSI 控制代码 RCP,恢复用 SaveCursorPosition 保存的光标位置 |
350
|
|
|
* |
351
|
|
|
* @return void |
352
|
|
|
*/ |
353
|
|
|
public static function restoreCursorPosition() |
354
|
|
|
{ |
355
|
|
|
echo '\033[u'; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* 通过发送 ANSI DECTCEM 代码隐藏光标到终端 |
360
|
|
|
* |
361
|
|
|
* 使用 ShowCursor 将其带回 |
362
|
|
|
* |
363
|
|
|
* 当应用程序退出时,不要忘记显示光标。退出后光标可能还隐藏在终端中 |
364
|
|
|
* |
365
|
|
|
* @return void |
366
|
|
|
*/ |
367
|
|
|
public static function hideCursor() |
368
|
|
|
{ |
369
|
|
|
echo '\033[?25l'; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* 当被光标被 hideCursor 隐藏时,通过发送 ANSI DECTCEM 代码将光标显示到终端 |
374
|
|
|
* |
375
|
|
|
* @return void |
376
|
|
|
*/ |
377
|
|
|
public static function showCursor() |
378
|
|
|
{ |
379
|
|
|
echo '\033[?25h'; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* 通过向终端发送带参数 2 的 ANSI 控制代码 ED,清除整个屏幕内容 |
384
|
|
|
* |
385
|
|
|
* 不会更改光标位置 |
386
|
|
|
* |
387
|
|
|
* 注意:在 Windows 中使用的 ANSI.SYS 实现将光标位置重置为屏幕的左上角 |
388
|
|
|
* |
389
|
|
|
* @return void |
390
|
|
|
*/ |
391
|
|
|
public static function clearScreen() |
392
|
|
|
{ |
393
|
|
|
echo '\033[2J'; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* 通过将带参数 1 的ANSI控制代码 ED 发送到终端,清除从光标到屏幕开头的文本 |
398
|
|
|
* |
399
|
|
|
* 不会更改光标位置 |
400
|
|
|
* |
401
|
|
|
* @return void |
402
|
|
|
*/ |
403
|
|
|
public static function clearScreenBeforeCursor() |
404
|
|
|
{ |
405
|
|
|
echo '\033[1J'; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* 通过将带参数 0 的 ANSI 控制代码 ED 发送到终端,清除从光标到屏幕结尾的文本 |
410
|
|
|
* |
411
|
|
|
* 不会更改光标位置 |
412
|
|
|
* |
413
|
|
|
* @return void |
414
|
|
|
*/ |
415
|
|
|
public static function clearScreenAfterCursor() |
416
|
|
|
{ |
417
|
|
|
echo '\033[0J'; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* 清除行,通过向终端发送带参数 2 的 ANSI 控制代码 EL,光标当前处于打开状态 |
422
|
|
|
* |
423
|
|
|
* 不会更改光标位置 |
424
|
|
|
* |
425
|
|
|
* @return void |
426
|
|
|
*/ |
427
|
|
|
public static function clearLine() |
428
|
|
|
{ |
429
|
|
|
echo '\033[2K'; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* 通过将带参数 1 的 ANSI 控制代码 EL 发送到终端,清除从光标位置到行首的文本 |
434
|
|
|
* |
435
|
|
|
* 不会更改光标位置 |
436
|
|
|
* |
437
|
|
|
* @return void |
438
|
|
|
*/ |
439
|
|
|
public static function clearLineBeforeCursor() |
440
|
|
|
{ |
441
|
|
|
echo '\033[1K'; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* 通过将参数为 0 的 ANSI 控制代码 EL 发送到终端,清除从光标位置到行尾的文本 |
446
|
|
|
* |
447
|
|
|
* 不会更改光标位置 |
448
|
|
|
* |
449
|
|
|
* @return void |
450
|
|
|
*/ |
451
|
|
|
public static function clearLineAfterCursor() |
452
|
|
|
{ |
453
|
|
|
echo '\033[0K'; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* 从字符串中删除 ANSI 控制代码 |
458
|
|
|
* |
459
|
|
|
* @param string $string 待处理的字符串 |
460
|
|
|
* |
461
|
|
|
* @return string |
462
|
|
|
*/ |
463
|
|
|
public static function stripAnsiFormat($string) |
464
|
|
|
{ |
465
|
|
|
return preg_replace('/\033\[[\d;?]*\w/', '', $string); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* 返回不带 ANSI 颜色代码的字符串的长度 |
470
|
|
|
* |
471
|
|
|
* @param string $string 待测量的字符串 |
472
|
|
|
* |
473
|
|
|
* @return int |
474
|
|
|
*/ |
475
|
|
|
public static function ansiStrlen($string) |
476
|
|
|
{ |
477
|
|
|
return Strings::length(static::stripAnsiFormat($string)); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* CLI 下获取命令参数 |
482
|
|
|
* |
483
|
|
|
* @return array |
484
|
|
|
*/ |
485
|
|
|
public static function get() |
486
|
|
|
{ |
487
|
|
|
global $argv; |
488
|
|
|
return array_slice($argv, 1); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* 执行一个命令并输出结果 |
493
|
|
|
* |
494
|
|
|
* @param string $command |
495
|
|
|
* |
496
|
|
|
* @return string|false |
497
|
|
|
*/ |
498
|
1 |
|
public static function exec($command) |
499
|
|
|
{ |
500
|
1 |
|
$process = new Process($command); |
501
|
1 |
|
$process->run(); |
502
|
1 |
|
if (false === $process->isSuccessful()) { |
503
|
|
|
return false; |
504
|
|
|
} |
505
|
1 |
|
return $process->getOutput(); |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
|