Skip to content

Commit e651da4

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

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

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

Lines changed: 13 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,8 @@ public function getProgressCharacter()
311309
*/
312310
public function setFormat($format)
313311
{
314-
$this->formatSetByUser = true;
315-
$this->setFormatInternal($format);
312+
$this->format = null;
313+
$this->internalFormat = $format;
316314
}
317315

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

339337
if (null !== $max) {
340338
$this->setMaxSteps($max);
341-
342-
if (!$this->formatSetByUser) {
343-
$this->setFormatInternal($this->determineBestFormat());
344-
}
345339
}
346340

347341
$this->display();
@@ -438,6 +432,10 @@ public function display()
438432
return;
439433
}
440434

435+
if (null === $this->format) {
436+
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
437+
}
438+
441439
// these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
442440
$self = $this;
443441
$output = $this->output;
@@ -472,6 +470,10 @@ public function clear()
472470
return;
473471
}
474472

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

@@ -480,7 +482,7 @@ public function clear()
480482
*
481483
* @param string $format The format
482484
*/
483-
private function setFormatInternal($format)
485+
private function setRealFormat($format)
484486
{
485487
// try to use the _nomax variant if available
486488
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,51 @@ public function testAdvanceOverMax()
106106
);
107107
}
108108

109-
public function testFormatWhenMaxInConstructAndInStart()
109+
public function testFormat()
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy