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

Skip to content

Commit cd97d41

Browse files
committed
bug #16196 [Console] Fix progress bar formatting when max is set on start() and some other edge cases (vsychov, fabpot)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix progress bar formatting when max is set on start() and some other edge cases | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Besides what #16149 fixed, it also fixes the case where `setFormat()` is called before `start()`. From #16149 When I set max count, by call "ProgressBar::start()", I got invalid progress bar format. Example code: ```php $bar = new ProgressBar($output, 100); $bar->start(); $bar->advance(100); $bar->finish(); ``` Output: 100/100 [============================] 100% Example code: ```php $bar = new ProgressBar($output); $bar->start(100); $bar->advance(100); $bar->finish(); ``` Output: 100 [============================] Commits ------- e651da4 [Console] fixed progress bar format on edge cases 3cbfa63 fix bug with set max count, by start method in progress bar
2 parents e49fa27 + e651da4 commit cd97d41

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class ProgressBar
2727
private $barChar;
2828
private $emptyBarChar = '-';
2929
private $progressChar = '>';
30-
private $format = null;
30+
private $format;
31+
private $internalFormat;
3132
private $redrawFreq = 1;
3233

3334
/**
@@ -72,8 +73,6 @@ public function __construct(OutputInterface $output, $max = 0)
7273
}
7374
}
7475

75-
$this->setFormat($this->determineBestFormat());
76-
7776
$this->startTime = time();
7877
}
7978

@@ -310,16 +309,8 @@ public function getProgressCharacter()
310309
*/
311310
public function setFormat($format)
312311
{
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");
312+
$this->format = null;
313+
$this->internalFormat = $format;
323314
}
324315

325316
/**
@@ -441,6 +432,10 @@ public function display()
441432
return;
442433
}
443434

435+
if (null === $this->format) {
436+
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
437+
}
438+
444439
// these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
445440
$self = $this;
446441
$output = $this->output;
@@ -475,9 +470,32 @@ public function clear()
475470
return;
476471
}
477472

473+
if (null === $this->format) {
474+
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
475+
}
476+
478477
$this->overwrite(str_repeat("\n", $this->formatLineCount));
479478
}
480479

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

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

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

109+
public function testFormat()
110+
{
111+
$expected =
112+
$this->generateOutput(' 0/10 [>---------------------------] 0%').
113+
$this->generateOutput(' 10/10 [============================] 100%').
114+
$this->generateOutput(' 10/10 [============================] 100%')
115+
;
116+
117+
// max in construct, no format
118+
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
119+
$bar->start();
120+
$bar->advance(10);
121+
$bar->finish();
122+
123+
rewind($output->getStream());
124+
$this->assertEquals($expected, stream_get_contents($output->getStream()));
125+
126+
// max in start, no format
127+
$bar = new ProgressBar($output = $this->getOutputStream());
128+
$bar->start(10);
129+
$bar->advance(10);
130+
$bar->finish();
131+
132+
rewind($output->getStream());
133+
$this->assertEquals($expected, stream_get_contents($output->getStream()));
134+
135+
// max in construct, explicit format before
136+
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
137+
$bar->setFormat('normal');
138+
$bar->start();
139+
$bar->advance(10);
140+
$bar->finish();
141+
142+
rewind($output->getStream());
143+
$this->assertEquals($expected, stream_get_contents($output->getStream()));
144+
145+
// max in start, explicit format before
146+
$bar = new ProgressBar($output = $this->getOutputStream());
147+
$bar->setFormat('normal');
148+
$bar->start(10);
149+
$bar->advance(10);
150+
$bar->finish();
151+
152+
rewind($output->getStream());
153+
$this->assertEquals($expected, stream_get_contents($output->getStream()));
154+
}
155+
109156
public function testCustomizations()
110157
{
111158
$bar = new ProgressBar($output = $this->getOutputStream(), 10);

0 commit comments

Comments
 (0)