Skip to content

Commit

Permalink
bug #47998 [Console] Fix console ProgressBar::override() after manu…
Browse files Browse the repository at this point in the history
…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
-------

aa661aa9ce [Console] Fix console `ProgressBar::override()` after manual `ProgressBar::cleanup()`
  • Loading branch information
fabpot committed Nov 11, 2022
2 parents ea59bb0 + afcca7c commit 005ed05
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
11 changes: 7 additions & 4 deletions Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,13 @@ private function overwrite(string $message): void
}
$this->output->clear($lineCount);
} else {
for ($i = 0; $i < $this->formatLineCount; ++$i) {
$this->cursor->moveToColumn(1);
$this->cursor->clearLine();
$this->cursor->moveUp();
if ('' !== $this->previousMessage) {
// only clear upper lines when last call was not a clear
for ($i = 0; $i < $this->formatLineCount; ++$i) {
$this->cursor->moveToColumn(1);
$this->cursor->clearLine();
$this->cursor->moveUp();
}
}

$this->cursor->moveToColumn(1);
Expand Down
31 changes: 29 additions & 2 deletions Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,10 @@ public function testMultilineFormat()
$this->assertEquals(
">---------------------------\nfoobar".
$this->generateOutput("=========>------------------\nfoobar").
"\x1B[1G\x1B[2K\x1B[1A\x1B[1G\x1B[2K".
$this->generateOutput("============================\nfoobar"),
"\x1B[1G\x1B[2K\x1B[1A".
$this->generateOutput('').
$this->generateOutput('============================').
"\nfoobar",
stream_get_contents($output->getStream())
);
}
Expand Down Expand Up @@ -1124,4 +1126,29 @@ public function testMultiLineFormatIsFullyCleared()
stream_get_contents($output->getStream())
);
}

public function testMultiLineFormatIsFullyCorrectlyWithManuallyCleanup()
{
ProgressBar::setFormatDefinition('normal_nomax', "[%bar%]\n%message%");
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->setMessage('Processing "foobar"...');
$bar->start();
$bar->clear();
$output->writeln('Foo!');
$bar->display();
$bar->finish();

rewind($output->getStream());
$this->assertEquals(
"[>---------------------------]\n".
'Processing "foobar"...'.
"\x1B[1G\x1B[2K\x1B[1A".
$this->generateOutput('').
'Foo!'.\PHP_EOL.
$this->generateOutput('[--->------------------------]').
"\nProcessing \"foobar\"...".
$this->generateOutput("[----->----------------------]\nProcessing \"foobar\"..."),
stream_get_contents($output->getStream())
);
}
}

0 comments on commit 005ed05

Please sign in to comment.