8000 [ProgressBar] Replace step based frequency with time-based frequency · symfony/symfony@e65af04 · GitHub
[go: up one dir, main page]

Skip to content

Commit e65af04

Browse files
committed
[ProgressBar] Replace step based frequency with time-based frequency
1 parent 308e12c commit e65af04

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

UPGRADE-5.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 4.x to 5.0
22
=======================
33

4+
Console
5+
------
6+
7+
* The `ProgressBar::setRedrawFrequency()` method has been removed and replaced with time-based redraw frequency set via constructor.
8+
49
Config
510
------
611

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added option to run suggested command if command is not found and only 1 alternative is available
8+
* deprecated `ProgressBar::setRedrawFrequency()`
89

910
4.0.0
1011
-----

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ final class ProgressBar
3131
private $format;
3232
private $internalFormat;
3333
private $redrawFreq = 1;
34+
private $redrawFrequency;
35+
private $lastWriteTime;
3436
private $output;
3537
private $step = 0;
3638
private $max;
@@ -41,23 +43,27 @@ final class ProgressBar
4143
private $messages = array();
4244
private $overwrite = true;
4345
private $terminal;
44-
private $firstRun = true;
4546

47+
private $firstRun = true;
4648
private static $formatters;
4749
private static $formats;
4850

4951
/**
50-
* @param OutputInterface $output An OutputInterface instance
51-
* @param int $max Maximum steps (0 if unknown)
52+
* @param OutputInterface $output An OutputInterface instance
53+
* @param int $max Maximum steps (0 if unknown)
54+
* @param float|null $redrawFrequency Frequency of redrawing in seconds.
55+
* If null, value in $redrawFreq is used instead,
56+
* which is frequency of redrawing in steps.
5257
*/
53-
public function __construct(OutputInterface $output, int $max = 0)
58+
public function __construct(OutputInterface $output, int $max = 0, float $redrawFrequency = null /* .1 */)
5459
{
5560
if ($output instanceof ConsoleOutputInterface) {
5661
$output = $output->getErrorOutput();
5762
}
5863

5964
$this->output = $output;
6065
$this->setMaxSteps($max);
66+
$this->redrawFrequency = $redrawFrequency;
6167
$this->terminal = new Terminal();
6268

6369
if (!$this->output->isDecorated()) {
@@ -236,9 +242,13 @@ public function setFormat(string $format)
236242
* Sets the redraw frequency.
237243
*
238244
* @param int|float $freq The frequency in steps
245+
*
246+
* @deprecated since version 4.1, to be removed in 5.0. Use $redrawFrequency argument in constructor instead.
239247
*/
240248
public function setRedrawFrequency(int $freq)
241249
{
250+
@trigger_error(sprintf('The %s() method is deprecated since Symfony 4.1 and will be removed in 5.0. Use $redrawFrequency argument in constructor instead.', __METHOD__), E_USER_DEPRECATED);
251+
242252
$this->redrawFreq = max($freq, 1);
243253
}
244254

@@ -290,7 +300,12 @@ public function setProgress(int $step)
290300
$currPeriod = (int) ($step / $this->redrawFreq);
291301
$this->step = $step;
292302
$this->percent = $this->max ? (float) $this->step / $this->max : 0;
293-
if ($prevPeriod !== $currPeriod || $this->max === $step) {
303+
304+
if (null === $this->redrawFrequency) {
305+
if ($prevPeriod !== $currPeriod || $this->max === $step) {
306+
$this->display();
307+
}
308+
} elseif (microtime(true) - $this->lastWriteTime >= $this->redrawFrequency || $this->max === $step) {
294309
$this->display();
295310
}
296311
}
@@ -391,6 +406,7 @@ private function overwrite(string $message): void
391406
}
392407

393408
$this->firstRun = false;
409+
$this->lastWriteTime = microtime(true);
394410

395411
$this->output->write($message);
396412
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ public function testRedrawFrequency()
372372
);
373373
}
374374

375+
/**
376+
* @group legacy
377+
*/
375378
public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
376379
{
377380
$bar = new ProgressBar($output = $this->getOutputStream());
@@ -387,6 +390,9 @@ public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
387390
);
388391
}
389392

393+
/**
394+
* @group legacy
395+
*/
390396
public function testRedrawFrequencyIsAtLeastOneIfSmallerOneGiven()
391397
{
392398
$bar = new ProgressBar($output = $this->getOutputStream());
@@ -789,4 +795,13 @@ public function testBarWidthWithMultilineFormat()
789795
$this->assertEquals(5, $bar->getBarWidth(), stream_get_contents($output->getStream()));
790796
putenv('COLUMNS=120');
791797
}
798+
799+
/**
800+
* @group legacy
801+
* @expectedDeprecation The Symfony\Component\Console\Helper\ProgressBar::setRedrawFrequency() method is deprecated since Symfony 4.1 and will be removed in 5.0. Use $redrawFrequency argument in constructor instead.
802+
*/
803+
public function testEnabledStrictEmailOptionIsMappedToStrictEmailValidationMode()
804+
{
805+
(new ProgressBar($this->getOutputStream()))->setRedrawFrequency(1);
806+
}
792807
}

0 commit comments

Comments
 (0)
0