Skip to content

[Console][ProgressHelper] Added estimated time and memory usage #9572

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/Symfony/Component/Console/Helper/ProgressHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +76,8 @@ class ProgressHelper extends Helper
'bar',
'percent',
'elapsed',
'estimated',
'memory'
);

/**
Expand All @@ -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
);

/**
Expand Down Expand Up @@ -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.
*
Expand Down