Skip to content

Commit eca495f

Browse files
bug #51378 [Console] avoid multiple new line when message already ends with a new line in section output (joelwurtz)
This PR was merged into the 6.3 branch. Discussion ---------- [Console] avoid multiple new line when message already ends with a new line in section output | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | symfony/symfony#50382 | License | MIT | Doc PR | When we write to the section with a new line in the message it ends up adding a double new line, it can be easily seen when we set a section as a console handler for the logs, each line of log will have a double new line ending adding a lot of noise. I began to fix the implementation to correctly handle this case, but with all the logic of the max height and other stuff it appears to be way more complicated. Simulating this case by removing the new line at the end of the message and recalling the write function with the newline parameter to true was way easier and avoid too many headcache when looking at the code. Should fix symfony/symfony#50382 Commits ------- 1f20f72640 fix(console): avoid multiple new line when message already ends with a new line
2 parents baad2fa + f3bf447 commit eca495f

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

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

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
}

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)