Skip to content

[Console] Fix progress bar formatting when max is set on start() and some other edge cases #16196

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

Merged
merged 2 commits into from
Oct 11, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[Console] fixed progress bar format on edge cases
  • Loading branch information
fabpot committed Oct 11, 2015
commit e651da4e6a5bbffb51ffb3e937d8e2e0ccf91c30
24 changes: 13 additions & 11 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 All @@ -43,7 +44,6 @@ class ProgressBar
private $formatLineCount;
private $messages;
private $overwrite = true;
private $formatSetByUser = false;

private static $formatters;
private static $formats;
Expand Down Expand Up @@ -73,8 +73,6 @@ public function __construct(OutputInterface $output, $max = 0)
}
}

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

$this->startTime = time();
}

Expand Down Expand Up @@ -311,8 +309,8 @@ public function getProgressCharacter()
*/
public function setFormat($format)
{
$this->formatSetByUser = true;
$this->setFormatInternal($format);
$this->format = null;
$this->internalFormat = $format;
}

/**
Expand All @@ -338,10 +336,6 @@ public function start($max = null)

if (null !== $max) {
$this->setMaxSteps($max);

if (!$this->formatSetByUser) {
$this->setFormatInternal($this->determineBestFormat());
}
}

$this->display();
Expand Down Expand Up @@ -438,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 @@ -472,6 +470,10 @@ public function clear()
return;
}

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

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

Expand All @@ -480,7 +482,7 @@ public function clear()
*
* @param string $format The format
*/
private function setFormatInternal($format)
private function setRealFormat($format)
{
// try to use the _nomax variant if available
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {
Expand Down
34 changes: 30 additions & 4 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,51 @@ public function testAdvanceOverMax()
);
}

public function testFormatWhenMaxInConstructAndInStart()
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());
$maxInConstruct = stream_get_contents($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());
$maxInStart = stream_get_contents($output->getStream());
$this->assertEquals($expected, stream_get_contents($output->getStream()));

$this->assertEquals($maxInStart, $maxInConstruct);
// 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()
Expand Down