Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] Add an iterate method to the ProgressBar class #29753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 3 composer.json
Original file line number Diff line number Diff line change
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"
},
"replace": {
"symfony/asset": "self.version",
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
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 18 src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
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();
chalasr marked this conversation as resolved.
Show resolved Hide resolved
}

$this->finish();
}

/**
* Starts the progress output.
*
Expand Down
35 changes: 35 additions & 0 deletions 35 src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
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 3 src/Symfony/Component/Console/composer.json
Original file line number Diff line number Diff line change
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"
},
"require-dev": {
"symfony/config": "~3.4|~4.0",
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.