From 58b2cfac96b469822141f23f8d6c6de3e0109e12 Mon Sep 17 00:00:00 2001 From: Alessandro Chitolina Date: Wed, 6 Apr 2016 01:05:47 +0200 Subject: [PATCH 1/2] rewritten overwrite method in ProgressBar to correctly clear previously printed output --- .../Component/Console/Helper/ProgressBar.php | 38 ++++++------------- .../Console/Tests/Helper/ProgressBarTest.php | 14 ++++--- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 016e885f4b288..1b2eefd29e0a3 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -40,7 +40,6 @@ class ProgressBar private $startTime; private $stepWidth; private $percent = 0.0; - private $lastMessagesLength = 0; private $formatLineCount; private $messages; private $overwrite = true; @@ -472,7 +471,7 @@ public function clear() $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat()); } - $this->overwrite(str_repeat("\n", $this->formatLineCount)); + $this->overwrite(''); } /** @@ -512,37 +511,22 @@ private function setMaxSteps($max) */ 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) { - // 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"); - } elseif ($this->step > 0) { - // move to new line - $this->output->writeln(''); - } - if ($this->formatLineCount) { - $this->output->write(sprintf("\033[%dA", $this->formatLineCount)); - } - $this->output->write(implode("\n", $lines)); + // Erase the line + $this->output->write("\x1B[2K"); - $this->lastMessagesLength = 0; - foreach ($lines as $line) { - $len = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line); - if ($len > $this->lastMessagesLength) { - $this->lastMessagesLength = $len; + // Erase previous lines + if ($this->formatLineCount > 0) { + $this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount)); } + } elseif ($this->step > 0) { + $this->output->writeln(''); } + + $this->output->write($message); } private function determineBestFormat() diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index c6bbe81c98a57..06b8cf421a1a6 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -13,6 +13,8 @@ use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\Helper; +use Symfony\Component\Console\Output\ConsoleOutput; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\StreamOutput; /** @@ -233,7 +235,7 @@ public function testOverwriteWithShorterLine() $this->generateOutput(' 0/50 [>---------------------------] 0%'). $this->generateOutput(' 0/50 [>---------------------------] 0%'). $this->generateOutput(' 1/50 [>---------------------------] 2%'). - $this->generateOutput(' 2/50 [=>--------------------------] '), + $this->generateOutput(' 2/50 [=>--------------------------]'), stream_get_contents($output->getStream()) ); } @@ -356,7 +358,7 @@ public function testClear() $this->assertEquals( $this->generateOutput(' 0/50 [>---------------------------] 0%'). $this->generateOutput(' 25/50 [==============>-------------] 50%'). - $this->generateOutput(' '), + $this->generateOutput(''), stream_get_contents($output->getStream()) ); } @@ -554,9 +556,9 @@ public function testMultilineFormat() rewind($output->getStream()); $this->assertEquals( $this->generateOutput(">---------------------------\nfoobar"). - $this->generateOutput("=========>------------------\nfoobar "). - $this->generateOutput(" \n "). - $this->generateOutput("============================\nfoobar "), + $this->generateOutput("=========>------------------\nfoobar"). + "\x0D\x1B[2K\x1B[1A\x1B[2K". + $this->generateOutput("============================\nfoobar"), stream_get_contents($output->getStream()) ); } @@ -665,6 +667,6 @@ protected function generateOutput($expected) { $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; } } From a1dd99149817d74021b75a39e4ddbb21f473222b Mon Sep 17 00:00:00 2001 From: Alessandro Chitolina Date: Sun, 10 Apr 2016 13:44:28 +0200 Subject: [PATCH 2/2] removed unused use statements --- src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 06b8cf421a1a6..04f3dbe4a73c3 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -13,8 +13,6 @@ use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\Helper; -use Symfony\Component\Console\Output\ConsoleOutput; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\StreamOutput; /**