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

Skip to content

Commit 5c682e8

Browse files
committed
[Console] fixed progress bar format on edge cases
1 parent 3cbfa63 commit 5c682e8

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

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

Lines changed: 16 additions & 11 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
/**
@@ -43,7 +44,6 @@ class ProgressBar
4344
private $formatLineCount;
4445
private $messages;
4546
private $overwrite = true;
46-
private $formatSetByUser = false;
4747

4848
private static $formatters;
4949
private static $formats;
@@ -73,8 +73,6 @@ public function __construct(OutputInterface $output, $max = 0)
7373
}
7474
}
7575

76-
$this->setFormatInternal($this->determineBestFormat());
77-
7876
$this->startTime = time();
7977
}
8078

@@ -311,8 +309,11 @@ public function getProgressCharacter()
311309
*/
312310
public function setFormat($format)
313311
{
314-
$this->formatSetByUser = true;
315-
$this->setFormatInternal($format);
312+
if (null !== $this->format) {
313+
$this->format = null;
314+
}
315+
316+
$this->internalFormat = $format;
316317
}
317318

318319
/**
@@ -338,10 +339,6 @@ public function start($max = null)
338339

339340
if (null !== $max) {
340341
$this->setMaxSteps($max);
341-
342-
if (!$this->formatSetByUser) {
343-
$this->setFormatInternal($this->determineBestFormat());
344-
}
345342
}
346343

347344
$this->display();
@@ -438,6 +435,10 @@ public function display()
438435
return;
439436
}
440437

438+
if (null === $this->format) {
439+
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
440+
}
441+
441442
// these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
442443
$self = $this;
443444
$output = $this->output;
@@ -472,6 +473,10 @@ public function clear()
472473
return;
473474
}
474475

476+
if (null === $this->format) {
477+
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
478+
}
479+
475480
$this->overwrite(str_repeat("\n", $this->formatLineCount));
476481
}
477482

@@ -480,7 +485,7 @@ public function clear()
480485
*
481486
* @param string $format The format
482487
*/
483-
private function setFormatInternal($format)
488+
private function setRealFormat($format)
484489
{
485490
// try to use the _nomax variant if available
486491
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,49 @@ public function testAdvanceOverMax()
108108

109109
public function testFormatWhenMaxInConstructAndInStart()
110110
{
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
111118
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
112119
$bar->start();
113120
$bar->advance(10);
114121
$bar->finish();
115122

116123
rewind($output->getStream());
117-
$maxInConstruct = stream_get_contents($output->getStream());
124+
$this->assertEquals($expected, stream_get_contents($output->getStream()));
118125

126+
// max in start, no format
119127
$bar = new ProgressBar($output = $this->getOutputStream());
120128
$bar->start(10);
121129
$bar->advance(10);
122130
$bar->finish();
123131

124132
rewind($output->getStream());
125-
$maxInStart = stream_get_contents($output->getStream());
133+
$this->assertEquals($expected, stream_get_contents($output->getStream()));
126134

127-
$this->assertEquals($maxInStart, $maxInConstruct);
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()));
128154
}
129155

130156
public function testCustomizations()

0 commit comments

Comments
 (0)