Skip to content

Commit

Permalink
Fix ProgressBar to correctly clear multi-line formats
Browse files Browse the repository at this point in the history
  • Loading branch information
rtek committed Aug 22, 2021
1 parent 45daf16 commit 2be7b5c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Helper/ProgressBar.php
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 Tests/Helper/ProgressBarTest.php
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 comments on commit 2be7b5c

Please sign in to comment.