diff --git a/Helper/ProgressBar.php b/Helper/ProgressBar.php index 91fba2b58..1c03a1d96 100644 --- a/Helper/ProgressBar.php +++ b/Helper/ProgressBar.php @@ -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); diff --git a/Tests/Helper/ProgressBarTest.php b/Tests/Helper/ProgressBarTest.php index e424a29df..ef5f06222 100644 --- a/Tests/Helper/ProgressBarTest.php +++ b/Tests/Helper/ProgressBarTest.php @@ -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()) ); @@ -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() @@ -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()) + ); + } }