Skip to content

Commit 4fd544b

Browse files
committed
fix error for color render
1 parent 72820bc commit 4fd544b

File tree

14 files changed

+229
-99
lines changed

14 files changed

+229
-99
lines changed

.php_cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
$header = <<<'EOF'
4+
This file is part of toolkit/cli-utils.
5+
6+
@link https://github.com/inhere
7+
@author https://github.com/inhere
8+
@license MIT
9+
EOF;
10+
11+
return PhpCsFixer\Config::create()
12+
->setRiskyAllowed(true)
13+
->setRules([
14+
'@PSR2' => true,
15+
'array_syntax' => [
16+
'syntax' => 'short'
17+
],
18+
'class_attributes_separation' => true,
19+
'declare_strict_types' => true,
20+
'global_namespace_import' => true,
21+
'header_comment' => [
22+
'comment_type' => 'PHPDoc',
23+
'header' => $header,
24+
'separate' => 'bottom'
25+
],
26+
'no_unused_imports' => true,
27+
'single_quote' => true,
28+
'standardize_not_equals' => true,
29+
])
30+
->setFinder(
31+
PhpCsFixer\Finder::create()
32+
// ->exclude('test')
33+
->exclude('runtime')
34+
->exclude('vendor')
35+
->in(__DIR__)
36+
)
37+
->setUsingCache(false);

example/color.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2018/5/4
6-
* Time: 下午3:03
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
78
*/
89

910
use Toolkit\Cli\Color;

example/down.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
3-
* demo for cli download file.
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
48
*/
59

610
use Toolkit\Cli\App;

example/high.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2018/5/4
6-
* Time: 下午3:13
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
78
*/
89

910
use Toolkit\Cli\Cli;

src/App.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2017-08-15
6-
* Time: 10:51
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
78
*/
89

910
namespace Toolkit\Cli;

src/Cli.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2017/5/1
6-
* Time: 下午5:33
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
78
*/
89

910
namespace Toolkit\Cli;

src/Color.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2018/5/4
6-
* Time: 上午9:12
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
78
*/
89

910
namespace Toolkit\Cli;
@@ -46,6 +47,7 @@
4647
class Color
4748
{
4849
public const RESET = 0;
50+
4951
public const NORMAL = 0;
5052

5153
/** Foreground base value */
@@ -62,51 +64,85 @@ class Color
6264

6365
// Foreground color
6466
public const FG_BLACK = 30;
67+
6568
public const FG_RED = 31;
69+
6670
public const FG_GREEN = 32;
71+
6772
public const FG_BROWN = 33; // like yellow
73+
6874
public const FG_BLUE = 34;
75+
6976
public const FG_CYAN = 36;
77+
7078
public const FG_WHITE = 37;
79+
7180
public const FG_DEFAULT = 39;
7281

7382
// extra Foreground color
7483
public const FG_DARK_GRAY = 90;
84+
7585
public const FG_LIGHT_RED = 91;
86+
7687
public const FG_LIGHT_GREEN = 92;
88+
7789
public const FG_LIGHT_YELLOW = 93;
90+
7891
public const FG_LIGHT_BLUE = 94;
92+
7993
public const FG_LIGHT_MAGENTA = 95;
94+
8095
public const FG_LIGHT_CYAN = 96;
96+
8197
public const FG_LIGHT_WHITE = 97;
8298

8399
// Background color
84100
public const BG_BLACK = 40;
101+
85102
public const BG_RED = 41;
103+
86104
public const BG_GREEN = 42;
105+
87106
public const BG_BROWN = 43; // like yellow
107+
88108
public const BG_BLUE = 44;
109+
89110
public const BG_CYAN = 46;
111+
90112
public const BG_WHITE = 47;
113+
91114
public const BG_DEFAULT = 49;
92115

93116
// extra Background color
94117
public const BG_DARK_GRAY = 100;
118+
95119
public const BG_LIGHT_RED = 101;
120+
96121
public const BG_LIGHT_GREEN = 102;
122+
97123
public const BG_LIGHT_YELLOW = 103;
124+
98125
public const BG_LIGHT_BLUE = 104;
126+
99127
public const BG_LIGHT_MAGENTA = 105;
128+
100129
public const BG_LIGHT_CYAN = 106;
130+
101131
public const BG_WHITE_W = 107;
102132

103133
// color option
104134
public const BOLD = 1; // 加粗
135+
105136
public const FUZZY = 2; // 模糊(不是所有的终端仿真器都支持)
137+
106138
public const ITALIC = 3; // 斜体(不是所有的终端仿真器都支持)
139+
107140
public const UNDERSCORE = 4; // 下划线
141+
108142
public const BLINK = 5; // 闪烁
143+
109144
public const REVERSE = 7; // 颠倒的 交换背景色与前景色
145+
110146
public const CONCEALED = 8; // 隐匿的
111147

112148
/**
@@ -304,11 +340,11 @@ public static function render(string $text, $style = null): string
304340
if (is_string($style)) {
305341
$color = self::STYLES[$style] ?? '';
306342

307-
// custom style: [self::FG_GREEN, self::BG_WHITE, self::UNDERSCORE]
343+
// custom style: [self::FG_GREEN, self::BG_WHITE, self::UNDERSCORE]
308344
} elseif (is_array($style)) {
309345
$color = implode(';', $style);
310346

311-
// user color tag: <info>message</info>
347+
// user color tag: <info>message</info>
312348
} elseif (strpos($text, '</') > 0) {
313349
return self::parseTag($text);
314350
}

src/ColorCode.php

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php declare(strict_types=1);
22
/**
3-
* Created by PhpStorm.
3+
* This file is part of toolkit/cli-utils.
4+
*
5+
* @link https://github.com/inhere
6+
* @author https://github.com/inhere
7+
* @license MIT
48
*/
59

610
namespace Toolkit\Cli;
@@ -35,38 +39,38 @@ class ColorCode
3539
public const BG_EXTRA = 100;
3640

3741
// color
38-
public const BLACK = 'black';
42+
public const BLACK = 'black';
3943

40-
public const RED = 'red';
44+
public const RED = 'red';
4145

42-
public const GREEN = 'green';
46+
public const GREEN = 'green';
4347

44-
public const YELLOW = 'yellow'; // BROWN
48+
public const YELLOW = 'yellow'; // BROWN
4549

46-
public const BLUE = 'blue';
50+
public const BLUE = 'blue';
4751

4852
public const MAGENTA = 'magenta';
4953

50-
public const CYAN = 'cyan';
54+
public const CYAN = 'cyan';
5155

52-
public const WHITE = 'white';
56+
public const WHITE = 'white';
5357

54-
public const NORMAL = 'normal';
58+
public const NORMAL = 'normal';
5559

5660
// color option
57-
public const BOLD = 'bold'; // 加粗
61+
public const BOLD = 'bold'; // 加粗
5862

59-
public const FUZZY = 'fuzzy'; // 模糊(不是所有的终端仿真器都支持)
63+
public const FUZZY = 'fuzzy'; // 模糊(不是所有的终端仿真器都支持)
6064

61-
public const ITALIC = 'italic'; // 斜体(不是所有的终端仿真器都支持)
65+
public const ITALIC = 'italic'; // 斜体(不是所有的终端仿真器都支持)
6266

6367
public const UNDERSCORE = 'underscore'; // 下划线
6468

65-
public const BLINK = 'blink'; // 闪烁
69+
public const BLINK = 'blink'; // 闪烁
6670

67-
public const REVERSE = 'reverse'; // 颠倒的 交换背景色与前景色
71+
public const REVERSE = 'reverse'; // 颠倒的 交换背景色与前景色
6872

69-
public const CONCEALED = 'concealed'; // 隐匿的
73+
public const CONCEALED = 'concealed'; // 隐匿的
7074

7175
/**
7276
* @var array Known color list
@@ -87,13 +91,13 @@ class ColorCode
8791
* @var array Known option code
8892
*/
8993
public const KNOWN_OPTIONS = [
90-
'bold' => self::BOLD, // 加粗
91-
'fuzzy' => self::FUZZY, // 模糊(不是所有的终端仿真器都支持)
92-
'italic' => self::ITALIC, // 斜体(不是所有的终端仿真器都支持)
93-
'underscore' => self::UNDERSCORE, // 下划线
94-
'blink' => self::BLINK, // 闪烁
95-
'reverse' => self::REVERSE, // 颠倒的 交换背景色与前景色
96-
'concealed' => self::CONCEALED, // 隐匿的
94+
'bold' => Color::BOLD, // 加粗
95+
'fuzzy' => Color::FUZZY, // 模糊(不是所有的终端仿真器都支持)
96+
'italic' => Color::ITALIC, // 斜体(不是所有的终端仿真器都支持)
97+
'underscore' => Color::UNDERSCORE, // 下划线
98+
'blink' => Color::BLINK, // 闪烁
99+
'reverse' => Color::REVERSE, // 颠倒的 交换背景色与前景色
100+
'concealed' => Color::CONCEALED, // 隐匿的
97101
];
98102

99103
/**
@@ -140,7 +144,7 @@ public static function make($fg = '', $bg = '', array $options = [], bool $extra
140144
* @throws InvalidArgumentException
141145
* @throws RuntimeException
142146
*/
143-
public static function fromString($string)
147+
public static function fromString(string $string)
144148
{
145149
$extra = false;
146150
$options = [];
@@ -189,35 +193,26 @@ public function __construct($fg = '', $bg = '', array $options = [], bool $extra
189193
{
190194
if ($fg) {
191195
if (!isset(self::KNOWN_COLORS[$fg])) {
192-
throw new InvalidArgumentException(sprintf(
193-
'Invalid foreground color "%1$s" [%2$s]',
194-
$fg,
195-
implode(', ', self::getKnownColors())
196-
));
196+
throw new InvalidArgumentException(sprintf('Invalid foreground color "%1$s" [%2$s]', $fg,
197+
implode(', ', self::getKnownColors())));
197198
}
198199

199200
$this->fgColor = ($extra ? self::FG_EXTRA : self::FG_BASE) + self::KNOWN_COLORS[$fg];
200201
}
201202

202203
if ($bg) {
203204
if (!isset(self::KNOWN_COLORS[$bg])) {
204-
throw new InvalidArgumentException(sprintf(
205-
'Invalid background color "%1$s" [%2$s]',
206-
$bg,
207-
implode(', ', self::getKnownColors())
208-
));
205+
throw new InvalidArgumentException(sprintf('Invalid background color "%1$s" [%2$s]', $bg,
206+
implode(', ', self::getKnownColors())));
209207
}
210208

211209
$this->bgColor = ($extra ? self::BG_EXTRA : self::BG_BASE) + self::KNOWN_COLORS[$bg];
212210
}
213211

214212
foreach ($options as $option) {
215213
if (!isset(self::KNOWN_OPTIONS[$option])) {
216-
throw new InvalidArgumentException(sprintf(
217-
'Invalid option "%1$s" [%2$s]',
218-
$option,
219-
implode(', ', self::getKnownOptions())
220-
));
214+
throw new InvalidArgumentException(sprintf('Invalid option "%1$s" [%2$s]', $option,
215+
implode(', ', self::getKnownOptions())));
221216
}
222217

223218
$this->options[] = $option;
@@ -242,6 +237,8 @@ public function toString(): string
242237

243238
/**
244239
* Get the translated color code.
240+
*
241+
* @return string
245242
*/
246243
public function toStyle(): string
247244
{

0 commit comments

Comments
 (0)