Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
44 changes: 31 additions & 13 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class ProgressBar
private $barChar;
private $emptyBarChar = '-';
private $progressChar = '>';
private $format = null;
private $format;
private $internalFormat;
private $redrawFreq = 1;

/**
Expand Down Expand Up @@ -72,8 +73,6 @@ public function __construct(OutputInterface $output, $max = 0)
}
}

$this->setFormat($this->determineBestFormat());

$this->startTime = time();
}

Expand Down Expand Up @@ -310,16 +309,8 @@ public function getProgressCharacter()
*/
public function setFormat($format)
{
// try to use the _nomax variant if available
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {
$this->format = self::getFormatDefinition($format.'_nomax');
} elseif (null !== self::getFormatDefinition($format)) {
$this->format = self::getFormatDefinition($format);
} else {
$this->format = $format;
}

$this->formatLineCount = substr_count($this->format, "\n");
$this->format = null;
$this->internalFormat = $format;
}

/**
Expand Down Expand Up @@ -441,6 +432,10 @@ public function display()
return;
}

if (null === $this->format) {
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
}

// these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
$self = $this;
$output = $this->output;
Expand Down Expand Up @@ -475,9 +470,32 @@ public function clear()
return;
}

if (null === $this->format) {
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
}

$this->overwrite(str_repeat("\n", $this->formatLineCount));
}

/**
* Sets the progress bar format.
*
* @param string $format The format
*/
private function setRealFormat($format)
{
// try to use the _nomax variant if available
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {
$this->format = self::getFormatDefinition($format.'_nomax');
} elseif (null !== self::getFormatDefinition($format)) {
$this->format = self::getFormatDefinition($format);
} else {
$this->format = $format;
}

$this->formatLineCount = substr_count($this->format, "\n");
}

/**
* Sets the progress bar maximal steps.
*
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,53 @@ public function testAdvanceOverMax()
);
}

public function testFormat()
{
$expected =
$this->generateOutput(' 0/10 [>---------------------------] 0%').
$this->generateOutput(' 10/10 [============================] 100%').
$this->generateOutput(' 10/10 [============================] 100%')
;

// max in construct, no format
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->start();
$bar->advance(10);
$bar->finish();

rewind($output->getStream());
$this->assertEquals($expected, stream_get_contents($output->getStream()));

// max in start, no format
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(10);
$bar->advance(10);
$bar->finish();

rewind($output->getStream());
$this->assertEquals($expected, stream_get_contents($output->getStream()));

// max in construct, explicit format before
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->setFormat('normal');
$bar->start();
$bar->advance(10);
$bar->finish();

rewind($output->getStream());
$this->assertEquals($expected, stream_get_contents($output->getStream()));

// max in start, explicit format before
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->setFormat('normal');
$bar->start(10);
$bar->advance(10);
$bar->finish();

rewind($output->getStream());
$this->assertEquals($expected, stream_get_contents($output->getStream()));
}

public function testCustomizations()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
Expand Down