diff --git a/Event/ConsoleEvent.php b/Event/ConsoleEvent.php index 89ab64559..4e00f7c73 100644 --- a/Event/ConsoleEvent.php +++ b/Event/ConsoleEvent.php @@ -28,7 +28,7 @@ class ConsoleEvent extends Event private $input; private $output; - public function __construct(Command $command = null, InputInterface $input, OutputInterface $output) + public function __construct(?Command $command, InputInterface $input, OutputInterface $output) { $this->command = $command; $this->input = $input; diff --git a/Helper/Helper.php b/Helper/Helper.php index 881b4dc4f..cfcbbd9a1 100644 --- a/Helper/Helper.php +++ b/Helper/Helper.php @@ -42,7 +42,7 @@ public function getHelperSet() /** * Returns the length of a string, using mb_strwidth if it is available. * - * @deprecated since 5.3 + * @deprecated since Symfony 5.3 * * @return int The length of the string */ @@ -154,7 +154,7 @@ public static function formatMemory(int $memory) } /** - * @deprecated since 5.3 + * @deprecated since Symfony 5.3 */ public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string) { @@ -170,7 +170,7 @@ public static function removeDecoration(OutputFormatterInterface $formatter, ?st // remove <...> formatting $string = $formatter->format($string ?? ''); // remove already formatted characters - $string = preg_replace("/\033\[[^m]*m/", '', $string); + $string = preg_replace("/\033\[[^m]*m/", '', $string ?? ''); $formatter->setDecorated($isDecorated); return $string; diff --git a/Helper/HelperSet.php b/Helper/HelperSet.php index 5c08a7606..679dceab1 100644 --- a/Helper/HelperSet.php +++ b/Helper/HelperSet.php @@ -89,8 +89,9 @@ public function getCommand() } /** - * @return Helper[] + * @return \Traversable */ + #[\ReturnTypeWillChange] public function getIterator() { return new \ArrayIterator($this->helpers); 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()) + ); + } }