File tree Expand file tree Collapse file tree 11 files changed +598
-312
lines changed Expand file tree Collapse file tree 11 files changed +598
-312
lines changed Original file line number Diff line number Diff line change 77use Toolkit \Cli \Download ;
88use Inhere \Console \Controller ;
99use Inhere \Console \IO \Input ;
10- use Inhere \Console \Util \Helper ;
1110use Inhere \Console \Util \Interact ;
1211use Inhere \Console \Util \ProgressBar ;
1312use Inhere \Console \Util \Show ;
@@ -231,11 +230,11 @@ public function spinnerCommand(): void
231230 */
232231 public function pendingCommand (): void
233232 {
234- $ total = 8000 ;
233+ $ total = 800 ;
235234
236235 while ($ total --) {
237236 Show::pending ();
238- usleep (200 );
237+ usleep (20000 );
239238 }
240239
241240 Show::pending ('Done ' , true );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Inhere \Console \Component \Notify ;
4+
5+ use Inhere \Console \Component \NotifyMessage ;
6+ use Inhere \Console \Console ;
7+ use Inhere \Library \Helpers \Cli ;
8+
9+ /**
10+ * Class CounterText
11+ * @package Inhere\Console\Component\Notify
12+ */
13+ class CounterText extends NotifyMessage
14+ {
15+ /**
16+ * 与文本进度条相比,没有 total
17+ *
18+ * ```php
19+ * $total = 120;
20+ * $ctt = Show::counterTxt('handling ...', 'handled.');
21+ * $this->write('Counter:');
22+ * while ($total - 1) {
23+ * $ctt->send(1);
24+ * usleep(30000);
25+ * $total--;
26+ * }
27+ * // end of the counter.
28+ * $ctt->send(-1);
29+ * ```
30+ *
31+ * @param string $msg
32+ * @param string $doneMsg
33+ * @return \Generator
34+ */
35+ public static function gen (string $ msg , $ doneMsg = '' ): \Generator
36+ {
37+ $ counter = 0 ;
38+ $ finished = false ;
39+
40+ $ tpl = (Cli::isSupportColor () ? "\x0D\x1B[2K " : "\x0D\r" ) . '%d %s ' ;
41+ $ msg = Console::style ()->render ($ msg );
42+
43+ $ doneMsg = $ doneMsg ? Console::style ()->render ($ doneMsg ) : '' ;
44+
45+ while (true ) {
46+ if ($ finished ) {
47+ break ;
48+ }
49+
50+ $ step = yield ;
51+
52+ if ((int )$ step <= 0 ) {
53+ $ counter ++;
54+ $ finished = true ;
55+ $ msg = $ doneMsg ?: $ msg ;
56+ } else {
57+ $ counter += $ step ;
58+ }
59+
60+ printf ($ tpl , $ counter , $ msg );
61+
62+ if ($ finished ) {
63+ echo "\n" ;
64+ break ;
65+ }
66+ }
67+
68+ yield false ;
69+ }
70+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Inhere \Console \Component \Notify ;
4+
5+ use Inhere \Console \Component \NotifyMessage ;
6+ use Inhere \Console \Component \Style \Style ;
7+ use Toolkit \Cli \Cli ;
8+
9+ /**
10+ * Class DynamicText
11+ * @package Inhere\Console\Component\Notify
12+ */
13+ class DynamicText extends NotifyMessage
14+ {
15+ /**
16+ * @param string $doneMsg
17+ * @param string $fixedMsg
18+ * @return \Generator
19+ */
20+ public static function gen (string $ doneMsg , string $ fixedMsg = '' ): \Generator
21+ {
22+ $ counter = 0 ;
23+ $ finished = false ;
24+ // $template = Cli::isSupportColor() ? "\x0D\x1B[2K" : "\x0D\r";
25+ $ template = Cli::isSupportColor () ? "\x0D\x1B[2K " : "\x0D" ;
26+
27+ if ($ fixedMsg ) {
28+ $ template .= Style::instance ()->render ($ fixedMsg );
29+ }
30+
31+ $ template .= '%s ' ;
32+ $ doneMsg = $ doneMsg ? Style::instance ()->render ($ doneMsg ) : '' ;
33+
34+ while (true ) {
35+ if ($ finished ) {
36+ break ;
37+ }
38+
39+ $ msg = yield ;
40+
41+ if ($ msg === false ) {
42+ $ msg = $ doneMsg ?: '' ;
43+ $ counter ++;
44+ $ finished = true ;
45+ }
46+
47+ \printf ($ template , $ msg );
48+
49+ if ($ finished ) {
50+ echo "\n" ;
51+ break ;
52+ }
53+ }
54+
55+ yield $ counter ;
56+ }
57+
58+ public function display (): void
59+ {
60+
61+ }
62+ }
Original file line number Diff line number Diff line change @@ -34,4 +34,8 @@ public function setSpeed($speed): void
3434 $ this ->speed = (int )$ speed ;
3535 }
3636
37+ public function display (): void
38+ {
39+ throw new \RuntimeException ('Please implement the method on sub-class ' );
40+ }
3741}
Original file line number Diff line number Diff line change 1212 * Class Bar
1313 * @package Inhere\Console\Component\Progress
1414 */
15- class Bar extends Text
15+ class Bar extends TextBar
1616{
1717
1818}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Inhere \Console \Component \Progress ;
4+
5+ use Inhere \Console \Component \NotifyMessage ;
6+ use Inhere \Console \Console ;
7+ use Toolkit \Cli \Cli ;
8+
9+ /**
10+ * Class SimpleBar
11+ * @package Inhere\Console\Component\Progress
12+ */
13+ class SimpleBar extends NotifyMessage
14+ {
15+ /**
16+ * Render a simple progress bar by 'yield'
17+ *
18+ * ```php
19+ * $i = 0;
20+ * $total = 120;
21+ * $bar = Show::progressBar($total, [
22+ * 'msg' => 'Msg Text',
23+ * 'doneChar' => '#'
24+ * ]);
25+ * echo "progress:\n";
26+ * while ($i <= $total) {
27+ * $bar->send(1); // 发送步进长度,通常是 1
28+ * usleep(50000);
29+ * $i++;
30+ * }
31+ * ```
32+ *
33+ * @param int $total
34+ * @param array $opts
35+ * @internal int $current
36+ * @return \Generator
37+ */
38+ public static function gen (int $ total , array $ opts = []): \Generator
39+ {
40+ $ current = 0 ;
41+ $ finished = false ;
42+ $ tplPrefix = Cli::isSupportColor () ? "\x0D\x1B[2K " : "\x0D\r" ;
43+
44+ $ opts = \array_merge ([
45+ 'doneChar ' => '= ' ,
46+ 'waitChar ' => ' ' ,
47+ 'signChar ' => '> ' ,
48+ 'msg ' => '' ,
49+ 'doneMsg ' => '' ,
50+ ], $ opts );
51+ $ msg = Console::style ()->render ($ opts ['msg ' ]);
52+
53+ $ doneMsg = Console::style ()->render ($ opts ['doneMsg ' ]);
54+ $ waitChar = $ opts ['waitChar ' ];
55+
56+ while (true ) {
57+ if ($ finished ) {
58+ break ;
59+ }
60+
61+ $ step = yield ;
62+
63+ if ((int )$ step <= 0 ) {
64+ $ step = 1 ;
65+ }
66+
67+ $ current += $ step ;
68+ $ percent = \ceil (($ current / $ total ) * 100 );
69+
70+ if ($ percent >= 100 ) {
71+ $ msg = $ doneMsg ?: $ msg ;
72+ $ percent = 100 ;
73+ $ finished = true ;
74+ }
75+
76+ /**
77+ * \r, \x0D 回车,到行首
78+ * \x1B ESC
79+ * 2K 清除本行
80+ */
81+ // printf("\r[%'--100s] %d%% %s",
82+ // printf("\x0D\x1B[2K[%'{$waitChar}-100s] %d%% %s",
83+ printf ("{$ tplPrefix }[%' {$ waitChar }-100s] %' 3d%% %s " ,
84+ str_repeat ($ opts ['doneChar ' ], $ percent ) . ($ finished ? '' : $ opts ['signChar ' ]),
85+ $ percent ,
86+ $ msg
87+ );// ♥ ■ ☺ ☻ = #
88+
89+ if ($ finished ) {
90+ echo "\n" ;
91+ break ;
92+ }
93+ }
94+
95+ yield false ;
96+ }
97+
98+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Inhere \Console \Component \Progress ;
4+
5+ use Inhere \Console \Component \NotifyMessage ;
6+ use Inhere \Console \Console ;
7+ use Toolkit \Cli \Cli ;
8+
9+ /**
10+ * Class SimpleTextBar
11+ * @package Inhere\Console\Component\Progress
12+ */
13+ class SimpleTextBar extends NotifyMessage
14+ {
15+ /**
16+ * Render a simple text progress bar by 'yield'
17+ * @param int $total
18+ * @param string $msg
19+ * @param string $doneMsg
20+ * @return \Generator
21+ */
22+ public static function gen (int $ total , string $ msg , string $ doneMsg = '' ): \Generator
23+ {
24+ $ current = 0 ;
25+ $ finished = false ;
26+ $ tpl = (Cli::isSupportColor () ? "\x0D\x1B[2K " : "\x0D\r" ) . "%' 3d%% %s " ;
27+ $ msg = Console::style ()->render ($ msg );
28+ $ doneMsg = $ doneMsg ? Console::style ()->render ($ doneMsg ) : '' ;
29+
30+ while (true ) {
31+ if ($ finished ) {
32+ break ;
33+ }
34+
35+ $ step = yield ;
36+
37+ if ((int )$ step <= 0 ) {
38+ $ step = 1 ;
39+ }
40+
41+ $ current += $ step ;
42+ $ percent = ceil (($ current / $ total ) * 100 );
43+
44+ if ($ percent >= 100 ) {
45+ $ percent = 100 ;
46+ $ finished = true ;
47+ $ msg = $ doneMsg ?: $ msg ;
48+ }
49+
50+ // printf("\r%d%% %s", $percent, $msg);
51+ // printf("\x0D\x2K %d%% %s", $percent, $msg);
52+ // printf("\x0D\r%'2d%% %s", $percent, $msg);
53+ printf ($ tpl , $ percent , $ msg );
54+
55+ if ($ finished ) {
56+ echo "\n" ;
57+ break ;
58+ }
59+ }
60+
61+ yield false ;
62+ }
63+ }
Original file line number Diff line number Diff line change 1212 * Class Text
1313 * @package Inhere\Console\Component\Progress
1414 */
15- class Text
15+ class TextBar
1616{
1717
1818}
You can’t perform that action at this time.
0 commit comments