Skip to content

Commit 248f40f

Browse files
committed
update, some bug fixed for progress. add new method counterTxt()
1 parent fd12e8e commit 248f40f

File tree

5 files changed

+125
-44
lines changed

5 files changed

+125
-44
lines changed

examples/HomeController.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ public function blockMsgCommand()
7474
return 0;
7575
}
7676

77+
/**
78+
* a counter example show. It is like progress txt, but no max value.
79+
* @example
80+
* {script} {command}
81+
* @return int
82+
*/
83+
public function counterCommand()
84+
{
85+
$total = 120;
86+
$ctr = Show::counterTxt('handling ...', 'handled.');
87+
$this->write('Counter:');
88+
89+
while ($total - 1) {
90+
$ctr->send(1);
91+
usleep(30000);
92+
$total--;
93+
}
94+
95+
// end of the counter.
96+
$ctr->send(-1);
97+
98+
return 0;
99+
}
100+
77101
/**
78102
* a progress bar example show
79103
* @options
@@ -82,8 +106,8 @@ public function blockMsgCommand()
82106
* --wait-char the waiting show char. <info>-</info>
83107
* --sign-char the sign char show. <info>></info>
84108
* @example
85-
* {script} home/progress
86-
* {script} home/progress --done-char '#' --wait-char ' '
109+
* {script} {command}
110+
* {script} {command} --done-char '#' --wait-char ' '
87111
* @param Input $input
88112
* @return int
89113
*/
@@ -94,6 +118,7 @@ public function progressCommand($input)
94118
if ($input->getOpt('type') === 'bar') {
95119
$bar = $this->output->progressBar($total, [
96120
'msg' => 'Msg Text',
121+
'doneMsg' => 'Done Msg Text',
97122
'doneChar' => $input->getOpt('done-char', '='), // ▓
98123
'waitChar' => $input->getOpt('wait-char', '-'), // ░
99124
'signChar' => $input->getOpt('sign-char', '>'),
@@ -105,7 +130,7 @@ public function progressCommand($input)
105130
$this->write('Progress:');
106131

107132
while ($i <= $total) {
108-
$bar->send($i);
133+
$bar->send(1);
109134
usleep(50000);
110135
$i++;
111136
}

src/Base/AbstractApplication.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Inhere\Console\IO\Output;
1313
use Inhere\Console\Traits\InputOutputTrait;
1414
use Inhere\Console\Traits\SimpleEventTrait;
15+
use Inhere\Console\Style\Style;
1516
use Inhere\Console\Utils\Helper;
1617

1718
/**
@@ -22,9 +23,6 @@ abstract class AbstractApplication implements ApplicationInterface
2223
{
2324
use InputOutputTrait, SimpleEventTrait;
2425

25-
/** @var bool render no color */
26-
private static $noColor = false;
27-
2826
/**
2927
* @var array
3028
*/
@@ -230,10 +228,10 @@ public function handleException($e)
230228
// open debug, throw exception
231229
if ($this->isDebug()) {
232230
$tpl = <<<ERR
233-
<danger>$title</danger>
231+
<danger>$title</danger>
234232
235233
Message <magenta>%s</magenta>
236-
File <cyan>%s</cyan> line <cyan>%d</cyan>
234+
At File <cyan>%s</cyan> line <cyan>%d</cyan>
237235
Catch by %s()\n
238236
Code Trace:\n%s\n
239237
ERR;
@@ -263,7 +261,7 @@ public function handleException($e)
263261
*/
264262
protected function logError($e)
265263
{
266-
// you can log error ...
264+
// you can log error on sub class ...
267265
}
268266

269267
/**
@@ -282,7 +280,7 @@ protected function filterSpecialCommand($command)
282280
}
283281

284282
if ($this->input->getSameOpt(['no-color'])) {
285-
self::$noColor = true;
283+
Style::setNoColor();
286284
}
287285

288286
$command = $command ?: 'list';
@@ -422,7 +420,7 @@ public function showCommandList($quit = true)
422420
$internalCommands = static::$internalCommands;
423421
ksort($internalCommands);
424422
array_unshift($internalCommands, "\n- <cyan>Internal Commands</cyan>");
425-
423+
426424
$this->output->mList([
427425
//'There are all console controllers and independent commands.',
428426
'Usage:' => "$script {route|command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
@@ -443,7 +441,7 @@ public function showCommandList($quit = true)
443441

444442
unset($controllerArr, $commandArr, $internalCommands);
445443
$this->output->write("More command information, please use: <cyan>$script {command} -h</cyan>");
446-
444+
447445
$quit && $this->stop();
448446
}
449447

@@ -471,14 +469,6 @@ public function addCommandMessage($name, $message)
471469
* getter/setter methods
472470
**********************************************************/
473471

474-
/**
475-
* @return bool
476-
*/
477-
public static function isNoColor(): bool
478-
{
479-
return self::$noColor;
480-
}
481-
482472
/**
483473
* @return array
484474
*/

src/Style/Style.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace Inhere\Console\Style;
1212

13-
use Inhere\Console\Application;
1413
use Inhere\Console\Utils\Helper;
1514

1615
/**
@@ -57,7 +56,7 @@ class Style
5756
* Flag to remove color codes from the output
5857
* @var bool
5958
*/
60-
public $noColor = false;
59+
protected static $noColor = false;
6160

6261
/**
6362
* Array of Style objects
@@ -147,7 +146,7 @@ public function format($text)
147146
}
148147

149148
// if don't support output color text, clear color tag.
150-
if (!Helper::isSupportColor() || Application::isNoColor()) {
149+
if (!Helper::isSupportColor() || self::isNoColor()) {
151150
return static::stripColor($text);
152151
}
153152

@@ -178,7 +177,7 @@ public function format($text)
178177
*/
179178
protected function replaceColor($text, $tag, $match, $style): string
180179
{
181-
$replace = $this->noColor ? $match : sprintf("\033[%sm%s\033[0m", $style, $match);
180+
$replace = self::$noColor ? $match : sprintf("\033[%sm%s\033[0m", $style, $match);
182181

183182
return str_replace("<$tag>$match</$tag>", $replace, $text);
184183
// return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
@@ -297,20 +296,18 @@ public function hasStyle($name): bool
297296
/**
298297
* Method to get property NoColor
299298
*/
300-
public function isNoColor(): bool
299+
public static function isNoColor(): bool
301300
{
302-
return (bool)$this->noColor;
301+
return (bool)self::$noColor;
303302
}
304303

305304
/**
306305
* Method to set property noColor
307306
* @param $noColor
308307
* @return $this
309308
*/
310-
public function setNoColor($noColor)
309+
public static function setNoColor($noColor = true)
311310
{
312-
$this->noColor = (bool)$noColor;
313-
314-
return $this;
311+
self::$noColor = (bool)$noColor;
315312
}
316313
}

src/Utils/AnsiCode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
* Class AnsiCode terminal
1313
* @package Inhere\Console\Utils
1414
*
15-
* \r, \x1B[2K 都是清除本行
16-
* \x0D -> 13 回到行首
17-
* ESC = \x1B -> 27
15+
* 2K 清除本行
16+
* \x0D = \r = 13 回车,回到行首
17+
* ESC = \x1B = 27
1818
*/
1919
final class AnsiCode
2020
{

src/Utils/Show.php

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
*/
3434
class 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

Comments
 (0)