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 (#27)
by t
03:29
created

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

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
523
            $process->run();
524
            if (false === $process->isSuccessful()) {
525
                return false;
526
            }
527
            return $process->getOutput();
528
        }else{
529
            return exec($command);
0 ignored issues
show
Bug introduced by
It seems like $command can also be of type array; however, parameter $command of exec() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

529
            return exec(/** @scrutinizer ignore-type */ $command);
Loading history...
530
        }
531
    }
532
}
533