Skip to content

[Console] Section always writes with a newline #37304

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

Closed
kmadejski opened this issue Jun 16, 2020 · 12 comments
Closed

[Console] Section always writes with a newline #37304

kmadejski opened this issue Jun 16, 2020 · 12 comments

Comments

@kmadejski
Copy link
Contributor

kmadejski commented Jun 16, 2020

Symfony version(s) affected: 4.1.0 and higher

Description
ConsoleSectionOutput always writes with a newline even if explicitly set to false, eg.:
$section->write('foo', false); since isDecorated() always returns true by default. Output behaves correctly if $section->setDecorated(false); but all decoration is automatically gone which should not be the case.

How to reproduce

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestCommand extends Command
{
    protected function configure(): void
    {
        $this->setName('test-command');
        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        /** @var \Symfony\Component\Console\Output\ConsoleOutputInterface $output */
        $section = $output->section();

        for ($i = 0; $i < 100; $i++) {
            $section->write('Element ' . (string) $i, false);
        }

        return 0;
    }
}

Possible Solution
$newline argument value should be taken into account in this method: https://github.com/symfony/console/blob/v4.1.0/Output/ConsoleSectionOutput.php#L97

Once confirmed I'm more than happy to create a PR that fixes it 🙂

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

@kmadejski
Copy link
Contributor Author

Judging from the code It seems that the issue is still present. I'll take a deeper look later today.

@carsonbot carsonbot removed the Stalled label Mar 4, 2021
@DigiLive
Copy link

I can confirm this behavior still exists at symfony/console 5.2

@chalasr
Copy link
Member

chalasr commented Apr 1, 2021

PR welcome!

@arnegroskurth
Copy link
Contributor

arnegroskurth commented May 20, 2021

I'm currently working on the section output and am likely able to fix/change this accordingly: #41266

However, the docs currently define the behaviour to kind of be like this: A new line is appended automatically when displaying information in a section. (https://symfony.com/doc/current/console.html#output-sections) Can we change this behaviour anyway and still consider it a fix or are we crossing the line to a BC here?

@chalasr

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

@chapterjason
Copy link
Contributor

I use a BufferedOutput as workaround like this:

         /** @var \Symfony\Component\Console\Output\ConsoleOutputInterface $output */
        $section = $output->section();
        $buffer = new BufferedOutput();            

        for ($i = 0; $i < 100; $i++) {
            $buffer->write('Element ' . (string) $i, false);

            $bufferedData = $buffer->fetch();
            $section->overwrite($bufferedData);
            $buffer->write($bufferedData);
        }

@carsonbot carsonbot removed the Stalled label Dec 14, 2021
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@chalasr
Copy link
Member

chalasr commented Jun 21, 2022

PR welcome on the lowest branch where the bug exists (with test case please!)

@carsonbot carsonbot removed the Stalled label Jun 21, 2022
bhujagendra-ishaya added a commit to TheBrightPath/symfony that referenced this issue Jun 29, 2022
bhujagendra-ishaya added a commit to TheBrightPath/symfony that referenced this issue Jun 29, 2022
bhujagendra-ishaya added a commit to TheBrightPath/symfony that referenced this issue Jun 29, 2022
bhujagendra-ishaya added a commit to TheBrightPath/symfony that referenced this issue Jun 30, 2022
bhujagendra-ishaya added a commit to TheBrightPath/symfony that referenced this issue Jun 30, 2022
Add PHPUnit tests to check write() vs. writeln() in sections
@fabpot fabpot closed this as completed in 92d9382 Jul 8, 2022
fabpot added a commit that referenced this issue Jul 8, 2022
…agendra-ishaya)

This PR was submitted for the 4.4 branch but it was merged into the 6.2 branch instead.

Discussion
----------

[Console] Fix console section output without newline

| Q             | A
| ------------- | ---
| Branch       | 4.4
| Bug fix      | yes
| New feature  | no
| Deprecations | no
| Tickets       | Fix #37304
| License       | MIT
| Doc PR        | n/a

As described in #37304, when writing to a output section, a newline would automatically be added, inrespective wether using `write()` or `writeln()`. This PR fixes said behaviour. Lines can now be appended to.

Commits
-------

92d9382 [Console] Fix forced linebreak in output sections (fixes #37304)
@bhujagendra-ishaya
Copy link
Contributor

If you stumble upon this issue, please note that while this was introduced in 4.1 and considered a bug, the patch was considered a new feature and hence applied only to 6.2 (refer to the conversation of #46812 for further details).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants