10000 Fix ProgressBar to correctly clear multi-line formats by rtek · Pull Request #42685 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix ProgressBar to correctly clear multi-line formats #42685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2021
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 8000
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,10 @@ private function overwrite(string $message): void
}
$this->output->clear($lineCount);
} else {
if ($this->formatLineCount > 0) {
$this->cursor->moveUp($this->formatLineCount);
for ($i = 0; $i < $this->formatLineCount; ++$i) {
$this->cursor->moveToColumn(1);
$this->cursor->clearLine();
$this->cursor->moveUp();
}

$this->cursor->moveToColumn(1);
Expand Down
33 changes: 31 additions & 2 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ public function testMultilineFormat()
$this->assertEquals(
">---------------------------\nfoobar".
$this->generateOutput("=========>------------------\nfoobar").
"\x1B[1A\x1B[1G\x1B[2K".
"\x1B[1G\x1B[2K\x1B[1A\x1B[1G\x1B[2K".
$this->generateOutput("============================\nfoobar"),
stream_get_contents($output->getStream())
);
Expand Down Expand Up @@ -983,7 +983,7 @@ protected function generateOutput($expected)
{
$count = substr_count($expected, "\n");

return ($count ? sprintf("\x1B[%dA\x1B[1G\x1b[2K", $count) : "\x1B[1G\x1B[2K").$expected;
return ($count ? str_repeat("\x1B[1G\x1b[2K\x1B[1A", $count) : '')."\x1B[1G\x1B[2K".$expected;
}

public function testBarWidthWithMultilineFormat()
Expand Down Expand Up @@ -1095,4 +1095,33 @@ public function testNoWriteWhenMessageIsSame()
stream_get_contents($output->getStream())
);
}

public function testMultiLineFormatIsFullyCleared()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 3);
$bar->setFormat("%current%/%max%\n%message%\nFoo");

$bar->setMessage('1234567890');
$bar->start();
$bar->display();

$bar->setMessage('ABC');
$bar->advance();
$bar->display();

$bar->setMessage('A');
$bar->advance();
$bar->display();

$bar->finish();

rewind($output->getStream());
$this->assertEquals(
"0/3\n1234567890\nFoo".
$this->generateOutput("1/3\nABC\nFoo").
$this->generateOutput("2/3\nA\nFoo").
$this->generateOutput("3/3\nA\nFoo"),
stream_get_contents($output->getStream())
);
}
}
0