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 (https://github.com/symfony/symfony-docs/pull/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
-------

e6ee7b07f3 [Console] Rename some methods related to redraw frequency
This commit is contained in:
Fabien Potencier 2019-11-04 13:46:19 +01:00
commit e4c661901e
3 changed files with 37 additions and 13 deletions

View File

@ -6,7 +6,7 @@ CHANGELOG
* deprecated finding hidden commands using an abbreviation, use the full name instead
* 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

View File

@ -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;
}
/**

View File

@ -936,23 +936,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());
@ -965,16 +989,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);