14
14
*/
15
15
class Cli
16
16
{
17
- const NORMAL = 0 ;
18
-
19
- // Foreground color
20
- const FG_BLACK = 30 ;
21
- const FG_RED = 31 ;
22
- const FG_GREEN = 32 ;
23
- const FG_BROWN = 33 ; // like yellow
24
- const FG_BLUE = 34 ;
25
- const FG_CYAN = 36 ;
26
- const FG_WHITE = 37 ;
27
- const FG_DEFAULT = 39 ;
28
-
29
- // extra Foreground color
30
- const FG_DARK_GRAY = 90 ;
31
- const FG_LIGHT_RED = 91 ;
32
- const FG_LIGHT_GREEN = 92 ;
33
- const FG_LIGHT_YELLOW = 93 ;
34
- const FG_LIGHT_BLUE = 94 ;
35
- const FG_LIGHT_MAGENTA = 95 ;
36
- const FG_LIGHT_CYAN = 96 ;
37
- const FG_WHITE_W = 97 ;
38
-
39
- // Background color
40
- const BG_BLACK = 40 ;
41
- const BG_RED = 41 ;
42
- const BG_GREEN = 42 ;
43
- const BG_BROWN = 43 ; // like yellow
44
- const BG_BLUE = 44 ;
45
- const BG_CYAN = 46 ;
46
- const BG_WHITE = 47 ;
47
- const BG_DEFAULT = 49 ;
48
-
49
- // extra Background color
50
- const BG_DARK_GRAY = 100 ;
51
- const BG_LIGHT_RED = 101 ;
52
- const BG_LIGHT_GREEN = 102 ;
53
- const BG_LIGHT_YELLOW = 103 ;
54
- const BG_LIGHT_BLUE = 104 ;
55
- const BG_LIGHT_MAGENTA = 105 ;
56
- const BG_LIGHT_CYAN = 106 ;
57
- const BG_WHITE_W = 107 ;
58
-
59
- // color option
60
- const BOLD = 1 ; // 加粗
61
- const FUZZY = 2 ; // 模糊(不是所有的终端仿真器都支持)
62
- const ITALIC = 3 ; // 斜体(不是所有的终端仿真器都支持)
63
- const UNDERSCORE = 4 ; // 下划线
64
- const BLINK = 5 ; // 闪烁
65
- const REVERSE = 7 ; // 颠倒的 交换背景色与前景色
66
- const CONCEALED = 8 ; // 隐匿的
67
-
68
- /**
69
- * Regex to match tags
70
- * @var string
71
- */
72
- const COLOR_TAG = '/<([a-z=;]+)>(.*?)<\/ \\1>/s ' ;
73
-
74
- /**
75
- * some styles
76
- * @var array
77
- */
78
- const STYLES = [
79
- 'light_red ' => '1;31 ' ,
80
- 'light_green ' => '1;32 ' ,
81
- 'yellow ' => '1;33 ' ,
82
- 'light_blue ' => '1;34 ' ,
83
- 'magenta ' => '1;35 ' ,
84
- 'light_cyan ' => '1;36 ' ,
85
- 'white ' => '1;37 ' ,
86
- 'black ' => '0;30 ' ,
87
- 'red ' => '0;31 ' ,
88
- 'green ' => '0;32 ' ,
89
- 'brown ' => '0;33 ' ,
90
- 'blue ' => '0;34 ' ,
91
- 'cyan ' => '0;36 ' ,
92
-
93
- 'bold ' => '1 ' ,
94
- 'underscore ' => '4 ' ,
95
- 'reverse ' => '7 ' ,
96
-
97
- //
98
- 'suc ' => '1;32 ' ,// same 'green' and 'bold'
99
- 'success ' => '1;32 ' ,
100
- 'info ' => '0;32 ' ,// same 'green'
101
- 'comment ' => '0;33 ' ,// same 'brown'
102
- 'warning ' => '0;30;43 ' ,
103
- 'danger ' => '0;31 ' ,// same 'red'
104
- 'error ' => '30;41 ' ,
105
- ];
106
-
107
17
/*******************************************************************************
108
18
* color render
109
19
******************************************************************************/
@@ -113,60 +23,9 @@ class Cli
113
23
* @param string|int|array $style
114
24
* @return string
115
25
*/
116
- public static function color ($ text , $ style = null ): string
117
- {
118
- if (!$ text ) {
119
- return $ text ;
120
- }
121
-
122
- if (!self ::isSupportColor ()) {
123
- return self ::clearColor ($ text );
124
- }
125
-
126
- if (\is_string ($ style )) {
127
- $ color = self ::STYLES [$ style ] ?? '0 ' ;
128
- } elseif (\is_int ($ style )) {
129
- $ color = $ style ;
130
-
131
- // array: [self::FG_GREEN, self::BG_WHITE, self::UNDERSCORE]
132
- } elseif (\is_array ($ style )) {
133
- $ color = implode ('; ' , $ style );
134
- } elseif (strpos ($ text , '< ' ) !== false ) {
135
- return self ::renderColor ($ text );
136
- } else {
137
- return $ text ;
138
- }
139
-
140
- // $result = chr(27). "$color{$text}" . chr(27) . chr(27) . "[0m". chr(27);
141
- return "\033[ {$ color }m {$ text }\033[0m " ;
142
- }
143
-
144
- public static function renderColor ($ text )
26
+ public static function color (string $ text , $ style = null ): string
145
27
{
146
- if (!$ text || false === strpos ($ text , '< ' )) {
147
- return $ text ;
148
- }
149
-
150
- // if don't support output color text, clear color tag.
151
- if (!SysEnv::isSupportColor ()) {
152
- return static ::clearColor ($ text );
153
- }
154
-
155
- if (!preg_match_all (self ::COLOR_TAG , $ text , $ matches )) {
156
- return $ text ;
157
- }
158
-
159
- foreach ((array )$ matches [0 ] as $ i => $ m ) {
160
- if ($ style = self ::STYLES [$ matches [1 ][$ i ]] ?? null ) {
161
- $ tag = $ matches [1 ][$ i ];
162
- $ match = $ matches [2 ][$ i ];
163
-
164
- $ replace = sprintf ("\033[%sm%s \033[0m " , $ style , $ match );
165
- $ text = str_replace ("< $ tag> $ match</ $ tag> " , $ replace , $ text );
166
- }
167
- }
168
-
169
- return $ text ;
28
+ return Color::render ($ text , $ style );
170
29
}
171
30
172
31
/**
@@ -209,7 +68,7 @@ public static function write($message, $nl = true, $quit = false)
209
68
$ message = implode ($ nl ? PHP_EOL : '' , $ message );
210
69
}
211
70
212
- self ::stdout (self :: renderColor ($ message ), $ nl , $ quit );
71
+ self ::stdout (Color:: parseTag ($ message ), $ nl , $ quit );
213
72
}
214
73
215
74
/**
0 commit comments