diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index b9e837a8d1..d61ba51155 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -5,7 +5,7 @@ CHANGELOG ----- * added `Question::setTrimmable` default to true to allow the answer to be trimmed - * added method `preventRedrawFasterThan()` and `forceRedrawSlowerThan()` on `ProgressBar` + * added method `minSecondsBetweenRedraws()` and `maxSecondsBetweenRedraws()` on `ProgressBar` * `Application` implements `ResetInterface` * marked all dispatched event classes as `@final` * added support for displaying table horizontally diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 3751439fa3..0bb7dfc776 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -256,14 +256,14 @@ final class ProgressBar $this->redrawFreq = null !== $freq ? max(1, $freq) : null; } - public function preventRedrawFasterThan(float $intervalInSeconds): void + public function minSecondsBetweenRedraws(float $seconds): void { - $this->minSecondsBetweenRedraws = $intervalInSeconds; + $this->minSecondsBetweenRedraws = $seconds; } - public function forceRedrawSlowerThan(float $intervalInSeconds): void + public function maxSecondsBetweenRedraws(float $seconds): void { - $this->maxSecondsBetweenRedraws = $intervalInSeconds; + $this->maxSecondsBetweenRedraws = $seconds; } /** diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 877b1dfd19..6da0af20b4 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -943,23 +943,47 @@ class ProgressBarTest extends TestCase putenv('COLUMNS=120'); } - public function testForceRedrawSlowerThan(): void + public function testMinAndMaxSecondsBetweenRedraws(): void + { + $bar = new ProgressBar($output = $this->getOutputStream()); + $bar->setRedrawFrequency(1); + $bar->minSecondsBetweenRedraws(5); + $bar->maxSecondsBetweenRedraws(10); + + $bar->start(); + $bar->setProgress(1); + sleep(10); + $bar->setProgress(2); + sleep(20); + $bar->setProgress(3); + + rewind($output->getStream()); + $this->assertEquals( + ' 0 [>---------------------------]'. + $this->generateOutput(' 2 [-->-------------------------]'). + $this->generateOutput(' 3 [--->------------------------]'), + stream_get_contents($output->getStream()) + ); + } + + public function testMaxSecondsBetweenRedraws(): void { $bar = new ProgressBar($output = $this->getOutputStream(), 0, 0); $bar->setRedrawFrequency(4); // disable step based redraws $bar->start(); + $bar->setProgress(1); // No treshold hit, no redraw - $bar->forceRedrawSlowerThan(2); + $bar->maxSecondsBetweenRedraws(2); sleep(1); - $bar->setProgress(2); // Still no redraw because redraw is forced after 2 seconds only + $bar->setProgress(2); // Still no redraw because it takes 2 seconds for a redraw sleep(1); $bar->setProgress(3); // 1+1 = 2 -> redraw finally $bar->setProgress(4); // step based redraw freq hit, redraw even without sleep $bar->setProgress(5); // No treshold hit, no redraw - $bar->preventRedrawFasterThan(3); + $bar->maxSecondsBetweenRedraws(3); sleep(2); $bar->setProgress(6); // No redraw even though 2 seconds passed. Throttling has priority - $bar->preventRedrawFasterThan(2); + $bar->maxSecondsBetweenRedraws(2); $bar->setProgress(7); // Throttling relaxed, draw rewind($output->getStream()); @@ -972,16 +996,16 @@ class ProgressBarTest extends TestCase ); } - public function testPreventRedrawFasterThan() + public function testMinSecondsBetweenRedraws() { $bar = new ProgressBar($output = $this->getOutputStream(), 0, 0); $bar->setRedrawFrequency(1); - $bar->preventRedrawFasterThan(1); + $bar->minSecondsBetweenRedraws(1); $bar->start(); $bar->setProgress(1); // Too fast, should not draw sleep(1); $bar->setProgress(2); // 1 second passed, draw - $bar->preventRedrawFasterThan(2); + $bar->minSecondsBetweenRedraws(2); sleep(1); $bar->setProgress(3); // 1 second passed but we changed threshold, should not draw sleep(1);