Skip to content

[Console] Progress bar clear/display remove too many lines #47987

Closed
@cedric-anne

Description

@cedric-anne

Symfony version(s) affected

5.4.14

Description

When using documented logic to display a message in the middle of a progress bar iteration, Symfony\Component\Console\Helper\ProgressBar::clear() / Symfony\Component\Console\Helper\ProgressBar::display() will remove too many lines if progress bar format contains multiple lines.

How to reproduce

// First, run "composer require symfony/console:^5.4"
// Then, execute this file:

<?php

require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

$command = new class() extends Command {
    protected static $defaultName = 'Test';

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Progress bar having only one line:');
        ProgressBar::setFormatDefinition('normal_nomax', "[%bar%]");
        $progressBar = new ProgressBar($output);
        $progressBar->setMessage('Processing "foobar"...');
        $progressBar->start();
        $progressBar->clear();
        $output->writeln('Foo!');
        $progressBar->display();
        $progressBar->finish();
        $output->write(PHP_EOL);

        $output->writeln('=-=-=-=');

        $output->writeln('Progress bar having two lines:');
        ProgressBar::setFormatDefinition('normal_nomax', "[%bar%]\n%message%");
        $progressBar = new ProgressBar($output);
        $progressBar->setMessage('Processing "foobar"...');
        $progressBar->start();
        $progressBar->clear();
        $output->writeln('Foo!');
        $progressBar->display();
        $progressBar->finish();
        $output->write(PHP_EOL);

        return self::SUCCESS;
    }
};

$app = new Application();
$app->add($command);
$app->setDefaultCommand($command->getName(), true);
$app->run();

Output will be:

Progress bar having only one line:
Foo!
[----->----------------------]
=-=-=-=
Progress bar having two lines:
[----->----------------------]
Processing "foobar"...

but is expected to be:

Progress bar having only one line:
Foo!
[----->----------------------]
=-=-=-=
Progress bar having two lines:
Foo!
[----->----------------------]
Processing "foobar"...

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions