[Console] use ANSI escape sequences in ProgressBar overwrite method
This commit is contained in:
parent
064aedf9e3
commit
b6cca4c020
@ -40,7 +40,6 @@ class ProgressBar
|
|||||||
private $startTime;
|
private $startTime;
|
||||||
private $stepWidth;
|
private $stepWidth;
|
||||||
private $percent = 0.0;
|
private $percent = 0.0;
|
||||||
private $lastMessagesLength = 0;
|
|
||||||
private $formatLineCount;
|
private $formatLineCount;
|
||||||
private $messages;
|
private $messages;
|
||||||
private $overwrite = true;
|
private $overwrite = true;
|
||||||
@ -472,7 +471,7 @@ class ProgressBar
|
|||||||
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
|
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->overwrite(str_repeat("\n", $this->formatLineCount));
|
$this->overwrite('');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -512,37 +511,22 @@ class ProgressBar
|
|||||||
*/
|
*/
|
||||||
private function overwrite($message)
|
private function overwrite($message)
|
||||||
{
|
{
|
||||||
$lines = explode("\n", $message);
|
|
||||||
|
|
||||||
// append whitespace to match the line's length
|
|
||||||
if (null !== $this->lastMessagesLength) {
|
|
||||||
foreach ($lines as $i => $line) {
|
|
||||||
if ($this->lastMessagesLength > Helper::strlenWithoutDecoration($this->output->getFormatter(), $line)) {
|
|
||||||
$lines[$i] = str_pad($line, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->overwrite) {
|
if ($this->overwrite) {
|
||||||
// move back to the beginning of the progress bar before redrawing it
|
// Move the cursor to the beginning of the line
|
||||||
$this->output->write("\x0D");
|
$this->output->write("\x0D");
|
||||||
|
|
||||||
|
// Erase the line
|
||||||
|
$this->output->write("\x1B[2K");
|
||||||
|
|
||||||
|
// Erase previous lines
|
||||||
|
if ($this->formatLineCount > 0) {
|
||||||
|
$this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount));
|
||||||
|
}
|
||||||
} elseif ($this->step > 0) {
|
} elseif ($this->step > 0) {
|
||||||
// move to new line
|
|
||||||
$this->output->writeln('');
|
$this->output->writeln('');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->formatLineCount) {
|
$this->output->write($message);
|
||||||
$this->output->write(sprintf("\033[%dA", $this->formatLineCount));
|
|
||||||
}
|
|
||||||
$this->output->write(implode("\n", $lines));
|
|
||||||
|
|
||||||
$this->lastMessagesLength = 0;
|
|
||||||
foreach ($lines as $line) {
|
|
||||||
$len = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line);
|
|
||||||
if ($len > $this->lastMessagesLength) {
|
|
||||||
$this->lastMessagesLength = $len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function determineBestFormat()
|
private function determineBestFormat()
|
||||||
|
@ -555,7 +555,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$this->generateOutput(">---------------------------\nfoobar").
|
$this->generateOutput(">---------------------------\nfoobar").
|
||||||
$this->generateOutput("=========>------------------\nfoobar").
|
$this->generateOutput("=========>------------------\nfoobar").
|
||||||
$this->generateOutput(" \n ").
|
"\x0D\x1B[2K\x1B[1A\x1B[2K".
|
||||||
$this->generateOutput("============================\nfoobar"),
|
$this->generateOutput("============================\nfoobar"),
|
||||||
stream_get_contents($output->getStream())
|
stream_get_contents($output->getStream())
|
||||||
);
|
);
|
||||||
@ -665,6 +665,6 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$count = substr_count($expected, "\n");
|
$count = substr_count($expected, "\n");
|
||||||
|
|
||||||
return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expected;
|
return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user