Skip to content

[Console] Fix bug with format, when set max count, by start method in progress bar #16149

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 1 commit into from
Closed
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
38 changes: 27 additions & 11 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ProgressBar
private $formatLineCount;
private $messages;
private $overwrite = true;
private $formatSetByUser = false;

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

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

$this->startTime = time();
}
Expand Down Expand Up @@ -311,16 +312,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->formatSetByUser = true;
$this->setFormatInternal($format);
}

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

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

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

$this->display();
Expand Down Expand Up @@ -479,6 +476,25 @@ public function clear()
$this->overwrite(str_repeat("\n", $this->formatLineCount));
}

/**
* Sets the progress bar format.
*
* @param string $format The format
*/
private function setFormatInternal($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
21 changes: 21 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,27 @@ public function testAdvanceOverMax()
);
}

public function testFormatWhenMaxInConstructAndInStart()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->start();
$bar->advance(10);
$bar->finish();

rewind($output->getStream());
$maxInConstruct = stream_get_contents($output->getStream());

$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($maxInStart, $maxInConstruct);
}

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