Skip to content

Instantly share code, notes, and snippets.

@edhaase
Created June 17, 2016 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edhaase/0cd2fc9048cc8658703357ed1881a1a3 to your computer and use it in GitHub Desktop.
Save edhaase/0cd2fc9048cc8658703357ed1881a1a3 to your computer and use it in GitHub Desktop.
<?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