Skip to content

Commit 1f20f72

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

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ public function addContent(string $input, bool $newline = true): int
119119
// re-add the line break (that has been removed in the above `explode()` for
120120
// - every line that is not the last line
121121
// - if $newline is required, also add it to the last line
122-
// - if it's not new line, but input ending with `\PHP_EOL`
123-
if ($i < $count || $newline || str_ends_with($input, \PHP_EOL)) {
122+
if ($i < $count || $newline) {
124123
$lineContent .= \PHP_EOL;
125124
}
126125

@@ -168,6 +167,12 @@ public function addNewLineOfInputSubmit(): void
168167
*/
169168
protected function doWrite(string $message, bool $newline)
170169
{
170+
// Simulate newline behavior for consistent output formatting, avoiding extra logic
171+
if (!$newline && str_ends_with($message, \PHP_EOL)) {
172+
$message = substr($message, 0, -\strlen(\PHP_EOL));
173+
$newline = true;
174+
}
175+
171176
if (!$this->isDecorated()) {
172177
parent::doWrite($message, $newline);
173178

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