Skip to content

Commit 2ad7142

Browse files
gidoStefano Sala
authored andcommitted
[Console][ProgressBar] Allow to advance past max.
1 parent 13ddfc0 commit 2ad7142

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

Helper/ProgressBar.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,14 @@ class ProgressBar
5252
*
5353
* @param OutputInterface $output An OutputInterface instance
5454
* @param int $max Maximum steps (0 if unknown)
55+
*
56+
* @throws \InvalidArgumentException
5557
*/
5658
public function __construct(OutputInterface $output, $max = 0)
5759
{
58-
if (!is_integer($max) || $max < 0) {
59-
throw new \InvalidArgumentException('Max steps should be a positive integer, 0 or null. Got "%s".', $max);
60-
}
61-
6260
// Disabling output when it does not support ANSI codes as it would result in a broken display anyway.
6361
$this->output = $output->isDecorated() ? $output : new NullOutput();
64-
$this->max = (int) $max;
65-
$this->stepWidth = $this->max > 0 ? Helper::strlen($this->max) : 4;
62+
$this->setMaxSteps($max);
6663

6764
if (!self::$formatters) {
6865
self::$formatters = self::initPlaceholderFormatters();
@@ -167,6 +164,25 @@ public function getStartTime()
167164
return $this->startTime;
168165
}
169166

167+
/**
168+
* Sets the progress bar maximal steps.
169+
*
170+
* @param int The progress bar max steps
171+
*
172+
* @throws \InvalidArgumentException
173+
*/
174+
public function setMaxSteps($max)
175+
{
176+
$max = (int) $max;
177+
178+
if ($max < 0) {
179+
throw new \InvalidArgumentException('Max steps should be a positive integer, 0 or null. Got "%s".', $max);
180+
}
181+
182+
$this->max = $max;
183+
$this->stepWidth = $this->max > 0 ? Helper::strlen($this->max) : 4;
184+
}
185+
170186
/**
171187
* Gets the progress bar maximal steps.
172188
*
@@ -338,8 +354,7 @@ public function setRedrawFrequency($freq)
338354
public function start($max = 0)
339355
{
340356
if (0 !== $max) {
341-
$this->max = $max;
342-
$this->stepWidth = $this->max > 0 ? Helper::strlen($this->max) : 4;
357+
$this->setMaxSteps($max);
343358
}
344359

345360
if (!$this->max) {
@@ -377,7 +392,7 @@ public function setCurrent($step)
377392
}
378393

379394
if ($this->max > 0 && $step > $this->max) {
380-
throw new \LogicException('You can\'t advance the progress bar past the max value.');
395+
$this->max = $step;
381396
}
382397

383398
$prevPeriod = intval($this->step / $this->redrawFreq);

Tests/Helper/ProgressBarTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ public function testAdvanceMultipleTimes()
7171
);
7272
}
7373

74+
public function testAdvanceOverMax()
75+
{
76+
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
77+
$bar->setCurrent(9);
78+
$bar->advance();
79+
$bar->advance();
80+
81+
rewind($output->getStream());
82+
$this->assertEquals(
83+
$this->generateOutput(' 9/10 [=========================>--] 90%').
84+
$this->generateOutput(' 10/10 [============================] 100%').
85+
$this->generateOutput(' 11/11 [============================] 100%'),
86+
stream_get_contents($output->getStream())
87+
);
88+
}
89+
7490
public function testCustomizations()
7591
{
7692
$bar = new ProgressBar($output = $this->getOutputStream(), 10);

0 commit comments

Comments
 (0)