8000 [Console] Add ability to regress the ProgressBar · symfony/symfony@ceecd00 · GitHub
[go: up one dir, main page]

Skip to content

Commit ceecd00

Browse files
author
James Halsall
committed
[Console] Add ability to regress the ProgressBar
1 parent 3521105 commit ceecd00

File tree

2 files changed

+83
-19
lines changed

2 files changed

+83
-19
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,22 @@ public function start($max = null)
339339
* Advances the progress output X steps.
340340
*
341341
* @param int $step Number of steps to advance
342-
*
343-
* @throws LogicException
344342
*/
345343
public function advance($step = 1)
346344
{
347345
$this->setProgress($this->step + $step);
348346
}
349347

348+
/**
349+
* Regresses the progress output X steps.
350+
*
351+
* @param int $step Number of steps to regress
352+
*/
353+
public function regress($step = 1)
354+
{
355+
$this->setProgress($this->step - $step);
356+
}
357+
350358
/**
351359
* Sets whether to overwrite the progressbar, false for new line.
352360
*
@@ -361,18 +369,15 @@ public function setOverwrite($overwrite)
361369
* Sets the current progress.
362370
*
363371
* @param int $step The current progress
364-
*
365-
* @throws LogicException
366372
*/
367373
public function setProgress($step)
368374
{
369375
$step = (int) $step;
370-
if ($step < $this->step) {
371-
throw new LogicException('You can\'t regress the progress bar.');
372-
}
373376

374377
if ($this->max && $step > $this->max) {
375378
$this->max = $step;
379+
} elseif ($step < 0) {
380+
$step = 0;
376381
}
377382

378383
$prevPeriod = (int) ($this->step / $this->redrawFreq);

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

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,77 @@ public function testAdvanceOverMax()
9696
);
9797
}
9898

99+
public function testRegress()
100+
{
101+
$bar = new ProgressBar($output = $this->getOutputStream());
102+
$bar->start();
103+
$bar->advance();
104+
$bar->advance();
105+
$bar->regress();
106+
107+
rewind($output->getStream());
108+
$this->assertEquals(
109+
' 0 [>---------------------------]'.
110+
$this->generateOutput(' 1 [->--------------------------]').
111+
$this->generateOutput(' 2 [-->-------------------------]').
112+
$this->generateOutput(' 1 [->--------------------------]'),
113+
stream_get_contents($output->getStream())
114+
);
115+
}
116+
117+
public function testRegressWithStep()
118+
{
119+
$bar = new ProgressBar($output = $this->getOutputStream());
120+
$bar->start();
121+
$bar->advance(4);
122+
$bar->advance(4);
123+
$bar->regress(2);
124+
125+
rewind($output->getStream());
126+
$this->assertEquals(
127+
' 0 [>---------------------------]'.
128+
$this->generateOutput(' 4 [---->-----------------------]').
129+
$this->generateOutput(' 8 [-------->-------------------]').
130+
$this->generateOutput(' 6 [------>---------------------]'),
131+
stream_get_contents($output->getStream())
132+
);
133+
}
134+
135+
public function testRegressMultipleTimes()
136+
{
137+
$bar = new ProgressBar($output = $this->getOutputStream());
138+
$bar->start();
139+
$bar->advance(3);
140+
$bar->advance(3);
141+
$bar->regress(1);
142+
$bar->regress(2);
143+
144+
rewind($output->getStream());
145+
$this->assertEquals(
146+
' 0 [>---------------------------]'.
147+
$this->generateOutput(' 3 [--->------------------------]').
148+
$this->generateOutput(' 6 [------>---------------------]').
149+
$this->generateOutput(' 5 [----->----------------------]').
150+
$this->generateOutput(' 3 [--->------------------------]'),
151+
stream_get_contents($output->getStream())
152+
);
153+
}
154+
155+
public function testRegressBelowMin()
156+
{
157+
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
158+
$bar->setProgress(1);
159+
$bar->regress();
160+
$bar->regress();
161+
162+
rewind($output->getStream());
163+
$this->assertEquals(
164+
' 1/10 [==>-------------------------] 10%'.
165+
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
166+
stream_get_contents($output->getStream())
167+
);
168+
}
169+
99170
public function testFormat()
100171
{
101172
$expected =
@@ -282,18 +353,6 @@ public function testSetCurrentBeforeStarting()
282353
$this->assertNotNull($bar->getStartTime());
283354
}
284355

285-
/**
286-
* @expectedException \LogicException
287-
* @expectedExceptionMessage You can't regress the progress bar
288-
*/
289-
public function testRegressProgress()
290-
{
291-
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
292-
$bar->start();
293-
$bar->setProgress(15);
294-
$bar->setProgress(10);
295-
}
296-
297356
public function testRedrawFrequency()
298357
{
299358
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream(), 6));

0 commit comments

Comments
 (0)
0