[Console] added more default placeholder formatters for the progress bar

This commit is contained in:
Fabien Potencier 2014-03-01 08:26:32 +01:00
parent 2a78a09f1b
commit 1aa7b8c8b9
2 changed files with 46 additions and 0 deletions

View File

@ -86,4 +86,21 @@ abstract class Helper implements HelperInterface
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);
}
}

View File

@ -419,6 +419,35 @@ class ProgressBar
'%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);
},