Skip to content

Commit 80f36c5

Browse files
committed
Add finished indicator to ProgressIndicator
1 parent 68a5704 commit 80f36c5

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/Symfony/Component/Console/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
CHANGELOG
22
=========
33

4+
* Add a configurable finished indicator to the progress indicator to show that the progress is finished
5+
46
7.1
57
---
68

src/Symfony/Component/Console/Helper/ProgressIndicator.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ class ProgressIndicator
3636
private ?string $message = null;
3737
private array $indicatorValues;
3838
private int $indicatorCurrent;
39+
private string $finishedIndicatorValue;
3940
private float $indicatorUpdateTime;
4041
private bool $started = false;
42+
private bool $finished = false;
4143

4244
/**
4345
* @var array<string, callable>
@@ -53,17 +55,20 @@ public function __construct(
5355
?string $format = null,
5456
private int $indicatorChangeInterval = 100,
5557
?array $indicatorValues = null,
58+
?string $finishedIndicatorValue = null,
5659
) {
5760
$format ??= $this->determineBestFormat();
5861
$indicatorValues ??= ['-', '\\', '|', '/'];
5962
$indicatorValues = array_values($indicatorValues);
63+
$finishedIndicatorValue ??= '';
6064

6165
if (2 > \count($indicatorValues)) {
6266
throw new InvalidArgumentException('Must have at least 2 indicator value characters.');
6367
}
6468

6569
$this->format = self::getFormatDefinition($format);
6670
$this->indicatorValues = $indicatorValues;
71+
$this->finishedIndicatorValue = $finishedIndicatorValue;
6772
$this->startTime = time();
6873
}
6974

@@ -88,6 +93,7 @@ public function start(string $message): void
8893

8994
$this->message = $message;
9095
$this->started = true;
96+
$this->finished = false;
9197
$this->startTime = time();
9298
$this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
9399
$this->indicatorCurrent = 0;
@@ -123,12 +129,17 @@ public function advance(): void
123129
/**
124130
* Finish the indicator with message.
125131
*/
126-
public function finish(string $message): void
132+
public function finish(string $message, ?string $finishedIndicator = null): void
127133
{
128134
if (!$this->started) {
129135
throw new LogicException('Progress indicator has not yet been started.');
130136
}
131137

138+
if ($finishedIndicator !== null) {
139+
$this->finishedIndicatorValue = $finishedIndicator;
140+
}
141+
142+
$this->finished = true;
132143
$this->message = $message;
133144
$this->display();
134145
$this->output->writeln('');
@@ -215,7 +226,7 @@ private function getCurrentTimeInMilliseconds(): float
215226
private static function initPlaceholderFormatters(): array
216227
{
217228
return [
218-
'indicator' => fn (self $indicator) => $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)],
229+
'indicator' => fn (self $indicator) => $indicator->finished ? $indicator->finishedIndicatorValue : $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)],
219230
'message' => fn (self $indicator) => $indicator->message,
220231
'elapsed' => fn (self $indicator) => Helper::formatTime(time() - $indicator->startTime, 2),
221232
'memory' => fn () => Helper::formatMemory(memory_get_usage(true)),

src/Symfony/Component/Console/Tests/Helper/ProgressIndicatorTest.php

+38-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public function testDefaultIndicator()
5454
$this->generateOutput(' \\ Starting...').
5555
$this->generateOutput(' \\ Advancing...').
5656
$this->generateOutput(' | Advancing...').
57-
$this->generateOutput(' | Done...').
57+
$this->generateOutput(' Done...').
5858
\PHP_EOL.
5959
$this->generateOutput(' - Starting Again...').
6060
$this->generateOutput(' \\ Starting Again...').
61-
$this->generateOutput(' \\ Done Again...').
61+
$this->generateOutput(' Done Again...').
6262
\PHP_EOL,
6363
stream_get_contents($output->getStream())
6464
);
@@ -109,6 +109,42 @@ public function testCustomIndicatorValues()
109109
);
110110
}
111111

112+
public function testCustomFinishedIndicatorValue()
113+
{
114+
$bar = new ProgressIndicator($output = $this->getOutputStream(), null, 100, ['a', 'b'], '');
115+
116+
$bar->start('Starting...');
117+
usleep(101000);
118+
$bar->finish('Done');
119+
120+
121+
rewind($output->getStream());
122+
123+
$this->assertEquals(
124+
$this->generateOutput(' a Starting...').
125+
$this->generateOutput(' ✅ Done'). \PHP_EOL,
126+
stream_get_contents($output->getStream())
127+
);
128+
}
129+
130+
public function testCustomFinishedIndicatorWhenFinishingProcess()
131+
{
132+
$bar = new ProgressIndicator($output = $this->getOutputStream(), null, 100, ['a', 'b']);
133+
134+
$bar->start('Starting...');
135+
usleep(101000);
136+
$bar->finish('Process failed', '');
137+
138+
139+
rewind($output->getStream());
140+
141+
$this->assertEquals(
142+
$this->generateOutput(' a Starting...').
143+
$this->generateOutput(' ❌ Process failed'). \PHP_EOL,
144+
stream_get_contents($output->getStream())
145+
);
146+
}
147+
112148
public function testCannotSetInvalidIndicatorCharacters()
113149
{
114150
$this->expectException(\InvalidArgumentException::class);

0 commit comments

Comments
 (0)