Skip to content

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

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
cedric-anne opened this issue Oct 25, 2022 · 2 comments · Fixed by #47998
Closed

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

cedric-anne opened this issue Oct 25, 2022 · 2 comments · Fixed by #47998

Comments

@cedric-anne
Copy link
Contributor

cedric-anne commented Oct 25, 2022

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

@maxbeckers
Copy link
Contributor

Yes, I can reproduce that with the example. I'll try a fix for that.

@maxbeckers
Copy link
Contributor

Status: Reviewed

@fabpot fabpot closed this as completed Nov 11, 2022
fabpot added a commit that referenced this issue Nov 11, 2022
…al `ProgressBar::cleanup()` (maxbeckers)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[Console] Fix console `ProgressBar::override()` after manual `ProgressBar::cleanup()`

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #47987
| License       | MIT
| Doc PR        |

In the issue #47987 is described, that there is a problem with overriding lines with `ProgressBar::override()` on a multiline console output with manual call of `ProgressBar::cleanup()`.

Testcode:
```
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 before the fix:
```
Progress bar having only one line:
Foo!
[----->----------------------]
=-=-=-=
Progress bar having two lines:
[----->----------------------]
Processing "foobar"...
```

Expected output / output after the fix:
```
Progress bar having only one line:
Foo!
[----->----------------------]
=-=-=-=
Progress bar having two lines:
Foo!
[----->----------------------]
Processing "foobar"...
```

Commits
-------

aa661aa [Console] Fix console `ProgressBar::override()` after manual `ProgressBar::cleanup()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants