Skip to content

Commit

Permalink
Add an iterate method to the ProgressBar class
Browse files Browse the repository at this point in the history
  • Loading branch information
jvasseur committed Jan 17, 2019
1 parent 5aa0967 commit 223a232
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -32,7 +32,8 @@
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-intl-icu": "~1.0",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php72": "~1.5"
"symfony/polyfill-php72": "~1.5",
"symfony/polyfill-php73": "^1.8.0"
},
"replace": {
"symfony/asset": "self.version",
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added support for hyperlinks
* added `ProgressBar::iterate()` method that simplify updating the progress bar when iterating

4.2.0
-----
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Expand Up @@ -243,6 +243,24 @@ public function setRedrawFrequency(int $freq)
$this->redrawFreq = max($freq, 1);
}

/**
* Returns an iterator that will automatically update the progress bar when iterated.
*
* @param int|null $max Number of steps to complete the bar (0 if indeterminate), if null it will be inferred from $iterable
*/
public function iterate(iterable $iterable, ?int $max = null): iterable
{
$this->start($max ?? (\is_countable($iterable) ? \count($iterable) : 0));

foreach ($iterable as $key => $value) {
yield $key => $value;

$this->advance();
}

$this->finish();
}

/**
* Starts the progress output.
*
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Expand Up @@ -867,6 +867,41 @@ public function provideFormat()
];
}

public function testIterate(): void
{
$bar = new ProgressBar($output = $this->getOutputStream());

$this->assertEquals([1, 2], \iterator_to_array($bar->iterate([1, 2])));

rewind($output->getStream());
$this->assertEquals(
' 0/2 [>---------------------------] 0%'.
$this->generateOutput(' 1/2 [==============>-------------] 50%').
$this->generateOutput(' 2/2 [============================] 100%').
$this->generateOutput(' 2/2 [============================] 100%'),
stream_get_contents($output->getStream())
);
}

public function testIterateUncountable(): void
{
$bar = new ProgressBar($output = $this->getOutputStream());

$this->assertEquals([1, 2], \iterator_to_array($bar->iterate((function () {
yield 1;
yield 2;
})())));

rewind($output->getStream());
$this->assertEquals(
' 0 [>---------------------------]'.
$this->generateOutput(' 1 [->--------------------------]').
$this->generateOutput(' 2 [-->-------------------------]').
$this->generateOutput(' 2 [============================]'),
stream_get_contents($output->getStream())
);
}

protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
{
return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/composer.json
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": "^7.1.3",
"symfony/contracts": "^1.0",
"symfony/polyfill-mbstring": "~1.0"
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php73": "^1.8.0"
},
"require-dev": {
"symfony/config": "~3.4|~4.0",
Expand Down

0 comments on commit 223a232

Please sign in to comment.