66 * Time: 下午5:33
77 */
88
9- namespace Toolkit \Sys ;
9+ namespace Toolkit \Cli ;
1010
1111/**
1212 * Class Cli
13- * @package Toolkit\Sys
13+ * @package Toolkit\Cli
1414 */
1515class Cli
1616{
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-
4117 /*******************************************************************************
4218 * read/write message
4319 ******************************************************************************/
@@ -58,17 +34,17 @@ public static function read($message = null, $nl = false): string
5834
5935 /**
6036 * write message to console
61- * @param $message
37+ * @param $messages
6238 * @param bool $nl
6339 * @param bool $quit
6440 */
65- public static function write ($ message , $ nl = true , $ quit = false )
41+ public static function write ($ messages , $ nl = true , $ quit = false )
6642 {
67- if (\is_array ($ message )) {
68- $ message = implode ($ nl ? PHP_EOL : '' , $ message );
43+ if (\is_array ($ messages )) {
44+ $ messages = implode ($ nl ? PHP_EOL : '' , $ messages );
6945 }
7046
71- self ::stdout (Color::parseTag ($ message ), $ nl , $ quit );
47+ self ::stdout (Color::parseTag ($ messages ), $ nl , $ quit );
7248 }
7349
7450 /**
@@ -77,7 +53,7 @@ public static function write($message, $nl = true, $quit = false)
7753 * @param bool $nl
7854 * @param bool|int $quit
7955 */
80- public static function stdout ($ message , $ nl = true , $ quit = false )
56+ public static function stdout (string $ message , $ nl = true , $ quit = false )
8157 {
8258 fwrite (\STDOUT , $ message . ($ nl ? PHP_EOL : '' ));
8359 fflush (\STDOUT );
@@ -94,7 +70,7 @@ public static function stdout($message, $nl = true, $quit = false)
9470 * @param bool $nl
9571 * @param bool|int $quit
9672 */
97- public static function stderr ($ message , $ nl = true , $ quit = -200 )
73+ public static function stderr (string $ message , $ nl = true , $ quit = -1 )
9874 {
9975 fwrite (\STDERR , self ::color ('[ERROR] ' , 'red ' ) . $ message . ($ nl ? PHP_EOL : '' ));
10076 fflush (\STDOUT );
@@ -105,6 +81,24 @@ public static function stderr($message, $nl = true, $quit = -200)
10581 }
10682 }
10783
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+
108102 /**
109103 * Returns true if STDOUT supports colorization.
110104 * This code has been copied and adapted from
@@ -113,9 +107,36 @@ public static function stderr($message, $nl = true, $quit = -200)
113107 */
114108 public static function isSupportColor (): bool
115109 {
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 );
117134 }
118135
136+ /*******************************************************************************
137+ * parse $argv
138+ ******************************************************************************/
139+
119140 /**
120141 * Parses $GLOBALS['argv'] for parameters and assigns them to an array.
121142 * Supports:
0 commit comments