3333 */
3434class Show
3535{
36+ const FINISHED = -1 ;
37+
3638 const CHAR_SPACE = ' ' ;
3739 const CHAR_HYPHEN = '- ' ;
3840 const CHAR_UNDERLINE = '_ ' ;
@@ -773,23 +775,76 @@ public static function table(array $data, $title = 'Data Table', array $opts = [
773775 }
774776
775777 /**
776- * @param int $ total
778+ * 与文本进度条相比,没有 total
777779 * @param string $msg
778- * @param string $doneMsg
780+ * @param string|null $doneMsg
779781 * @return \Generator
780782 */
781- public static function progressTxt ( $ total , $ msg , $ doneMsg = '' )
783+ public static function counterTxt ( $ msg , $ doneMsg = null )
782784 {
785+ $ counter = 0 ;
783786 $ finished = false ;
787+ $ tpl = (Helper::isSupportColor () ? "\x0D\x1B[2K " : "\x0D\r" ) . "%d %s " ;
784788 $ msg = self ::getStyle ()->render ($ msg );
789+ $ doneMsg = $ doneMsg ? self ::getStyle ()->render ($ doneMsg ) : null ;
785790
786791 while (true ) {
787- $ current = yield ;
792+ if ($ finished ) {
793+ return ;
794+ }
795+
796+ $ step = yield ;
797+
798+ if ((int )$ step === self ::FINISHED ) {
799+ $ counter ++;
800+ $ finished = true ;
801+ $ msg = $ doneMsg ?: $ msg ;
802+ } else {
803+ if ((int )$ step <= 0 ) {
804+ $ step = 1 ;
805+ }
806+
807+ $ counter += $ step ;
808+ }
809+
810+ // printf("\r%d%% %s", $percent, $msg);
811+ // printf("\x0D\x2K %d%% %s", $percent, $msg);
812+ // printf("\x0D\r%'2d%% %s", $percent, $msg);
813+ printf ($ tpl , $ counter , $ msg );
814+
815+ if ($ finished ) {
816+ echo "\n" ;
817+ break ;
818+ }
819+ }
820+ }
821+
822+ /**
823+ * @param int $total
824+ * @param string $msg
825+ * @param string|null $doneMsg
826+ * @return \Generator
827+ */
828+ public static function progressTxt ($ total , $ msg , $ doneMsg = null )
829+ {
830+ $ current = 0 ;
831+ $ finished = false ;
832+ $ tpl = (Helper::isSupportColor () ? "\x0D\x1B[2K " : "\x0D\r" ) . "%' 3d%% %s " ;
833+ $ msg = self ::getStyle ()->render ($ msg );
834+ $ doneMsg = $ doneMsg ? self ::getStyle ()->render ($ doneMsg ) : null ;
788835
836+ while (true ) {
789837 if ($ finished ) {
790838 return ;
791839 }
792840
841+ $ step = yield ;
842+
843+ if ((int )$ step <= 0 ) {
844+ $ step = 1 ;
845+ }
846+
847+ $ current += $ step ;
793848 $ percent = ceil (($ current / $ total ) * 100 );
794849
795850 if ($ percent >= 100 ) {
@@ -799,8 +854,9 @@ public static function progressTxt($total, $msg, $doneMsg = '')
799854 }
800855
801856 // printf("\r%d%% %s", $percent, $msg);
802- // printf("\x0D\x1B[2K%d%% %s", $percent, $msg);
803- printf ("\x0D\r%d%% %s " , $ percent , $ msg );
857+ // printf("\x0D\x2K %d%% %s", $percent, $msg);
858+ // printf("\x0D\r%'2d%% %s", $percent, $msg);
859+ printf ($ tpl , $ percent , $ msg );
804860
805861 if ($ finished ) {
806862 echo "\n" ;
@@ -822,7 +878,7 @@ public static function progressTxt($total, $msg, $doneMsg = '')
822878 * ]);
823879 * echo "progress:\n";
824880 * while ($i <= $total) {
825- * $bar->send($i);
881+ * $bar->send(1); // 发送步进长度,通常是 1
826882 * usleep(50000);
827883 * $i++;
828884 * }
@@ -834,36 +890,49 @@ public static function progressTxt($total, $msg, $doneMsg = '')
834890 */
835891 public static function progressBar ($ total , array $ opts = [])
836892 {
893+ $ current = 0 ;
837894 $ finished = false ;
895+ $ tplPrefix = Helper::isSupportColor () ? "\x0D\x1B[2K " : "\x0D\r" ;
838896 $ opts = array_merge ([
839897 'doneChar ' => '= ' ,
840898 'waitChar ' => ' ' ,
841899 'signChar ' => '> ' ,
842900 'msg ' => '' ,
901+ 'doneMsg ' => '' ,
843902 ], $ opts );
903+
844904 $ msg = self ::getStyle ()->render ($ opts ['msg ' ]);
905+ $ doneMsg = self ::getStyle ()->render ($ opts ['doneMsg ' ]);
845906 $ waitChar = $ opts ['waitChar ' ];
846907
847908 while (true ) {
848909 if ($ finished ) {
849910 return ;
850911 }
851912
852- $ current = yield ;
913+ $ step = yield ;
914+
915+ if ((int )$ step <= 0 ) {
916+ $ step = 1 ;
917+ }
918+
919+ $ current += $ step ;
853920 $ percent = ceil (($ current / $ total ) * 100 );
854921
855922 if ($ percent >= 100 ) {
923+ $ msg = $ doneMsg ?: $ msg ;
856924 $ percent = 100 ;
857925 $ finished = true ;
858926 }
859927
860928 /**
861- * \x0D 调到行首
862- * \r, \x1B[2K 都是清除本行
929+ * \r, \x0D 回车,到行首
930+ * \x1B ESC
931+ * 2K 清除本行
863932 */
864933 // printf("\r[%'--100s] %d%% %s",
865934 // printf("\x0D\x1B[2K[%'{$waitChar}-100s] %d%% %s",
866- printf ("\x0D\r [%' {$ waitChar }-100s] %d %% %s " ,
935+ printf ("{ $ tplPrefix } [%' {$ waitChar }-100s] %' 3d %% %s " ,
867936 str_repeat ($ opts ['doneChar ' ], $ percent ) . ($ finished ? '' : $ opts ['signChar ' ]),
868937 $ percent ,
869938 $ msg
0 commit comments