GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#14)
by t
02:14
created

Console::restoreCursorPosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
150
     */
151
    public static function stdin()
152
    {
153
        return rtrim((string) fgets(\STDIN), PHP_EOL);
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
     * @param mixed $defaultValue 默认值
173
     *
174
     * @return string
175
     */
176
    public static function input($prompt = null, $defaultValue = '')
177
    {
178
        if (isset($prompt)) {
179
            self::output($prompt);
180
        }
181
182
        $input = self::stdin();
183
        if ('' === $input) {
184
            return (string) $defaultValue;
185
        }
186
        return $input;
187
    }
188
189
    /**
190
     * 输出提示
191
     *
192
     * @param string $string 提示文字
193
     *
194
     * @return integer|false
195
     */
196
    public static function output($string = null, $format = [])
197
    {
198
        if (!empty($format)) {
199
            $string = self::ansiFormat($string, $format);
200
        }
201
        return self::stdout($string . PHP_EOL);
202
    }
203
204
    /**
205
     * 输出列表
206
     *
207
     * @param array $array
208
     *
209
     * @return void
210
     */
211
    public static function outputList($array)
212
    {
213
        foreach($array as $string){
214
            self::output($string);
215
        }
216
    }
217
218
    /**
219
     * ANSI 格式化
220
     *
221
     * @param string $string ANSI 格式文本
222
     * @param array $format 格式代码数组
223
     *
224
     * @return string
225
     */
226
    public static function ansiFormat($string, $format = [])
227
    {
228
        $code = implode(';', $format);
229
230
        return "\033[0m" . ($code !== '' ? "\033[" . $code . 'm' : '') . $string . "\033[0m";
231
    }
232
233
    /**
234
     * 向终端发送 ANSI 控制代码 CUU,上移光标 n 行
235
     *
236
     * 如果光标已经在屏幕边缘,则此操作无效
237
     *
238
     * @param integer $rows 光标应向上移动的行数
239
     *
240
     * @return void
241
     */
242
    public static function moveCursorUp($rows = 1)
243
    {
244
        echo '\033[' . (int) $rows . 'A';
245
    }
246
247
    /**
248
     * 通过向终端发送 ANSI 控制代码 CUD,向下移动终端光标
249
     *
250
     * 如果光标已经在屏幕边缘,则此操作无效
251
     *
252
     * @param integer $rows 光标应向下移动的行数
253
     *
254
     * @return void
255
     */
256
    public static function moveCursorDown($rows = 1)
257
    {
258
        echo '\033[' . (int) $rows . 'B';
259
    }
260
261
    /**
262
     * 通过向终端发送 ANSI 控制代码 CUF,向前移动终端光标
263
     *
264
     * 如果光标已经在屏幕边缘,则此操作无效
265
     *
266
     * @param integer $steps 光标应向前移动的步数
267
     *
268
     * @return void
269
     */
270
    public static function moveCursorForward($steps = 1)
271
    {
272
        echo '\033[' . (int) $steps . 'C';
273
    }
274
275
    /**
276
     * 通过向终端发送 ANSI 控制代码 CUB,向后移动终端光标
277
     *
278
     * 如果光标已经在屏幕边缘,则此操作无效
279
     *
280
     * @param integer $steps 光标应向后移动的步数
281
     *
282
     * @return void
283
     */
284
    public static function moveCursorBackward($steps = 1)
285
    {
286
        echo '\033[' . (int) $steps . 'D';
287
    }
288
289
    /**
290
     * 向终端发送 ANSI 控制代码 CNL,让光标移到下 n 行的开头
291
     *
292
     * @param integer $lines 光标应向下移动的行数
293
     *
294
     * @return void
295
     */
296
    public static function moveCursorNextLine($lines = 1)
297
    {
298
        echo '\033[' . (int) $lines . 'E';
299
    }
300
301
    /**
302
     * 向终端发送 ANSI 控制代码 CPL,让光标移到上 n 行的开头
303
     *
304
     * @param integer $lines 光标应向上移动的行数
305
     *
306
     * @return void
307
     */
308
    public static function moveCursorPrevLine($lines = 1)
309
    {
310
        echo '\033[' . (int) $lines . 'F';
311
    }
312
313
    /**
314
     * 通过向终端发送 ANSI 控制代码 CUP 或 CPA,将光标移动到给定行和列的绝对位置上
315
     *
316
     * @param int $column 基于 1 的列号,1 是屏幕的左边缘
317
     * @param int $row 行 基于 1 的行数,1 是屏幕的上边缘。如果未设置,将只在当前行中移动光标
318
     *
319
     * @return void
320
     */
321
    public static function moveCursorTo($column, $row = null)
322
    {
323
        if ($row === null) {
324
            echo '\033[' . (int) $column . 'G';
325
        } else {
326
            echo '\033[' . (int) $row . ';' . (int) $column . 'H';
327
        }
328
    }
329
330
    /**
331
     * 通过向终端发送 ANSI 控制代码 SU 来向上滚动整页
332
     *
333
     * 在底部添加新行。在 Windows 中使用的 ANSI.SYS 不支持此操作
334
     *
335
     * @param integer $lines 要向上滚动的行数
336
     *
337
     * @return void
338
     */
339
    public static function scrollUp($lines = 1)
340
    {
341
        echo '\033[' . (int) $lines . 'S';
342
    }
343
344
    /**
345
     * 通过向终端发送 ANSI 控制代码 SD,向下滚动整页
346
     *
347
     * 在顶部添加新行。在 Windows 中使用的 ANSI.SYS 不支持此操作
348
     *
349
     * @param integer $lines 要向下滚动的行数
350
     *
351
     * @return void
352
     */
353
    public static function scrollDown($lines = 1)
354
    {
355
        echo '\033[' . (int) $lines . 'T';
356
    }
357
358
    /**
359
     * 通过向终端发送 ANSI 控制代码 SCP 来保存当前光标位置
360
     *
361
     * 然后可以使用 RestoreCursorPosition 恢复位置
362
     *
363
     * @return void
364
     */
365
    public static function saveCursorPosition()
366
    {
367
        echo '\033[s';
368
    }
369
370
    /**
371
     * 通过向终端发送 ANSI 控制代码 RCP,恢复用 SaveCursorPosition 保存的光标位置
372
     *
373
     * @return void
374
     */
375
    public static function restoreCursorPosition()
376
    {
377
        echo '\033[u';
378
    }
379
380
    /**
381
     * 通过发送 ANSI DECTCEM 代码隐藏光标到终端
382
     *
383
     * 使用 ShowCursor 将其带回
384
     *
385
     * 当应用程序退出时,不要忘记显示光标。退出后光标可能还隐藏在终端中
386
     *
387
     * @return void
388
     */
389
    public static function hideCursor()
390
    {
391
        echo '\033[?25l';
392
    }
393
394
    /**
395
     * 当被光标被 hideCursor 隐藏时,通过发送 ANSI DECTCEM 代码将光标显示到终端
396
     *
397
     * @return void
398
     */
399
    public static function showCursor()
400
    {
401
        echo '\033[?25h';
402
    }
403
404
    /**
405
     * 通过向终端发送带参数 2 的 ANSI 控制代码 ED,清除整个屏幕内容
406
     *
407
     * 不会更改光标位置
408
     *
409
     * 注意:在 Windows 中使用的 ANSI.SYS 实现将光标位置重置为屏幕的左上角
410
     *
411
     * @return void
412
     */
413
    public static function clearScreen()
414
    {
415
        echo '\033[2J';
416
    }
417
418
    /**
419
     * 通过将带参数 1 的ANSI控制代码 ED 发送到终端,清除从光标到屏幕开头的文本
420
     *
421
     * 不会更改光标位置
422
     *
423
     * @return void
424
     */
425
    public static function clearScreenBeforeCursor()
426
    {
427
        echo '\033[1J';
428
    }
429
430
    /**
431
     * 通过将带参数 0 的 ANSI 控制代码 ED 发送到终端,清除从光标到屏幕结尾的文本
432
     *
433
     * 不会更改光标位置
434
     *
435
     * @return void
436
     */
437
    public static function clearScreenAfterCursor()
438
    {
439
        echo '\033[0J';
440
    }
441
442
    /**
443
     * 清除行,通过向终端发送带参数 2 的 ANSI 控制代码 EL,光标当前处于打开状态
444
     *
445
     * 不会更改光标位置
446
     *
447
     * @return void
448
     */
449
    public static function clearLine()
450
    {
451
        echo '\033[2K';
452
    }
453
454
    /**
455
     * 通过将带参数 1 的 ANSI 控制代码 EL 发送到终端,清除从光标位置到行首的文本
456
     *
457
     * 不会更改光标位置
458
     *
459
     * @return void
460
     */
461
    public static function clearLineBeforeCursor()
462
    {
463
        echo '\033[1K';
464
    }
465
466
    /**
467
     * 通过将参数为 0 的 ANSI 控制代码 EL 发送到终端,清除从光标位置到行尾的文本
468
     *
469
     * 不会更改光标位置
470
     *
471
     * @return void
472
     */
473
    public static function clearLineAfterCursor()
474
    {
475
        echo '\033[0K';
476
    }
477
478
    /**
479
     * 从字符串中删除 ANSI 控制代码
480
     *
481
     * @param string $string 待处理的字符串
482
     *
483
     * @return string
484
     */
485
    public static function stripAnsiFormat($string)
486
    {
487
        return preg_replace('/\033\[[\d;?]*\w/', '', $string);
488
    }
489
490
    /**
491
     * 返回不带 ANSI 颜色代码的字符串的长度
492
     *
493
     * @param string $string 待测量的字符串
494
     *
495
     * @return int
496
     */
497
    public static function ansiStrlen($string)
498
    {
499
        return Strings::length(static::stripAnsiFormat($string));
500
    }
501
502
    /**
503
     * CLI 下获取命令参数
504
     *
505
     * @return array
506
     */
507
    public static function get()
508
    {
509
        global $argv;
510
        return array_slice($argv, 1);
511
    }
512
513
    /**
514
     * 执行一个命令并输出结果
515
     *
516
     * @param string $command
517
     *
518
     * @return string|false
519
     */
520 1
    public static function exec($command)
521
    {
522 1
        $process = new Process($command);
523 1
        $process->run();
524 1
        if (false === $process->isSuccessful()) {
525
            return false;
526
        }
527 1
        return $process->getOutput();
528
    }
529
}
530