Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4c6619

Browse files
committed
feature #33732 [Console] Rename some methods related to redraw frequency (javiereguiluz)
This PR was squashed before being merged into the 4.4 branch (closes #33732). Discussion ---------- [Console] Rename some methods related to redraw frequency | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - In #26339 we added `preventRedrawFasterThan()` and `forceRedrawSlowerThan()`. While merging the docs for them (symfony/symfony-docs#12364) I thought that the method names are a bit hard to understand. In this PR I propose a renaming for your consideration. Thanks! In the following example, we want to update the progress bar every 100 iterations, but not faster than 100ms or slower than 200ms. **Before** ```php $progressBar = new ProgressBar($output, 50000); $progressBar->start(); $progressBar->setRedrawFrequency(100); $progressBar->preventRedrawFasterThan(0.1); $progressBar->forceRedrawSlowerThan(0.2); ``` **After** ```php $progressBar = new ProgressBar($output, 50000); $progressBar->start(); $progressBar->setRedrawFrequency(100); $progressBar->maxRefreshInterval(0.1); $progressBar->minRefreshInterval(0.2); ``` Commits ------- e6ee7b0 [Console] Rename some methods related to redraw frequency
2 parents 13dd18c + e6ee7b0 commit e4c6619

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* deprecated finding hidden commands using an abbreviation, use the full name instead
88
* added `Question::setTrimmable` default to true to allow the answer to be trimmed
9-
* added method `preventRedrawFasterThan()` and `forceRedrawSlowerThan()` on `ProgressBar`
9+
* added method `minSecondsBetweenRedraws()` and `maxSecondsBetweenRedraws()` on `ProgressBar`
1010
* `Application` implements `ResetInterface`
1111
* marked all dispatched event classes as `@final`
1212
* added support for displaying table horizontally

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ public function setRedrawFrequency(?int $freq)
256256
$this->redrawFreq = null !== $freq ? max(1, $freq) : null;
257257
}
258258

259-
public function preventRedrawFasterThan(float $intervalInSeconds): void
259+
public function minSecondsBetweenRedraws(float $seconds): void
260260
{
261-
$this->minSecondsBetweenRedraws = $intervalInSeconds;
261+
$this->minSecondsBetweenRedraws = $seconds;
262262
}
263263

264-
public function forceRedrawSlowerThan(float $intervalInSeconds): void
264+
public function maxSecondsBetweenRedraws(float $seconds): void
265265
{
266-
$this->maxSecondsBetweenRedraws = $intervalInSeconds;
266+
$this->maxSecondsBetweenRedraws = $seconds;
267267
}
268268

269269
/**

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -936,23 +936,47 @@ public function testBarWidthWithMultilineFormat()
936936
putenv('COLUMNS=120');
937937
}
938938

939-
public function testForceRedrawSlowerThan(): void
939+
public function testMinAndMaxSecondsBetweenRedraws(): void
940+
{
941+
$bar = new ProgressBar($output = $this->getOutputStream());
942+
$bar->setRedrawFrequency(1);
943+
$bar->minSecondsBetweenRedraws(5);
944+
$bar->maxSecondsBetweenRedraws(10);
945+
946+
$bar->start();
947+
$bar->setProgress(1);
948+
sleep(10);
949+
$bar->setProgress(2);
950+
sleep(20);
951+
$bar->setProgress(3);
952+
953+
rewind($output->getStream());
954+
$this->assertEquals(
955+
' 0 [>---------------------------]'.
956+
$this->generateOutput(' 2 [-->-------------------------]').
957+
$this->generateOutput(' 3 [--->------------------------]'),
958+
stream_get_contents($output->getStream())
959+
);
960+
}
961+
962+
public function testMaxSecondsBetweenRedraws(): void
940963
{
941964
$bar = new ProgressBar($output = $this->getOutputStream(), 0, 0);
942965
$bar->setRedrawFrequency(4); // disable step based redraws
943966
$bar->start();
967+
944968
$bar->setProgress(1); // No treshold hit, no redraw
945-
$bar->forceRedrawSlowerThan(2);
969+
$bar->maxSecondsBetweenRedraws(2);
946970
sleep(1);
947-
$bar->setProgress(2); // Still no redraw because redraw is forced after 2 seconds only
971+
$bar->setProgress(2); // Still no redraw because it takes 2 seconds for a redraw
948972
sleep(1);
949973
$bar->setProgress(3); // 1+1 = 2 -> redraw finally
950974
$bar->setProgress(4); // step based redraw freq hit, redraw even without sleep
951975
$bar->setProgress(5); // No treshold hit, no redraw
952-
$bar->preventRedrawFasterThan(3);
976+
$bar->maxSecondsBetweenRedraws(3);
953977
sleep(2);
954978
$bar->setProgress(6); // No redraw even though 2 seconds passed. Throttling has priority
955-
$bar->preventRedrawFasterThan(2);
979+
$bar->maxSecondsBetweenRedraws(2);
956980
$bar->setProgress(7); // Throttling relaxed, draw
957981

958982
rewind($output->getStream());
@@ -965,16 +989,16 @@ public function testForceRedrawSlowerThan(): void
965989
);
966990
}
967991

968-
public function testPreventRedrawFasterThan()
992+
public function testMinSecondsBetweenRedraws()
969993
{
970994
$bar = new ProgressBar($output = $this->getOutputStream(), 0, 0);
971995
$bar->setRedrawFrequency(1);
972-
$bar->preventRedrawFasterThan(1);
996+
$bar->minSecondsBetweenRedraws(1);
973997
$bar->start();
974998
$bar->setProgress(1); // Too fast, should not draw
975999
sleep(1);
9761000
$bar->setProgress(2); // 1 second passed, draw
977-
$bar->preventRedrawFasterThan(2);
1001+
$bar->minSecondsBetweenRedraws(2);
9781002
sleep(1);
9791003
$bar->setProgress(3); // 1 second passed but we changed threshold, should not draw
9801004
sleep(1);

0 commit comments

Comments
 (0)