@@ -184,6 +184,22 @@ public static function __callStatic($method, array $args = [])
184184 * Output Format Message(section/list/helpPanel/panel/table)
185185 **************************************************************************************************/
186186
187+ /**
188+ * @param string $title
189+ * @param string $char
190+ * @param int $width
191+ */
192+ public static function splitLine (string $ title , string $ char = '- ' , int $ width = 0 )
193+ {
194+ if ($ width <= 0 ) {
195+ list ($ width ,) = CliUtil::getScreenSize ();
196+ }
197+
198+ $ length = $ width - Helper::strLen ($ title ) + 2 ;
199+
200+ self ::write (str_pad ('' . ucwords ($ title ) . ' ' , $ length , $ char , STR_PAD_BOTH ));
201+ }
202+
187203 /**
188204 * @param string $title The title text
189205 * @param array $opts
@@ -201,22 +217,22 @@ public static function title($title, array $opts = [])
201217 // list($sW, $sH) = Helper::getScreenSize();
202218 $ width = (int )$ opts ['width ' ];
203219 $ char = trim ($ opts ['char ' ]);
204- $ indent = (int )$ opts ['indent ' ] > 0 ? $ opts ['indent ' ] : 2 ;
205- $ indentStr = str_pad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
220+ $ indent = (int )$ opts ['indent ' ] >= 0 ? $ opts ['indent ' ] : 2 ;
221+ $ indentStr = Helper:: strPad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
206222
207223 $ title = ucwords (trim ($ title ));
208224 $ tLength = Helper::strLen ($ title );
209225 $ width = $ width > 10 ? $ width : 80 ;
210226
211227 // title position
212228 if ($ tLength >= $ width ) {
213- $ titleIndent = str_pad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
229+ $ titleIndent = Helper:: strPad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
214230 } elseif ($ opts ['titlePos ' ] === self ::POS_RIGHT ) {
215- $ titleIndent = str_pad (self ::CHAR_SPACE , ceil ($ width - $ tLength ) + $ indent , self ::CHAR_SPACE );
231+ $ titleIndent = Helper:: strPad (self ::CHAR_SPACE , ceil ($ width - $ tLength ) + $ indent , self ::CHAR_SPACE );
216232 } elseif ($ opts ['titlePos ' ] === self ::POS_MIDDLE ) {
217- $ titleIndent = str_pad (self ::CHAR_SPACE , ceil (($ width - $ tLength ) / 2 ) + $ indent , self ::CHAR_SPACE );
233+ $ titleIndent = Helper:: strPad (self ::CHAR_SPACE , ceil (($ width - $ tLength ) / 2 ) + $ indent , self ::CHAR_SPACE );
218234 } else {
219- $ titleIndent = str_pad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
235+ $ titleIndent = Helper:: strPad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
220236 }
221237
222238 $ titleLine = "$ titleIndent<bold> $ title</bold> \n" ;
@@ -244,22 +260,22 @@ public static function section($title, $body, array $opts = [])
244260 // list($sW, $sH) = Helper::getScreenSize();
245261 $ width = (int )$ opts ['width ' ];
246262 $ char = trim ($ opts ['char ' ]);
247- $ indent = (int )$ opts ['indent ' ] > 0 ? $ opts ['indent ' ] : 2 ;
248- $ indentStr = str_pad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
263+ $ indent = (int )$ opts ['indent ' ] >= 0 ? $ opts ['indent ' ] : 2 ;
264+ $ indentStr = Helper:: strPad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
249265
250266 $ title = ucwords (trim ($ title ));
251267 $ tLength = Helper::strLen ($ title );
252268 $ width = $ width > 10 ? $ width : 80 ;
253269
254270 // title position
255271 if ($ tLength >= $ width ) {
256- $ titleIndent = str_pad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
272+ $ titleIndent = Helper:: strPad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
257273 } elseif ($ opts ['titlePos ' ] === self ::POS_RIGHT ) {
258274 $ titleIndent = str_pad (self ::CHAR_SPACE , ceil ($ width - $ tLength ) + $ indent , self ::CHAR_SPACE );
259275 } elseif ($ opts ['titlePos ' ] === self ::POS_MIDDLE ) {
260276 $ titleIndent = str_pad (self ::CHAR_SPACE , ceil (($ width - $ tLength ) / 2 ) + $ indent , self ::CHAR_SPACE );
261277 } else {
262- $ titleIndent = str_pad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
278+ $ titleIndent = Helper:: strPad (self ::CHAR_SPACE , $ indent , self ::CHAR_SPACE );
263279 }
264280
265281 $ tpl = "%s \n%s%s \n%s " ;// title topBorder body bottomBorder
@@ -635,12 +651,54 @@ public static function panel($data, $title = 'Information Panel', array $opts =
635651
636652 /**
637653 * @todo un-completed
654+ * ├ ─ ─
655+ * └ ─
638656 * @param array $data
639657 * @param array $opts
640658 */
641659 public static function tree (array $ data , array $ opts = [])
642660 {
661+ static $ counter = 0 ;
662+ static $ started = 1 ;
643663
664+ if ($ started ) {
665+ $ started = 0 ;
666+ $ opts = array_merge ([
667+ // 'char' => Helper::supportColor() ? '─' : '-', // ——
668+ 'char ' => '- ' ,
669+ 'prefix ' => Helper::supportColor () ? '├ ' : '| ' ,
670+ 'leftPadding ' => '' ,
671+ ], $ opts );
672+
673+ $ opts ['_level ' ] = 1 ;
674+ $ opts ['_is_main ' ] = true ;
675+
676+ self ::startBuffer ();
677+ }
678+
679+ foreach ($ data as $ key => $ value ) {
680+ if (is_scalar ($ value )) {
681+ $ counter ++;
682+ $ leftString = $ opts ['leftPadding ' ] . str_pad ($ opts ['prefix ' ], $ opts ['_level ' ] + 1 , $ opts ['char ' ]);
683+
684+ self ::write ($ leftString . ' ' . FormatUtil::typeToString ($ value ));
685+ } elseif (\is_array ($ value )) {
686+ $ newOpts = $ opts ;
687+ $ newOpts ['_is_main ' ] = false ;
688+ $ newOpts ['_level ' ]++;
689+
690+ self ::tree ($ value , $ newOpts );
691+ }
692+ }
693+
694+ if ($ opts ['_is_main ' ]) {
695+ self ::write ('node count: ' . $ counter );
696+ // var_dump('f');
697+ self ::flushBuffer ();
698+
699+ // reset.
700+ $ counter = $ started = 0 ;
701+ }
644702 }
645703
646704 /**
@@ -875,12 +933,10 @@ public static function loading($msg = 'Loading ', $ended = false)
875933 * show a pending message
876934 * ```php
877935 * $total = 8000;
878- *
879936 * while ($total--) {
880937 * Show::pending();
881938 * usleep(200);
882939 * }
883- *
884940 * Show::pending('Done', true);
885941 * ```
886942 * @param string $msg
@@ -913,8 +969,53 @@ public static function pending($msg = 'Pending ', $ended = false)
913969 }
914970 }
915971
972+ /**
973+ * show a pending message
974+ * ```php
975+ * $total = 8000;
976+ * while ($total--) {
977+ * Show::pointing();
978+ * usleep(200);
979+ * }
980+ * Show::pointing('Total', true);
981+ * ```
982+ * @param string $msg
983+ * @param bool $ended
984+ * @return int
985+ */
986+ public static function pointing ($ msg = 'handling ' , $ ended = false )
987+ {
988+ static $ counter = 0 ;
989+
990+ if ($ ended ) {
991+ return printf (' %s %d ' , $ msg ?: 'Total ' , $ counter );
992+ }
993+
994+ if ($ counter === 0 && $ msg ) {
995+ echo $ msg ;
996+ }
997+
998+ $ counter ++;
999+
1000+ return print '. ' ;
1001+ }
1002+
9161003 /**
9171004 * 与文本进度条相比,没有 total
1005+ * ```php
1006+ * $total = 120;
1007+ * $ctt = Show::counterTxt('handling ...', 'handled.');
1008+ * $this->write('Counter:');
1009+ *
1010+ * while ($total - 1) {
1011+ * $ctt->send(1);
1012+ * usleep(30000);
1013+ * $total--;
1014+ * }
1015+ *
1016+ * // end of the counter.
1017+ * $ctt->send(-1);
1018+ * ```
9181019 * @param string $msg
9191020 * @param string|null $doneMsg
9201021 * @return \Generator
@@ -934,15 +1035,11 @@ public static function counterTxt($msg, $doneMsg = null)
9341035
9351036 $ step = yield ;
9361037
937- if ((int )$ step === self :: FINISHED ) {
1038+ if ((int )$ step <= 0 ) {
9381039 $ counter ++;
9391040 $ finished = true ;
9401041 $ msg = $ doneMsg ?: $ msg ;
9411042 } else {
942- if ((int )$ step <= 0 ) {
943- $ step = 1 ;
944- }
945-
9461043 $ counter += $ step ;
9471044 }
9481045
0 commit comments