Skip to content

Commit b8c2675

Browse files
committed
fix(console): avoid multiple new line when message already ends with a new line
1 parent a107488 commit b8c2675

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/Symfony/Component/Console/Output/ConsoleSectionOutput.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ public function addNewLineOfInputSubmit(): void
168168
*/
169169
protected function doWrite(string $message, bool $newline)
170170
{
171+
// if there is a new line at the end of message, but we do not add a new line, then simulate if without new line and add new line
172+
// a lot of code relies on the newline bool to correctly format the output, it avoid a lot of new logic to handle both cases which are roughly the same
173+
if (!$newline && str_ends_with($message, \PHP_EOL)) {
174+
$message = substr($message, 0, -\strlen(\PHP_EOL));
175+
$newline = true;
176+
}
177+
171178
if (!$this->isDecorated()) {
172179
parent::doWrite($message, $newline);
173180

src/Symfony/Component/Console/Tests/Output/ConsoleSectionOutputTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function testMaxHeightMultipleSections()
158158
$expected .= 'Two'.\PHP_EOL.'Three'.\PHP_EOL.'Four'.\PHP_EOL;
159159

160160
// cause overflow of first section (redraw whole section, without first line)
161-
$firstSection->writeln("Four\nFive\nSix");
161+
$firstSection->writeln('Four'.\PHP_EOL.'Five'.\PHP_EOL.'Six');
162162
$expected .= "\x1b[6A\x1b[0J";
163163
$expected .= 'Four'.\PHP_EOL.'Five'.\PHP_EOL.'Six'.\PHP_EOL;
164164
$expected .= 'Two'.\PHP_EOL.'Three'.\PHP_EOL.'Four'.\PHP_EOL;
@@ -290,4 +290,16 @@ public function testClearSectionContainingQuestion()
290290
rewind($output->getStream());
291291
$this->assertSame('What\'s your favorite super hero?'.\PHP_EOL."\x1b[2A\x1b[0J", stream_get_contents($output->getStream()));
292292
}
293+
294+
public function testWriteWithoutNewLine()
295+
{
296+
$sections = [];
297+
$output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
298+
299+
$output->write('Foo'.\PHP_EOL);
300+
$output->write('Bar');
301+
302+
rewind($output->getStream());
303+
$this->assertEquals(escapeshellcmd('Foo'.\PHP_EOL.'Bar'.\PHP_EOL), escapeshellcmd(stream_get_contents($output->getStream())));
304+
}
293305
}

src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ public function testAskAndClearExpectFullSectionCleared()
212212

213213
rewind($output->getStream());
214214
$this->assertEquals($answer, $givenAnswer);
215-
$this->assertEquals(
215+
$this->assertEquals(escapeshellcmd(
216216
'start'.\PHP_EOL. // write start
217217
'foo'.\PHP_EOL. // write foo
218218
"\x1b[1A\x1b[0Jfoo and bar".\PHP_EOL. // complete line
219-
\PHP_EOL.\PHP_EOL." \033[32mDummy question?\033[39m:".\PHP_EOL.' > '.\PHP_EOL.\PHP_EOL.\PHP_EOL. // question
220-
'foo2'.\PHP_EOL.\PHP_EOL. // write foo2
219+
\PHP_EOL." \033[32mDummy question?\033[39m:".\PHP_EOL.' > '.\PHP_EOL.\PHP_EOL. // question
220+
'foo2'.\PHP_EOL. // write foo2
221221
'bar2'.\PHP_EOL. // write bar
222-
"\033[12A\033[0J", // clear 12 lines (11 output lines and one from the answer input return)
223-
stream_get_contents($output->getStream())
222+
"\033[9A\033[0J"), // clear 9 lines (8 output lines and one from the answer input return)
223+
escapeshellcmd(stream_get_contents($output->getStream()))
224224
);
225225
}
226226
}

0 commit comments

Comments
 (0)
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