Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,21 @@ private function overwrite(string $message): void
if ($this->output instanceof ConsoleSectionOutput) {
$messageLines = explode("\n", $this->previousMessage);
$lineCount = \count($messageLines);

$lastLineWithoutDecoration = Helper::removeDecoration($this->output->getFormatter(), end($messageLines) ?? '');

// When the last previous line is empty (without formatting) it is already cleared by the section output, so we don't need to clear it again
if ('' === $lastLineWithoutDecoration) {
--$lineCount;
}

foreach ($messageLines as $messageLine) {
$messageLineLength = Helper::width(Helper::removeDecoration($this->output->getFormatter(), $messageLine));
if ($messageLineLength > $this->terminal->getWidth()) {
$lineCount += floor($messageLineLength / $this->terminal->getWidth());
}
}

$this->output->clear($lineCount);
} else {
$lineCount = substr_count($this->previousMessage, "\n");
Expand Down
75 changes: 75 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,81 @@ public function testOverwriteWithSectionOutput()
);
}

public function testOverwriteWithSectionOutputAndEol()
{
$sections = [];
$stream = $this->getOutputStream(true);
$output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter());

$bar = new ProgressBar($output, 50, 0);
$bar->setFormat('[%bar%] %percent:3s%%' . PHP_EOL . '%message%' . PHP_EOL);
$bar->setMessage('');
$bar->start();
$bar->display();
$bar->setMessage('Doing something...');
$bar->advance();
$bar->setMessage('Doing something foo...');
$bar->advance();

rewind($output->getStream());
$this->assertEquals(escapeshellcmd(
'[>---------------------------] 0%'.\PHP_EOL.\PHP_EOL.
"\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL. 'Doing something...' . \PHP_EOL .
"\x1b[2A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL. 'Doing something foo...' . \PHP_EOL),
escapeshellcmd(stream_get_contents($output->getStream()))
);
}

public function testOverwriteWithSectionOutputAndEolWithEmptyMessage()
{
$sections = [];
$stream = $this->getOutputStream(true);
$output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter());

$bar = new ProgressBar($output, 50, 0);
$bar->setFormat('[%bar%] %percent:3s%%' . PHP_EOL . '%message%');
$bar->setMessage('Start');
$bar->start();
$bar->display();
$bar->setMessage('');
$bar->advance();
$bar->setMessage('Doing something...');
$bar->advance();

rewind($output->getStream());
$this->assertEquals(escapeshellcmd(
'[>---------------------------] 0%'.\PHP_EOL.'Start'.\PHP_EOL.
"\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL .
"\x1b[1A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL. 'Doing something...' . \PHP_EOL),
escapeshellcmd(stream_get_contents($output->getStream()))
);
}

public function testOverwriteWithSectionOutputAndEolWithEmptyMessageComment()
{
$sections = [];
$stream = $this->getOutputStream(true);
$output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter());

$bar = new ProgressBar($output, 50, 0);
$bar->setFormat('[%bar%] %percent:3s%%' . PHP_EOL . '<comment>%message%</comment>');
$bar->setMessage('Start');
$bar->start();
$bar->display();
$bar->setMessage('');
$bar->advance();
$bar->setMessage('Doing something...');
$bar->advance();

rewind($output->getStream());
$this->assertEquals(escapeshellcmd(
'[>---------------------------] 0%'.\PHP_EOL."\x1b[33mStart\x1b[39m".\PHP_EOL.
"\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL .
"\x1b[1A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL. "\x1b[33mDoing something...\x1b[39m" . \PHP_EOL),
escapeshellcmd(stream_get_contents($output->getStream()))
);
}

public function testOverwriteWithAnsiSectionOutput()
{
// output has 43 visible characters plus 2 invisible ANSI characters
Expand Down
Loading