Created
June 17, 2016 02:10
-
-
Save edhaase/0cd2fc9048cc8658703357ed1881a1a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ProgressBar.php | |
* | |
* Extension of Symfony's ProgressBar to limit redraw's to a minimum time | |
*/ | |
namespace App\Lib; | |
use Symfony\Component\Console\Helper\ProgressBar as SymfonyProgressBar; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class ProgressBar extends SymfonyProgressBar | |
{ | |
protected $delay, $lastDraw; | |
/** | |
* @param OutputInterface $output An OutputInterface instance | |
* @param int $max Maximum steps (0 if unknown) | |
*/ | |
public function __construct(OutputInterface $output, $max = 0) | |
{ | |
parent::__construct($output, $max); | |
$this->delay = null; | |
} | |
/** | |
* @param $ms | |
*/ | |
public function setRedrawDelay($ms) | |
{ | |
$this->delay = $ms; | |
$this->setRedrawFrequency(1); | |
} | |
/** | |
* | |
*/ | |
public function display() | |
{ | |
if($this->delay) { | |
$elapsed = round(microtime(true) - $this->lastDraw, 3)*1000; | |
if ($elapsed < $this->delay && $this->getMaxSteps() !== $this->getProgress()) { | |
return; | |
} | |
} | |
$this->lastDraw = microtime(true); | |
parent::display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment