6
6
* Time: 下午5:33
7
7
*/
8
8
9
- namespace Toolkit \Sys ;
9
+ namespace Toolkit \Cli ;
10
10
11
11
/**
12
12
* Class Cli
13
- * @package Toolkit\Sys
13
+ * @package Toolkit\Cli
14
14
*/
15
15
class Cli
16
16
{
17
- /*******************************************************************************
18
- * color render
19
- ******************************************************************************/
20
-
21
- /**
22
- * @param $text
23
- * @param string|int|array $style
24
- * @return string
25
- */
26
- public static function color (string $ text , $ style = null ): string
27
- {
28
- return Color::render ($ text , $ style );
29
- }
30
-
31
- /**
32
- * @param string $text
33
- * @return string
34
- */
35
- public static function clearColor ($ text ): string
36
- {
37
- // return preg_replace('/\033\[(?:\d;?)+m/', '' , "\033[0;36mtext\033[0m");
38
- return preg_replace ('/\033\[(?:\d;?)+m/ ' , '' , strip_tags ($ text ));
39
- }
40
-
41
17
/*******************************************************************************
42
18
* read/write message
43
19
******************************************************************************/
@@ -58,17 +34,17 @@ public static function read($message = null, $nl = false): string
58
34
59
35
/**
60
36
* write message to console
61
- * @param $message
37
+ * @param $messages
62
38
* @param bool $nl
63
39
* @param bool $quit
64
40
*/
65
- public static function write ($ message , $ nl = true , $ quit = false )
41
+ public static function write ($ messages , $ nl = true , $ quit = false )
66
42
{
67
- if (\is_array ($ message )) {
68
- $ message = implode ($ nl ? PHP_EOL : '' , $ message );
43
+ if (\is_array ($ messages )) {
44
+ $ messages = implode ($ nl ? PHP_EOL : '' , $ messages );
69
45
}
70
46
71
- self ::stdout (Color::parseTag ($ message ), $ nl , $ quit );
47
+ self ::stdout (Color::parseTag ($ messages ), $ nl , $ quit );
72
48
}
73
49
74
50
/**
@@ -77,7 +53,7 @@ public static function write($message, $nl = true, $quit = false)
77
53
* @param bool $nl
78
54
* @param bool|int $quit
79
55
*/
80
- public static function stdout ($ message , $ nl = true , $ quit = false )
56
+ public static function stdout (string $ message , $ nl = true , $ quit = false )
81
57
{
82
58
fwrite (\STDOUT , $ message . ($ nl ? PHP_EOL : '' ));
83
59
fflush (\STDOUT );
@@ -94,7 +70,7 @@ public static function stdout($message, $nl = true, $quit = false)
94
70
* @param bool $nl
95
71
* @param bool|int $quit
96
72
*/
97
- public static function stderr ($ message , $ nl = true , $ quit = -200 )
73
+ public static function stderr (string $ message , $ nl = true , $ quit = -1 )
98
74
{
99
75
fwrite (\STDERR , self ::color ('[ERROR] ' , 'red ' ) . $ message . ($ nl ? PHP_EOL : '' ));
100
76
fflush (\STDOUT );
@@ -105,6 +81,24 @@ public static function stderr($message, $nl = true, $quit = -200)
105
81
}
106
82
}
107
83
84
+ /*******************************************************************************
85
+ * color render
86
+ ******************************************************************************/
87
+
88
+ /**
89
+ * @param $text
90
+ * @param string|int|array $style
91
+ * @return string
92
+ */
93
+ public static function color (string $ text , $ style = null ): string
94
+ {
95
+ return Color::render ($ text , $ style );
96
+ }
97
+
98
+ /*******************************************************************************
99
+ * some helpers
100
+ ******************************************************************************/
101
+
108
102
/**
109
103
* Returns true if STDOUT supports colorization.
110
104
* This code has been copied and adapted from
@@ -113,9 +107,36 @@ public static function stderr($message, $nl = true, $quit = -200)
113
107
*/
114
108
public static function isSupportColor (): bool
115
109
{
116
- return SysEnv::isSupportColor ();
110
+ if (DIRECTORY_SEPARATOR === '\\' ) {
111
+ return
112
+ '10.0.10586 ' === PHP_WINDOWS_VERSION_MAJOR . '. ' . PHP_WINDOWS_VERSION_MINOR . '. ' . PHP_WINDOWS_VERSION_BUILD
113
+ || false !== getenv ('ANSICON ' )
114
+ || 'ON ' === getenv ('ConEmuANSI ' )
115
+ || 'xterm ' === getenv ('TERM ' )// || 'cygwin' === getenv('TERM')
116
+ ;
117
+ }
118
+
119
+ if (!\defined ('STDOUT ' )) {
120
+ return false ;
121
+ }
122
+
123
+ return self ::isInteractive (STDOUT );
124
+ }
125
+
126
+ /**
127
+ * Returns if the file descriptor is an interactive terminal or not.
128
+ * @param int|resource $fileDescriptor
129
+ * @return boolean
130
+ */
131
+ public static function isInteractive ($ fileDescriptor ): bool
132
+ {
133
+ return \function_exists ('posix_isatty ' ) && @posix_isatty ($ fileDescriptor );
117
134
}
118
135
136
+ /*******************************************************************************
137
+ * parse $argv
138
+ ******************************************************************************/
139
+
119
140
/**
120
141
* Parses $GLOBALS['argv'] for parameters and assigns them to an array.
121
142
* Supports:
0 commit comments