Skip to content

Commit b68e5b3

Browse files
committed
[ProgressBar] Add preventRedrawFasterThan, forceRedrawSlowerThan methods
1 parent e8dac19 commit b68e5b3

File tree

2 files changed

+80
-78
lines changed

2 files changed

+80
-78
lines changed

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ final class ProgressBar
3131
private $progressChar = '>';
3232
private $format;
3333
private $internalFormat;
34-
private $redrawFreq;
34+
private $redrawFreq = 1;
3535
private $lastWriteTime;
36+
private $minSecondsBetweenRedraws = 0;
37+
private $maxSecondsBetweenRedraws = 1;
3638
private $output;
3739
private $step = 0;
3840
private $max;
@@ -244,6 +246,16 @@ public function setRedrawFrequency(int $freq)
244246
$this->redrawFreq = max($freq, 1);
245247
}
246248

249+
public function preventRedrawFasterThan(float $intervalInSeconds): void
250+
{
251+
$this->minSecondsBetweenRedraws = $intervalInSeconds;
252+
}
253+
254+
public function forceRedrawSlowerThan(float $intervalInSeconds): void
255+
{
256+
$this->maxSecondsBetweenRedraws = $intervalInSeconds;
257+
}
258+
247259
/**
248260
* Returns an iterator that will automatically update the progress bar when iterated.
249261
*
@@ -306,22 +318,26 @@ public function setProgress(int $step)
306318
$step = 0;
307319
}
308320

309-
$prevStep = $this->step;
321+
$prevPeriod = (int) ($this->step / $this->redrawFreq);
322+
$currPeriod = (int) ($step / $this->redrawFreq);
310323
$this->step = $step;
311324
$this->percent = $this->max ? (float) $this->step / $this->max : 0;
325+
$timeInterval = microtime(true) - $this->lastWriteTime;
312326

313-
if (null === $this->redrawFreq) {
314-
if (microtime(true) - $this->lastWriteTime >= .04 || $this->max === $step) {
315-
$this->display();
316-
}
327+
// Draw regardless of other limits
328+
if ($this->max === $step) {
329+
$this->display();
317330

318331
return;
319332
}
320333

321-
$prevPeriod = (int) ($prevStep / $this->redrawFreq);
322-
$currPeriod = (int) ($step / $this->redrawFreq);
334+
// Throttling
335+
if ($timeInterval < $this->minSecondsBetweenRedraws) {
336+
return;
337+
}
323338

324-
if ($prevPeriod !== $currPeriod || $this->max === $step) {
339+
// Draw each step period, but not too late
340+
if ($prevPeriod !== $currPeriod || $timeInterval >= $this->maxSecondsBetweenRedraws) {
325341
$this->display();
326342
}
327343
}

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