Skip to content

Commit

Permalink
[Console] Fix console ProgressBar::override() after manual `Progres…
Browse files Browse the repository at this point in the history
…sBar::cleanup()`
  • Loading branch information
maxbeckers authored and fabpot committed Nov 11, 2022
1 parent ae0727b commit afcca7c
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
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
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 afcca7c

Please sign in to comment.