diff --git a/src/Symfony/Component/Console/Helper/ProgressHelper.php b/src/Symfony/Component/Console/Helper/ProgressHelper.php index b23ab21d4ec52..270120d9a320c 100644 --- a/src/Symfony/Component/Console/Helper/ProgressHelper.php +++ b/src/Symfony/Component/Console/Helper/ProgressHelper.php @@ -23,10 +23,10 @@ class ProgressHelper extends Helper { const FORMAT_QUIET = ' %percent%%'; const FORMAT_NORMAL = ' %current%/%max% [%bar%] %percent%%'; - const FORMAT_VERBOSE = ' %current%/%max% [%bar%] %percent%% Elapsed: %elapsed%'; + const FORMAT_VERBOSE = ' %current%/%max% [%bar%] %percent%% Elapsed: %elapsed% (%memory%)'; const FORMAT_QUIET_NOMAX = ' %current%'; const FORMAT_NORMAL_NOMAX = ' %current% [%bar%]'; - const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed%'; + const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed% Estimated: %estimated% (%memory%)'; // options private $barWidth = 28; @@ -76,6 +76,8 @@ class ProgressHelper extends Helper 'bar', 'percent', 'elapsed', + 'estimated', + 'memory' ); /** @@ -91,10 +93,12 @@ class ProgressHelper extends Helper * @var array */ private $widths = array( - 'current' => 4, - 'max' => 4, - 'percent' => 3, - 'elapsed' => 6, + 'current' => 4, + 'max' => 4, + 'percent' => 3, + 'elapsed' => 6, + 'estimated' => 6, + 'memory' => 6 ); /** @@ -392,9 +396,38 @@ private function generate($finish = false) $vars['percent'] = str_pad(floor($percent * 100), $this->widths['percent'], ' ', STR_PAD_LEFT); } + if (isset($this->formatVars['memory'])) { + $vars['memory'] = str_pad($this->humaneMemory(), $this->widths['memory'], ' ', STR_PAD_LEFT); + } + + if (isset($this->formatVars['estimated'])) { + $eta = round((time() - $this->startTime) * ($this->max - $this->current)) / $this->current; + $vars['estimated'] = str_pad($this->humaneTime($eta), $this->widths['estimated'], ' ', STR_PAD_LEFT); + } + return $vars; } + /** + * Converts memory usage to human-readable format. + * + * @return string + */ + private function humaneMemory() + { + $memory = memory_get_usage(true); + if ($memory>1024*1024*1024*10) { + return sprintf('%.2fGB', $memory/1024/1024/1024); + } elseif ($memory>1024*1024*10) { + return sprintf('%.2fMB', $memory/1024/1024); + } elseif ($memory>1024*10) { + return sprintf('%.2fkB', $memory/1024); + } + + return sprintf('%.2fB', $memory); + + } + /** * Converts seconds into human-readable format. *