[Console] Moved estimated & remaining calculation logic to separate get method
This commit is contained in:
parent
1db7005c2c
commit
19958fba5a
@ -191,11 +191,29 @@ final class ProgressBar
|
|||||||
return $this->percent;
|
return $this->percent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBarOffset(): int
|
public function getBarOffset(): float
|
||||||
{
|
{
|
||||||
return floor($this->max ? $this->percent * $this->barWidth : (null === $this->redrawFreq ? min(5, $this->barWidth / 15) * $this->writeCount : $this->step) % $this->barWidth);
|
return floor($this->max ? $this->percent * $this->barWidth : (null === $this->redrawFreq ? min(5, $this->barWidth / 15) * $this->writeCount : $this->step) % $this->barWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getEstimated(): float
|
||||||
|
{
|
||||||
|
if (!$this->step) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return round((time() - $this->startTime) / $this->step * $this->max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRemaining(): float
|
||||||
|
{
|
||||||
|
if (!$this->step) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return round((time() - $this->startTime) / $this->step * ($this->max - $this->step));
|
||||||
|
}
|
||||||
|
|
||||||
public function setBarWidth(int $size)
|
public function setBarWidth(int $size)
|
||||||
{
|
{
|
||||||
$this->barWidth = max(1, $size);
|
$this->barWidth = max(1, $size);
|
||||||
@ -500,26 +518,14 @@ final class ProgressBar
|
|||||||
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
|
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$bar->getProgress()) {
|
return Helper::formatTime($bar->getRemaining());
|
||||||
$remaining = 0;
|
|
||||||
} else {
|
|
||||||
$remaining = round((time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress()));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Helper::formatTime($remaining);
|
|
||||||
},
|
},
|
||||||
'estimated' => function (self $bar) {
|
'estimated' => function (self $bar) {
|
||||||
if (!$bar->getMaxSteps()) {
|
if (!$bar->getMaxSteps()) {
|
||||||
throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
|
throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$bar->getProgress()) {
|
return Helper::formatTime($bar->getEstimated());
|
||||||
$estimated = 0;
|
|
||||||
} else {
|
|
||||||
$estimated = round((time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Helper::formatTime($estimated);
|
|
||||||
},
|
},
|
||||||
'memory' => function (self $bar) {
|
'memory' => function (self $bar) {
|
||||||
return Helper::formatMemory(memory_get_usage(true));
|
return Helper::formatMemory(memory_get_usage(true));
|
||||||
|
Reference in New Issue
Block a user