Skip to content

Commit

Permalink
[Console] converted options to proper setters in ProgressHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 29, 2012
1 parent 2598323 commit 729e3bf
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 48 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Helper\ProgressHelper;

/**
* An Application is the container for a collection of commands.
Expand Down Expand Up @@ -934,6 +935,7 @@ protected function getDefaultHelperSet()
return new HelperSet(array(
new FormatterHelper(),
new DialogHelper(),
new ProgressHelper(),
));
}

Expand Down
153 changes: 105 additions & 48 deletions src/Symfony/Component/Console/Helper/ProgressHelper.php
Expand Up @@ -17,6 +17,7 @@
* The Progress class providers helpers to display progress output.
*
* @author Chris Jones <leeked@gmail.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
class ProgressHelper extends Helper
{
Expand All @@ -27,29 +28,15 @@ class ProgressHelper extends Helper
const FORMAT_NORMAL_NOMAX = ' %current% [%bar%]';
const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed%';

/**
* @var array
*/
protected $options = array(
'barWidth' => null,
'barChar' => null,
'emptyBarChar' => null,
'progressChar' => null,
'format' => null,
'redrawFreq' => null,
);
// options
private $barWidth = 28;
private $barChar = '=';
private $emptyBarChar = '-';
private $progressChar = '>';
private $format = self::FORMAT_NORMAL_NOMAX;
private $redrawFreq = 1;

/**
* @var array
*/
private $defaultOptions = array(
'barWidth' => 28,
'barChar' => '=',
'emptyBarChar' => '-',
'progressChar' => '>',
'format' => self::FORMAT_NORMAL_NOMAX,
'redrawFreq' => 1,
);
private $barCharOriginal;

/**
* @var OutputInterface
Expand Down Expand Up @@ -126,14 +113,73 @@ class ProgressHelper extends Helper
array(604800, 'days', 86400),
);

/**
* Sets the progress bar width.
*
* @param int $size The progress bar size
*/
public function setBarWidth($size)
{
$this->barWidth = (int) $size;
}

/**
* Sets the bar character.
*
* @param string $char A character
*/
public function setBarCharacter($char)
{
$this->barChar = $char;
}

/**
* Sets the empty bar character.
*
* @param string $char A character
*/

This comment has been minimized.

Copy link
@weaverryan

weaverryan Sep 29, 2012

Member

There's some inconsistency with the setters - setEmptyBarCharacter versus setProgressChar (Character -> Char)

This comment has been minimized.

Copy link
@fabpot

fabpot Sep 29, 2012

Author Member

fixed

public function setEmptyBarCharacter($char)
{
$this->emptyBarChar = $char;
}

/**
* Sets the progress bar character.
*
* @param string $char A character
*/
public function setProgressChar($char)
{
$this->progressChar = $char;
}

/**
* Sets the progress bar format.
*
* @param string $format The format
*/
public function setFormat($format)
{
$this->format = $format;
}

/**
* Sets the redraw frequency.
*
* @param int $freq The frequency in seconds
*/
public function setRedrawFrequency($freq)
{
$this->redrawFreq = (int) $freq;
}

/**
* Starts the progress output.
*
* @param OutputInterface $output An Output instance
* @param integer $max Maximum steps
* @param array $options Options for progress helper
*/
public function start(OutputInterface $output, $max = null, array $options = array())
public function start(OutputInterface $output, $max = null)
{
$this->startTime = time();
$this->current = 0;
Expand All @@ -142,25 +188,24 @@ public function start(OutputInterface $output, $max = null, array $options = arr

switch ($output->getVerbosity()) {
case OutputInterface::VERBOSITY_QUIET:
$this->options['format'] = self::FORMAT_QUIET_NOMAX;
$this->format = self::FORMAT_QUIET_NOMAX;
if ($this->max > 0) {
$this->options['format'] = self::FORMAT_QUIET;
$this->format = self::FORMAT_QUIET;
}
break;
case OutputInterface::VERBOSITY_VERBOSE:
$this->options['format'] = self::FORMAT_VERBOSE_NOMAX;
$this->format = self::FORMAT_VERBOSE_NOMAX;
if ($this->max > 0) {
$this->options['format'] = self::FORMAT_VERBOSE;
$this->format = self::FORMAT_VERBOSE;
}
break;
default:
if ($this->max > 0) {
$this->options['format'] = self::FORMAT_NORMAL;
$this->format = self::FORMAT_NORMAL;
}

This comment has been minimized.

Copy link
@weaverryan

weaverryan Sep 29, 2012

Member

If the format is set manually before calling start via the setter, this will override it. So, I think we just need a conditional to only calculate this "default" of the format is null.

This comment has been minimized.

Copy link
@fabpot

fabpot Sep 29, 2012

Author Member

fixed

break;
}

$this->options = array_merge($this->defaultOptions, $options);
$this->inititalize();
}

Expand All @@ -172,11 +217,15 @@ public function start(OutputInterface $output, $max = null, array $options = arr
*/
public function advance($step = 1, $redraw = false)
{
if (null === $this->startTime) {
throw new \LogicException('You must start the progress bar before calling advance().');
}

if ($this->current === 0) {
$redraw = true;
}
$this->current += $step;
if ($redraw || $this->current % $this->options['redrawFreq'] === 0) {
if ($redraw || $this->current % $this->redrawFreq === 0) {
$this->display();
}
}
Expand All @@ -188,21 +237,29 @@ public function advance($step = 1, $redraw = false)
*/
public function display($finish = false)
{
$message = $this->options['format'];
if (null === $this->startTime) {
throw new \LogicException('You must start the progress bar before calling display().');
}

$message = $this->format;
foreach ($this->generate($finish) as $name => $value) {
$message = str_replace("%{$name}%", $value, $message);
}
$this->overwrite($this->output, $message);
}

/**
* Finish the progress output
* Finishes the progress output.
*/
public function finish()
{
if (null === $this->startTime) {
throw new \LogicException('You must start the progress bar before calling finish().');
}

if ($this->startTime !== null) {
if (!$this->max) {
$this->options['barChar'] = $this->options['barCharOriginal'];
$this->barChar = $this->barCharOriginal;
$this->display(true);
}
$this->startTime = null;
Expand All @@ -212,13 +269,13 @@ public function finish()
}

/**
* Initialize the progress helper.
* Initializes the progress helper.
*/
protected function inititalize()
private function inititalize()
{
$this->formatVars = array();
foreach ($this->defaultFormatVars as $var) {
if (strpos($this->options['format'], "%{$var}%") !== false) {
if (strpos($this->format, "%{$var}%") !== false) {
$this->formatVars[$var] = true;
}
}
Expand All @@ -227,8 +284,8 @@ protected function inititalize()
$this->widths['max'] = strlen($this->max);
$this->widths['current'] = $this->widths['max'];
} else {
$this->options['barCharOriginal'] = $this->options['barChar'];
$this->options['barChar'] = $this->options['emptyBarChar'];
$this->barCharOriginal = $this->barChar;
$this->barChar = $this->emptyBarChar;
}
}

Expand All @@ -238,7 +295,7 @@ protected function inititalize()
* @param Boolean $finish Forces the end result
* @return array Array of format vars and values
*/
protected function generate($finish = false)
private function generate($finish = false)
{
$vars = array();
$percent = 0;
Expand All @@ -250,20 +307,20 @@ protected function generate($finish = false)
$completeBars = 0;
$emptyBars = 0;
if ($this->max > 0) {
$completeBars = floor($percent * $this->options['barWidth']);
$completeBars = floor($percent * $this->barWidth);
} else {
if (!$finish) {
$completeBars = floor($this->current % $this->options['barWidth']);
$completeBars = floor($this->current % $this->barWidth);
} else {
$completeBars = $this->options['barWidth'];
$completeBars = $this->barWidth;
}
}

$emptyBars = $this->options['barWidth'] - $completeBars - strlen($this->options['progressChar']);
$bar = str_repeat($this->options['barChar'], $completeBars);
if ($completeBars < $this->options['barWidth']) {
$bar .= $this->options['progressChar'];
$bar .= str_repeat($this->options['emptyBarChar'], $emptyBars);
$emptyBars = $this->barWidth - $completeBars - strlen($this->progressChar);
$bar = str_repeat($this->barChar, $completeBars);
if ($completeBars < $this->barWidth) {
$bar .= $this->progressChar;
$bar .= str_repeat($this->emptyBarChar, $emptyBars);
}

$vars['bar'] = $bar;
Expand Down

0 comments on commit 729e3bf

Please sign in to comment.