Skip to content

Commit 231f37f

Browse files
committed
add new show methods: dynamicText
1 parent 1ca9024 commit 231f37f

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

examples/Controllers/HomeController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected static function commandAliases(): array
4545
'ml' => 'multiList',
4646
'ms' => 'multiSelect',
4747
'sl' => 'splitLine',
48+
'dt' => 'dynamicText',
4849
];
4950
}
5051

@@ -374,6 +375,23 @@ public function pointingCommand()
374375
Show::pointing('Done', true);
375376
}
376377

378+
/**
379+
* dynamic text message example, by Show::dynamicText
380+
*/
381+
public function dynamicTextCommand()
382+
{
383+
$dt = Show::dynamicText('Complete', 'Download file: xyz.zip ... ');
384+
$dt->send('Start');
385+
386+
foreach (['Request','Downloading', 'Save'] as $txt) {
387+
\sleep(2);
388+
$dt->send($txt);
389+
}
390+
391+
\sleep(2);
392+
$dt->send(false);
393+
}
394+
377395
/**
378396
* a progress bar example show, by Show::progressBar()
379397
* @options

src/Utils/Show.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,49 @@ public static function counterTxt(string $msg, $doneMsg = null)
10681068
yield false;
10691069
}
10701070

1071+
/**
1072+
* @param string $doneMsg
1073+
* @param string|null $fixMsg
1074+
* @return \Generator
1075+
*/
1076+
public static function dynamicText(string $doneMsg, string $fixMsg = null)
1077+
{
1078+
$counter = 0;
1079+
$finished = false;
1080+
// $tpl = Cli::isSupportColor() ? "\x0D\x1B[2K" : "\x0D\r";
1081+
$tpl = Cli::isSupportColor() ? "\x0D\x1B[2K" : "\x0D";
1082+
1083+
if ($fixMsg) {
1084+
$tpl .= self::getStyle()->render($fixMsg);
1085+
}
1086+
1087+
$tpl .= '%s';
1088+
$doneMsg = $doneMsg ? self::getStyle()->render($doneMsg) : '';
1089+
1090+
while (true) {
1091+
if ($finished) {
1092+
return;
1093+
}
1094+
1095+
$msg = yield;
1096+
1097+
if ($msg === false) {
1098+
$counter++;
1099+
$finished = true;
1100+
$msg = $doneMsg ?: '';
1101+
}
1102+
1103+
printf($tpl, $msg);
1104+
1105+
if ($finished) {
1106+
echo "\n";
1107+
break;
1108+
}
1109+
}
1110+
1111+
yield $counter;
1112+
}
1113+
10711114
/**
10721115
* @param int $total
10731116
* @param string $msg

0 commit comments

Comments
 (0)