Last active
August 29, 2015 13:59
-
-
Save markomafs/10947269 to your computer and use it in GitHub Desktop.
Trait to Implement Progress Bar for CLI's
This file contains hidden or 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 | |
/** | |
* Progress Bar Trait | |
* @package Core\Traits | |
* @author Marco Souza<[email protected]> | |
*/ | |
namespace Core\Traits; | |
/** | |
* Class ProgressBar | |
* @package Core\Traits | |
*/ | |
trait ProgressBar | |
{ | |
private $startTime; | |
private $currentTime; | |
private $spentTime; | |
private $pendingTime; | |
private $totalItems = 0; | |
private $width = 30; | |
private $percentage = 0; | |
private $formattedPercentage = 0; | |
private $countBar = 0; | |
private $stdOut = ''; | |
/** | |
* @param $quantity | |
*/ | |
protected function setTotalAndResetStartTime($quantity){ | |
$this->totalItems = $quantity; | |
$this->resetStartTime(); | |
} | |
protected function resetStartTime(){ | |
$this->startTime = time(); | |
} | |
/** | |
* @param $size | |
*/ | |
protected function setWidth($size){ | |
$this->width = $size; | |
} | |
/** | |
* @param $currentPosition | |
*/ | |
protected function updateStatus($currentPosition){ | |
$this->recalculateTime($currentPosition); | |
$this->recalculateStatus($currentPosition); | |
$this->setStatusBar($currentPosition); | |
$this->showStatusBar(); | |
} | |
private function recalculateTime($currentPosition){ | |
$this->currentTime = time(); | |
$this->spentTime = $this->currentTime - $this->startTime; | |
$averageTime = ($this->spentTime)/$currentPosition; | |
$pendingItems = $this->totalItems - $currentPosition; | |
$this->pendingTime = round($averageTime * $pendingItems, 0); | |
} | |
private function recalculateStatus($currentPosition){ | |
$this->percentage = (double)($currentPosition/$this->totalItems); | |
$this->formattedPercentage = number_format($this->percentage*100, 0); | |
$this->countBar = floor($this->percentage*$this->width); | |
} | |
private function setStatusBar($currentPosition){ | |
$this->appendBar(); | |
$this->appendStats($currentPosition); | |
if($currentPosition == $this->totalItems) { | |
$this->stdOut .= "\n"; | |
} | |
} | |
private function appendBar(){ | |
$barProcessed = str_repeat('=', $this->countBar); | |
$this->stdOut ="\r[".$barProcessed; | |
$barPending = str_repeat(' ', $this->width-$this->countBar); | |
$this->stdOut .= ($this->countBar < $this->width ? '>' : '=').$barPending; | |
$this->stdOut .=']'; | |
} | |
private function appendStats($currentPosition){ | |
$this->stdOut .= " {$this->formattedPercentage}% {$currentPosition}/{$this->totalItems}"; | |
$this->stdOut .= ' remaining:'.$this->getVerboseTime($this->pendingTime).'. elapsed:'.$this->getVerboseTime($this->spentTime).'.'; | |
} | |
private function getVerboseTime($seconds) | |
{ | |
switch (true){ | |
case $seconds > 3600 : | |
$verboseTime = gmdate(" G \h I \m s \s", $seconds); | |
break; | |
case $seconds > 60 : | |
$verboseTime = gmdate(" I \m s \s", $seconds); | |
break; | |
default : | |
$verboseTime = gmdate(" s \s", $seconds); | |
break; | |
} | |
return $verboseTime; | |
} | |
private function showStatusBar(){ | |
echo $this->stdOut; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment