Skip to content

Commit fcc028b

Browse files
committed
[ProgressBar] Replace step based frequency with time-based frequency
1 parent 40fbaa2 commit fcc028b

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
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added option to run suggested command if command is not found and only 1 alternative is available
88
* added option to modify console output and print multiple modifiable sections
9+
* deprecated `ProgressBar::setRedrawFrequency()`
910

1011
4.0.0
1112
-----

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ final class ProgressBar
3232
private $format;
3333
private $internalFormat;
3434
private $redrawFreq = 1;
35+
private $redrawFrequency;
36+
private $lastWriteTime;
3537
private $output;
3638
private $step = 0;
3739
private $max;
@@ -42,23 +44,27 @@ final class ProgressBar
4244
private $messages = array();
4345
private $overwrite = true;
4446
private $terminal;
45-
private $firstRun = true;
4647

48+
private $firstRun = true;
4749
private static $formatters;
4850
private static $formats;
4951

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

6065
$this->output = $output;
6166
$this->setMaxSteps($max);
67+
$this->redrawFrequency = $redrawFrequency;
6268
$this->terminal = new Terminal();
6369

6470
if (!$this->output->isDecorated()) {
@@ -237,9 +243,13 @@ public function setFormat(string $format)
237243
* Sets the redraw frequency.
238244
*
239245
* @param int|float $freq The frequency in steps
246+
*
247+
* @deprecated since version 4.1, to be removed in 5.0. Use $redrawFrequency argument in constructor instead.
240248
*/
241249
public function setRedrawFrequency(int $freq)
242250
{
251+
@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);
252+
243253
$this->redrawFreq = max($freq, 1);
244254
}
245255

@@ -291,7 +301,12 @@ public function setProgress(int $step)
291301
$currPeriod = (int) ($step / $this->redrawFreq);
292302
$this->step = $step;
293303
$this->percent = $this->max ? (float) $this->step / $this->max : 0;
294-
if ($prevPeriod !== $currPeriod || $this->max === $step) {
304+
305+
if (null === $this->redrawFrequency) {
306+
if ($prevPeriod !== $currPeriod || $this->max === $step) {
307+
$this->display();
308+
}
309+
} elseif (microtime(true) - $this->lastWriteTime >= $this->redrawFrequency || $this->max === $step) {
295310
$this->display();
296311
}
297312
}
@@ -398,6 +413,7 @@ private function overwrite(string $message): void
398413
}
399414

400415
$this->firstRun = false;
416+
$this->lastWriteTime = microtime(true);
401417

402418
$this->output->write($message);
403419
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ public function testRedrawFrequency()
456456
);
457457
}
458458

459+
/**
460+
* @group legacy
461+
*/
459462
public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
460463
{
461464
$bar = new ProgressBar($output = $this->getOutputStream());
@@ -471,6 +474,9 @@ public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
471474
);
472475
}
473476

477+
/**
478+
* @group legacy
479+
*/
474480
public function testRedrawFrequencyIsAtLeastOneIfSmallerOneGiven()
475481
{
476482
$bar = new ProgressBar($output = $this->getOutputStream());
@@ -896,4 +902,13 @@ public function testBarWidthWithMultilineFormat()
896902
$this->assertEquals(5, $bar->getBarWidth(), stream_get_contents($output->getStream()));
897903
putenv('COLUMNS=120');
898904
}
905+
906+
/**
907+
* @group legacy
908+
* @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.
909+
*/
910+
public function testEnabledStrictEmailOptionIsMappedToStrictEmailValidationMode()
911+
{
912+
(new ProgressBar($this->getOutputStream()))->setRedrawFrequency(1);
913+
}
899914
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy