8000 fix bug with set max count, by start method in progress bar · symfony/symfony@3cbfa63 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cbfa63

Browse files
vsychovfabpot
authored andcommitted
fix bug with set max count, by start method in progress bar
1 parent c98ce2a commit 3cbfa63

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ProgressBar
4343
private $formatLineCount;
4444
private $messages;
4545
private $overwrite = true;
46+
private $formatSetByUser = false;
4647

4748
private static $formatters;
4849
private static $formats;
@@ -72,7 +73,7 @@ public function __construct(OutputInterface $output, $max = 0)
7273
}
7374
}
7475

75-
$this->setFormat($this->determineBestFormat());
76+
$this->setFormatInternal($this->determineBestFormat());
7677

7778
$this->startTime = time();
7879
}
@@ -310,16 +311,8 @@ public function getProgressCharacter()
310311
*/
311312
public function setFormat($format)
312313
{
313-
// try to use the _nomax variant if available
314-
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {
315-
$this->format = self::getFormatDefinition($format.'_nomax');
316-
} elseif (null !== self::getFormatDefinition($format)) {
317-
$this->format = self::getFormatDefinition($format);
318-
} else {
319-
$this->format = $format;
320-
}
321-
322-
$this->formatLineCount = substr_count($this->format, "\n");
314+
$this->formatSetByUser = true;
315+
$this->setFormatInternal($format);
323316
}
324317

325318
/**
@@ -345,6 +338,10 @@ public function start($max = null)
345338

346339
if (null !== $max) {
347340
$this->setMaxSteps($max);
341+
342+
if (!$this->formatSetByUser) {
343+
$this->setFormatInternal($this->determineBestFormat());
344+
}
348345
}
349346

350347
$this->display();
@@ -478,6 +475,25 @@ public function clear()
478475
$this->overwrite(str_repeat("\n", $this->formatLineCount));
479476
}
480477

478+
/**
479+
* Sets the progress bar format.
480+
*
481+
* @param string $format The format
482+
*/
483+
private function setFormatInternal($format)
484+
{
485+
// try to use the _nomax variant if available
486+
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {
487+
$this->format = self::getFormatDefinition($format.'_nomax');
488+
} elseif (null !== self::getFormatDefinition($format)) {
489+
$this->format = self::getFormatDefinition($format);
490+
} else {
491+
$this->format = $format;
492+
}
493+
494+
$this->formatLineCount = substr_count($this->format, "\n");
495+
}
496+
481497
/**
482498
* Sets the progress bar maximal steps.
483499
*

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,27 @@ public function testAdvanceOverMax()
106106
);
107107
}
108108

109+
public function testFormatWhenMaxInConstructAndInStart()
110+
{
111+
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
112+
$bar->start();
113+
$bar->advance(10);
114+
$bar->finish();
115+
116+
rewind($output->getStream());
117+
$maxInConstruct = stream_get_contents($output->getStream());
118+
119+
$bar = new ProgressBar($output = $this->getOutputStream());
120+
$bar->start(10);
121+
$bar->advance(10);
122+
$bar->finish();
123+
124+
rewind($output->getStream());
125+
$maxInStart = stream_get_contents($output->getStream());
126+
127+
$this->assertEquals($maxInStart, $maxInConstruct);
128+
}
129+
109130
public function testCustomizations()
110131
{
111132
$bar = new ProgressBar($output = $this->getOutputStream(), 10);

0 commit comments

Comments
 (0)
0