Skip to content

Commit 1aa7b8c

Browse files
committed
[Console] added more default placeholder formatters for the progress bar
1 parent 2a78a09 commit 1aa7b8c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Symfony/Component/Console/Helper/Helper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,21 @@ public static function formatTime($secs)
8686
return ceil($secs / $format[2]).' '.$format[1];
8787
}
8888
}
89+
90+
public static function formatMemory($memory)
91+
{
92+
if ($memory >= 1024 * 1024 * 1024) {
93+
return sprintf('%.1f GB', $memory / 1024 / 1024 / 1024);
94+
}
95+
96+
if ($memory >= 1024 * 1024) {
97+
return sprintf('%.1f MB', $memory / 1024 / 1024);
98+
}
99+
100+
if ($memory >= 1024) {
101+
return sprintf('%d kB', $memory / 1024);
102+
}
103+
104+
return sprintf('%d B', $memory);
105+
}
89106
}

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,35 @@ static private function initPlaceholderFormatters()
419419
'%elapsed%' => function (ProgressBar $bar) {
420420
return str_pad(Helper::formatTime(time() - $bar->getStartTime()), 6, ' ', STR_PAD_LEFT);
421421
},
422+
'%remaining%' => function (ProgressBar $bar) {
423+
if (!$bar->getMaxSteps()) {
424+
throw new \LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
425+
}
426+
427+
if (!$bar->getStep()) {
428+
$remaining = 0;
429+
} else {
430+
$remaining = round((time() - $bar->getStartTime()) / $bar->getStep() * ($bar->getMaxSteps() - $bar->getStep()));
431+
}
432+
433+
return str_pad(Helper::formatTime($remaining), 6, ' ', STR_PAD_LEFT);
434+
},
435+
'%estimated%' => function (ProgressBar $bar) {
436+
if (!$bar->getMaxSteps()) {
437+
throw new \LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
438+
}
439+
440+
if (!$bar->getStep()) {
441+
$estimated = 0;
442+
} else {
443+
$estimated = round((time() - $bar->getStartTime()) / $bar->getStep() * $bar->getMaxSteps());
444+
}
445+
446+
return str_pad(Helper::formatTime($estimated), 6, ' ', STR_PAD_LEFT);
447+
},
448+
'%memory%' => function (ProgressBar $bar) {
449+
return str_pad(Helper::formatMemory(memory_get_usage(true)), 6, ' ', STR_PAD_LEFT);;
450+
},
422451
'%current%' => function (ProgressBar $bar) {
423452
return str_pad($bar->getStep(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
424453
},

0 commit comments

Comments
 (0)