diff --git a/Helper/ProgressBar.php b/Helper/ProgressBar.php index b1fb213b6..ddc5870aa 100644 --- a/Helper/ProgressBar.php +++ b/Helper/ProgressBar.php @@ -474,10 +474,13 @@ private function overwrite(string $message): void } $this->output->clear($lineCount); } else { - for ($i = 0; $i < $this->formatLineCount; ++$i) { - $this->cursor->moveToColumn(1); - $this->cursor->clearLine(); - $this->cursor->moveUp(); + if ('' !== $this->previousMessage) { + // only clear upper lines when last call was not a clear + 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 ef5f06222..17401b887 100644 --- a/Tests/Helper/ProgressBarTest.php +++ b/Tests/Helper/ProgressBarTest.php @@ -812,8 +812,10 @@ public function testMultilineFormat() $this->assertEquals( ">---------------------------\nfoobar". $this->generateOutput("=========>------------------\nfoobar"). - "\x1B[1G\x1B[2K\x1B[1A\x1B[1G\x1B[2K". - $this->generateOutput("============================\nfoobar"), + "\x1B[1G\x1B[2K\x1B[1A". + $this->generateOutput(''). + $this->generateOutput('============================'). + "\nfoobar", stream_get_contents($output->getStream()) ); } @@ -1124,4 +1126,29 @@ public function testMultiLineFormatIsFullyCleared() stream_get_contents($output->getStream()) ); } + + public function testMultiLineFormatIsFullyCorrectlyWithManuallyCleanup() + { + ProgressBar::setFormatDefinition('normal_nomax', "[%bar%]\n%message%"); + $bar = new ProgressBar($output = $this->getOutputStream()); + $bar->setMessage('Processing "foobar"...'); + $bar->start(); + $bar->clear(); + $output->writeln('Foo!'); + $bar->display(); + $bar->finish(); + + rewind($output->getStream()); + $this->assertEquals( + "[>---------------------------]\n". + 'Processing "foobar"...'. + "\x1B[1G\x1B[2K\x1B[1A". + $this->generateOutput(''). + 'Foo!'.\PHP_EOL. + $this->generateOutput('[--->------------------------]'). + "\nProcessing \"foobar\"...". + $this->generateOutput("[----->----------------------]\nProcessing \"foobar\"..."), + stream_get_contents($output->getStream()) + ); + } }