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

Skip to content

Commit ad6661e

Browse files
committed
fix(console): fix progress bar messing output in section when there is an EOL
1 parent 0266d3c commit ad6661e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ private function overwrite(string $message): void
486486
if ($this->output instanceof ConsoleSectionOutput) {
487487
$messageLines = explode("\n", $this->previousMessage);
488488
$lineCount = \count($messageLines);
489+
490+
// When the previous line was ending with a newline it is already clear by the section output, so we don't need to clear it again
491+
if (str_ends_with($this->previousMessage, \PHP_EOL)) {
492+
--$lineCount;
493+
}
494+
489495
foreach ($messageLines as $messageLine) {
490496
$messageLineLength = Helper::width(Helper::removeDecoration($this->output->getFormatter(), $messageLine));
491497
if ($messageLineLength > $this->terminal->getWidth()) {

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,56 @@ public function testOverwriteWithSectionOutput()
416416
);
417417
}
418418

419+
public function testOverwriteWithSectionOutputAndEol()
420+
{
421+
$sections = [];
422+
$stream = $this->getOutputStream(true);
423+
$output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter());
424+
425+
$bar = new ProgressBar($output, 50, 0);
426+
$bar->setFormat('[%bar%] %percent:3s%%' . PHP_EOL . '%message%' . PHP_EOL);
427+
$bar->setMessage('');
428+
$bar->start();
429+
$bar->display();
430+
$bar->setMessage('Doing something...');
431+
$bar->advance();
432+
$bar->setMessage('Doing something foo...');
433+
$bar->advance();
434+
435+
rewind($output->getStream());
436+
$this->assertEquals(escapeshellcmd(
437+
'[>---------------------------] 0%'.\PHP_EOL.\PHP_EOL.
438+
"\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL. 'Doing something...' . \PHP_EOL .
439+
"\x1b[2A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL. 'Doing something foo...' . \PHP_EOL),
440+
escapeshellcmd(stream_get_contents($output->getStream()))
441+
);
442+
}
443+
444+
public function testOverwriteWithSectionOutputAndEolWithEmptyMessage()
445+
{
446+
$sections = [];
447+
$stream = $this->getOutputStream(true);
448+
$output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter());
449+
450+
$bar = new ProgressBar($output, 50, 0);
451+
$bar->setFormat('[%bar%] %percent:3s%%' . PHP_EOL . '%message%');
452+
$bar->setMessage('Start');
453+
$bar->start();
454+
$bar->display();
455+
$bar->setMessage('');
456+
$bar->advance();
457+
$bar->setMessage('Doing something...');
458+
$bar->advance();
459+
460+
rewind($output->getStream());
461+
$this->assertEquals(escapeshellcmd(
462+
'[>---------------------------] 0%'.\PHP_EOL.'Start'.\PHP_EOL.
463+
"\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL .
464+
"\x1b[1A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL. 'Doing something...' . \PHP_EOL),
465+
escapeshellcmd(stream_get_contents($output->getStream()))
466+
);
467+
}
468+
419469
public function testOverwriteWithAnsiSectionOutput()
420470
{
421471
// output has 43 visible characters plus 2 invisible ANSI characters

0 commit comments

Comments
 (0)