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

Commit eb35531

Browse filesBrowse files
committed
Add an iterate method to the ProgressBar class
1 parent 5aa0967 commit eb35531
Copy full SHA for eb35531

File tree

5 files changed

+58
-2
lines changed
Filter options

5 files changed

+58
-2
lines changed

‎composer.json

Copy file name to clipboardExpand all lines: composer.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"symfony/polyfill-ctype": "~1.8",
3333
"symfony/polyfill-intl-icu": "~1.0",
3434
"symfony/polyfill-mbstring": "~1.0",
35-
"symfony/polyfill-php72": "~1.5"
35+
"symfony/polyfill-php72": "~1.5",
36+
"symfony/polyfill-php73": "^1.8"
3637
},
3738
"replace": {
3839
"symfony/asset": "self.version",

‎src/Symfony/Component/Console/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

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

910
4.2.0
1011
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/ProgressBar.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,24 @@ public function setRedrawFrequency(int $freq)
243243
$this->redrawFreq = max($freq, 1);
244244
}
245245

246+
/**
247+
* Returns an iterator that will automatically update the progress bar when iterated.
248+
*
249+
* @param int|null $max Number of steps to complete the bar (0 if indeterminate), if null it will be inferred from $iterable
250+
*/
251+
public function iterate(iterable $iterable, ?int $max = null): iterable
252+
{
253+
$this->start($max ?? (\is_countable($iterable) ? \count($iterable) : 0));
254+
255+
foreach ($iterable as $key => $value) {
256+
yield $key => $value;
257+
258+
$this->advance();
259+
}
260+
261+
$this->finish();
262+
}
263+
246264
/**
247265
* Starts the progress output.
248266
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,41 @@ public function provideFormat()
867867
];
868868
}
869869

870+
public function testIterate(): void
871+
{
872+
$bar = new ProgressBar($output = $this->getOutputStream());
873+
874+
$this->assertEquals([1, 2], \iterator_to_array($bar->iterate([1, 2])));
875+
876+
rewind($output->getStream());
877+
$this->assertEquals(
878+
' 0/2 [>---------------------------] 0%'.
879+
$this->generateOutput(' 1/2 [==============>-------------] 50%').
880+
$this->generateOutput(' 2/2 [============================] 100%').
881+
$this->generateOutput(' 2/2 [============================] 100%'),
882+
stream_get_contents($output->getStream())
883+
);
884+
}
885+
886+
public function testIterateUncountable(): void
887+
{
888+
$bar = new ProgressBar($output = $this->getOutputStream());
889+
890+
$this->assertEquals([1, 2], \iterator_to_array($bar->iterate((function () {
891+
yield 1;
892+
yield 2;
893+
})())));
894+
895+
rewind($output->getStream());
896+
$this->assertEquals(
897+
' 0 [>---------------------------]'.
898+
$this->generateOutput(' 1 [->--------------------------]').
899+
$this->generateOutput(' 2 [-->-------------------------]').
900+
$this->generateOutput(' 2 [============================]'),
901+
stream_get_contents($output->getStream())
902+
);
903+
}
904+
870905
protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
871906
{
872907
return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);

‎src/Symfony/Component/Console/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/composer.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": "^7.1.3",
2020
"symfony/contracts": "^1.0",
21-
"symfony/polyfill-mbstring": "~1.0"
21+
"symfony/polyfill-mbstring": "~1.0",
22+
"symfony/polyfill-php73": "^1.8"
2223
},
2324
"require-dev": {
2425
"symfony/config": "~3.4|~4.0",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.