10000 [Console] A better progress bar by fabpot · Pull Request #10356 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] A better progress bar #10356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 3, 2014
Prev Previous commit
Next Next commit
[Console] added more default placeholder formatters for the progress bar
  • Loading branch information
fabpot committed Mar 1, 2014
commit 1aa7b8c8b9e0fdf89e4c76d45cf69e709d394134
17 changes: 17 additions & 0 deletions src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,21 @@ public static function formatTime($secs)
return ceil($secs / $format[2]).' '.$format[1];
}
}

public static function formatMemory($memory)
{
if ($memory >= 1024 * 1024 * 1024) {
return sprintf('%.1f GB', $memory / 1024 / 1024 / 1024);
}

if ($memory >= 1024 * 1024) {
return sprintf('%.1f MB', $memory / 1024 / 1024);
}

if ($memory >= 1024) {
return sprintf('%d kB', $memory / 1024);
}

return sprintf('%d B', $memory);
}
}
29 changes: 29 additions & 0 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,35 @@ static private function initPlaceholderFormatters()
'%elapsed%' => function (ProgressBar $bar) {
return str_pad(Helper::formatTime(time() - $bar->getStartTime()), 6, ' ', STR_PAD_LEFT);
},
'%remaining%' => function (ProgressBar $bar) {
if (!$bar->getMaxSteps()) {
throw new \LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
}

if (!$bar->getStep()) {
$remaining = 0;
} else {
$remaining = round((time() - $bar->getStartTime()) / $bar->getStep() * ($bar->getMaxSteps() - $bar->getStep()));
}

return str_pad(Helper::formatTime($remaining), 6, ' ', STR_PAD_LEFT);
},
'%estimated%' => function (ProgressBar $bar) {
if (!$bar->getMaxSteps()) {
throw new \LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
}

if (!$bar->getStep()) {
$estimated = 0;
} else {
$estimated = round((time() - $bar->getStartTime()) / $bar->getStep() * $bar->getMaxSteps());
}

return str_pad(Helper::formatTime($estimated), 6, ' ', STR_PAD_LEFT);
},
'%memory%' => function (ProgressBar $bar) {
return str_pad(Helper::formatMemory(memory_get_usage(true)), 6, ' ', STR_PAD_LEFT);;
},
'%current%' => function (ProgressBar $bar) {
return str_pad($bar->getStep(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
},
Expand Down
0