From 1f20f726406cc7d8562155277cd17f5c6afa7108 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Mon, 14 Aug 2023 10:15:15 +0200 Subject: [PATCH] fix(console): avoid multiple new line when message already ends with a new line --- .../Console/Output/ConsoleSectionOutput.php | 9 +++++++-- .../Tests/Output/ConsoleSectionOutputTest.php | 14 +++++++++++++- .../Console/Tests/Style/SymfonyStyleTest.php | 10 +++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Console/Output/ConsoleSectionOutput.php b/src/Symfony/Component/Console/Output/ConsoleSectionOutput.php index 3d499bbcc9e37..21c4a44a8eb25 100644 --- a/src/Symfony/Component/Console/Output/ConsoleSectionOutput.php +++ b/src/Symfony/Component/Console/Output/ConsoleSectionOutput.php @@ -119,8 +119,7 @@ public function addContent(string $input, bool $newline = true): int // re-add the line break (that has been removed in the above `explode()` for // - every line that is not the last line // - if $newline is required, also add it to the last line - // - if it's not new line, but input ending with `\PHP_EOL` - if ($i < $count || $newline || str_ends_with($input, \PHP_EOL)) { + if ($i < $count || $newline) { $lineContent .= \PHP_EOL; } @@ -168,6 +167,12 @@ public function addNewLineOfInputSubmit(): void */ protected function doWrite(string $message, bool $newline) { + // Simulate newline behavior for consistent output formatting, avoiding extra logic + if (!$newline && str_ends_with($message, \PHP_EOL)) { + $message = substr($message, 0, -\strlen(\PHP_EOL)); + $newline = true; + } + if (!$this->isDecorated()) { parent::doWrite($message, $newline); diff --git a/src/Symfony/Component/Console/Tests/Output/ConsoleSectionOutputTest.php b/src/Symfony/Component/Console/Tests/Output/ConsoleSectionOutputTest.php index 0a775fd68e4f9..b653b75c1eed2 100644 --- a/src/Symfony/Component/Console/Tests/Output/ConsoleSectionOutputTest.php +++ b/src/Symfony/Component/Console/Tests/Output/ConsoleSectionOutputTest.php @@ -158,7 +158,7 @@ public function testMaxHeightMultipleSections() $expected .= 'Two'.\PHP_EOL.'Three'.\PHP_EOL.'Four'.\PHP_EOL; // cause overflow of first section (redraw whole section, without first line) - $firstSection->writeln("Four\nFive\nSix"); + $firstSection->writeln('Four'.\PHP_EOL.'Five'.\PHP_EOL.'Six'); $expected .= "\x1b[6A\x1b[0J"; $expected .= 'Four'.\PHP_EOL.'Five'.\PHP_EOL.'Six'.\PHP_EOL; $expected .= 'Two'.\PHP_EOL.'Three'.\PHP_EOL.'Four'.\PHP_EOL; @@ -290,4 +290,16 @@ public function testClearSectionContainingQuestion() rewind($output->getStream()); $this->assertSame('What\'s your favorite super hero?'.\PHP_EOL."\x1b[2A\x1b[0J", stream_get_contents($output->getStream())); } + + public function testWriteWithoutNewLine() + { + $sections = []; + $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter()); + + $output->write('Foo'.\PHP_EOL); + $output->write('Bar'); + + rewind($output->getStream()); + $this->assertEquals(escapeshellcmd('Foo'.\PHP_EOL.'Bar'.\PHP_EOL), escapeshellcmd(stream_get_contents($output->getStream()))); + } } diff --git a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php index f053b60f84335..a56dc38706a05 100644 --- a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php +++ b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php @@ -212,15 +212,15 @@ public function testAskAndClearExpectFullSectionCleared() rewind($output->getStream()); $this->assertEquals($answer, $givenAnswer); - $this->assertEquals( + $this->assertEquals(escapeshellcmd( 'start'.\PHP_EOL. // write start 'foo'.\PHP_EOL. // write foo "\x1b[1A\x1b[0Jfoo and bar".\PHP_EOL. // complete line - \PHP_EOL.\PHP_EOL." \033[32mDummy question?\033[39m:".\PHP_EOL.' > '.\PHP_EOL.\PHP_EOL.\PHP_EOL. // question - 'foo2'.\PHP_EOL.\PHP_EOL. // write foo2 + \PHP_EOL." \033[32mDummy question?\033[39m:".\PHP_EOL.' > '.\PHP_EOL.\PHP_EOL. // question + 'foo2'.\PHP_EOL. // write foo2 'bar2'.\PHP_EOL. // write bar - "\033[12A\033[0J", // clear 12 lines (11 output lines and one from the answer input return) - stream_get_contents($output->getStream()) + "\033[9A\033[0J"), // clear 9 lines (8 output lines and one from the answer input return) + escapeshellcmd(stream_get_contents($output->getStream())) ); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy