From 0aca9bf03cf01438810cd1510f68c5ad509113ff Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 31 Oct 2016 11:00:03 -0700 Subject: [PATCH] [Console] simplified code --- .../Console/Helper/ProgressIndicator.php | 19 +------------------ .../Tests/Helper/ProgressIndicatorTest.php | 10 +++++----- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressIndicator.php b/src/Symfony/Component/Console/Helper/ProgressIndicator.php index ccf9771bcf9e4..2e43bc3f5a3b8 100644 --- a/src/Symfony/Component/Console/Helper/ProgressIndicator.php +++ b/src/Symfony/Component/Console/Helper/ProgressIndicator.php @@ -28,7 +28,6 @@ class ProgressIndicator private $indicatorCurrent; private $indicatorChangeInterval; private $indicatorUpdateTime; - private $lastMessagesLength; private $started = false; private static $formatters; @@ -125,7 +124,6 @@ public function start($message) $this->message = $message; $this->started = true; - $this->lastMessagesLength = 0; $this->startTime = time(); $this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval; $this->indicatorCurrent = 0; @@ -262,27 +260,12 @@ private function determineBestFormat() */ private function overwrite($message) { - // append whitespace to match the line's length - if (null !== $this->lastMessagesLength) { - if ($this->lastMessagesLength > Helper::strlenWithoutDecoration($this->output->getFormatter(), $message)) { - $message = str_pad($message, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT); - } - } - if ($this->output->isDecorated()) { - $this->output->write("\x0D"); + $this->output->write("\x0D\x1B[2K"); $this->output->write($message); } else { $this->output->writeln($message); } - - $this->lastMessagesLength = 0; - - $len = Helper::strlenWithoutDecoration($this->output->getFormatter(), $message); - - if ($len > $this->lastMessagesLength) { - $this->lastMessagesLength = $len; - } } private function getCurrentTimeInMilliseconds() diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressIndicatorTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressIndicatorTest.php index 192625263db87..2f72d3094b9bb 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressIndicatorTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressIndicatorTest.php @@ -44,11 +44,11 @@ public function testDefaultIndicator() $this->generateOutput(' \\ Starting...'). $this->generateOutput(' \\ Advancing...'). $this->generateOutput(' | Advancing...'). - $this->generateOutput(' | Done... '). + $this->generateOutput(' | Done...'). PHP_EOL. $this->generateOutput(' - Starting Again...'). $this->generateOutput(' \\ Starting Again...'). - $this->generateOutput(' \\ Done Again... '). + $this->generateOutput(' \\ Done Again...'). PHP_EOL, stream_get_contents($output->getStream()) ); @@ -70,8 +70,8 @@ public function testNonDecoratedOutput() $this->assertEquals( ' Starting...'.PHP_EOL. - ' Midway... '.PHP_EOL. - ' Done... '.PHP_EOL.PHP_EOL, + ' Midway...'.PHP_EOL. + ' Done...'.PHP_EOL.PHP_EOL, stream_get_contents($output->getStream()) ); } @@ -177,6 +177,6 @@ protected function generateOutput($expected) { $count = substr_count($expected, "\n"); - return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expected; + return "\x0D\x1B[2K".($count ? sprintf("\033[%dA", $count) : '').$expected; } }