Skip to content

Commit 005ed05

Browse files
committed
bug #47998 [Console] Fix console ProgressBar::override() after manual 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()`
2 parents ea59bb0 + afcca7c commit 005ed05

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

Helper/ProgressBar.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,13 @@ private function overwrite(string $message): void
474474
}
475475
$this->output->clear($lineCount);
476476
} else {
477-
for ($i = 0; $i < $this->formatLineCount; ++$i) {
478-
$this->cursor->moveToColumn(1);
479-
$this->cursor->clearLine();
480-
$this->cursor->moveUp();
477+
if ('' !== $this->previousMessage) {
478+
// only clear upper lines when last call was not a clear
479+
for ($i = 0; $i < $this->formatLineCount; ++$i) {
480+
$this->cursor->moveToColumn(1);
481+
$this->cursor->clearLine();
482+
$this->cursor->moveUp();
483+
}
481484
}
482485

483486
$this->cursor->moveToColumn(1);

Tests/Helper/ProgressBarTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,10 @@ public function testMultilineFormat()
812812
$this->assertEquals(
813813
">---------------------------\nfoobar".
814814
$this->generateOutput("=========>------------------\nfoobar").
815-
"\x1B[1G\x1B[2K\x1B[1A\x1B[1G\x1B[2K".
816-
$this->generateOutput("============================\nfoobar"),
815+
"\x1B[1G\x1B[2K\x1B[1A".
816+
$this->generateOutput('').
817+
$this->generateOutput('============================').
818+
"\nfoobar",
817819
stream_get_contents($output->getStream())
818820
);
819821
}
@@ -1124,4 +1126,29 @@ public function testMultiLineFormatIsFullyCleared()
11241126
stream_get_contents($output->getStream())
11251127
);
11261128
}
1129+
1130+
public function testMultiLineFormatIsFullyCorrectlyWithManuallyCleanup()
1131+
{
1132+
ProgressBar::setFormatDefinition('normal_nomax', "[%bar%]\n%message%");
1133+
$bar = new ProgressBar($output = $this->getOutputStream());
1134+
$bar->setMessage('Processing "foobar"...');
1135+
$bar->start();
1136+
$bar->clear();
1137+
$output->writeln('Foo!');
1138+
$bar->display();
1139+
$bar->finish();
1140+
1141+
rewind($output->getStream());
1142+
$this->assertEquals(
1143+
"[>---------------------------]\n".
1144+
'Processing "foobar"...'.
1145+
"\x1B[1G\x1B[2K\x1B[1A".
1146+
$this->generateOutput('').
1147+
'Foo!'.\PHP_EOL.
1148+
$this->generateOutput('[--->------------------------]').
1149+
"\nProcessing \"foobar\"...".
1150+
$this->generateOutput("[----->----------------------]\nProcessing \"foobar\"..."),
1151+
stream_get_contents($output->getStream())
1152+
);
1153+
}
11271154
}

0 commit comments

Comments
 (0)