Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] avoid multiple new line when message already ends with a new line in section output #51378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(console): avoid multiple new line when message already ends with …
…a new line
  • Loading branch information
joelwurtz committed Aug 16, 2023
commit 1f20f726406cc7d8562155277cd17f5c6afa7108
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)) {
joelwurtz marked this conversation as resolved.
Show resolved Hide resolved
$message = substr($message, 0, -\strlen(\PHP_EOL));
$newline = true;
}

if (!$this->isDecorated()) {
parent::doWrite($message, $newline);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
joelwurtz marked this conversation as resolved.
Show resolved Hide resolved
$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;
Expand Down Expand Up @@ -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())));
}
}
10 changes: 5 additions & 5 deletions 10 src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.