From f8a986ec1bb22130d994f48cc546f637367b8a15 Mon Sep 17 00:00:00 2001 From: Artem Genvald Date: Sat, 11 Jun 2016 14:48:07 +0300 Subject: [PATCH] Refactoring logic for %message% placeholder in ProgressBar --- .../Component/Console/Helper/ProgressBar.php | 46 ++++++++--- .../Console/Tests/Helper/ProgressBarTest.php | 81 +++++++++++++++++++ 2 files changed, 118 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index ca460bee132f1..95e5f10fd3654 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -42,7 +42,7 @@ class ProgressBar private $stepWidth; private $percent = 0.0; private $formatLineCount; - private $messages; + private $messages = array(); private $overwrite = true; private static $formatters; @@ -140,11 +140,24 @@ public static function getFormatDefinition($name) return isset(self::$formats[$name]) ? self::$formats[$name] : null; } + /** + * Set message. + * + * @param string $message Message + * @param string $name Name for a message which is used as placeholder + */ public function setMessage($message, $name = 'message') { $this->messages[$name] = $message; } + /** + * Get message. + * + * @param string $name Name for a message which is used as placeholder + * + * @return mixed + */ public function getMessage($name = 'message') { return $this->messages[$name]; @@ -563,23 +576,38 @@ private static function initPlaceholderFormatters() 'percent' => function (ProgressBar $bar) { return floor($bar->getProgressPercent() * 100); }, + 'message' => function (ProgressBar $bar) { + $message = ''; + + if (isset($bar->messages['message'])) { + $message = $bar->getMessage(); + + if (0 !== strlen($message) && ' ' !== substr($message, 0, 1)) { + // If message does not start with space, then add 1 space at the beginning of the message + // to separate the message from the [%bar%] placeholder + $message = ' '.$message; + } + } + + return $message; + }, ); } private static function initFormats() { return array( - 'normal' => ' %current%/%max% [%bar%] %percent:3s%%', - 'normal_nomax' => ' %current% [%bar%]', + 'normal' => ' %current%/%max% [%bar%]%message% %percent:3s%%', + 'normal_nomax' => ' %current% [%bar%]%message%', - 'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%', - 'verbose_nomax' => ' %current% [%bar%] %elapsed:6s%', + 'verbose' => ' %current%/%max% [%bar%]%message% %percent:3s%% %elapsed:6s%', + 'verbose_nomax' => ' %current% [%bar%]%message% %elapsed:6s%', - 'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%', - 'very_verbose_nomax' => ' %current% [%bar%] %elapsed:6s%', + 'very_verbose' => ' %current%/%max% [%bar%]%message% %percent:3s%% %elapsed:6s%/%estimated:-6s%', + 'very_verbose_nomax' => ' %current% [%bar%]%message% %elapsed:6s%', - 'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%', - 'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%', + 'debug' => ' %current%/%max% [%bar%]%message% %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%', + 'debug_nomax' => ' %current% [%bar%]%message% %elapsed:6s% %memory:6s%', ); } } diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 261908b542307..ef036a78c7872 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -635,6 +635,87 @@ public function testFormatsWithoutMax($format) $this->assertNotEmpty(stream_get_contents($output->getStream())); } + /** + * @param string $format Format + * @param string $expectedOutput Expected output + * + * @dataProvider defaultBehaviorForMessagePlaceholderProvider + */ + public function testDefaultBehaviorForMessagePlaceholder($format, $expectedOutput) + { + $bar = new ProgressBar($output = $this->getOutputStream(), 4); + $bar->setFormat($format); + $bar->setMessage('Text without a space at the beginning'); + $bar->start(); + $bar->setMessage(' Text with a space at the beginning'); + $bar->advance(); + $bar->setMessage(' Text with two spaces at the beginning'); + $bar->advance(); + $bar->setMessage(''); + $bar->advance(); + $bar->setMessage('Finish'); + $bar->finish(); + rewind($output->getStream()); + $this->assertEquals($expectedOutput, stream_get_contents($output->getStream())); + } + + public function defaultBehaviorForMessagePlaceholderProvider() + { + $data = array(); + $data[] = array( + 'normal', + $this->generateOutput(' 0/4 [>---------------------------] Text without a space at the beginning 0%'). + $this->generateOutput(' 1/4 [=======>--------------------] Text with a space at the beginning 25%'). + $this->generateOutput(' 2/4 [==============>-------------] Text with two spaces at the beginning 50%'). + $this->generateOutput(' 3/4 [=====================>------] 75%'). + $this->generateOutput(' 4/4 [============================] Finish 100%'), + ); + $data[] = array( + 'normal_nomax', + $this->generateOutput(' 0 [>---------------------------] Text without a space at the beginning'). + $this->generateOutput(' 1 [=======>--------------------] Text with a space at the beginning'). + $this->generateOutput(' 2 [==============>-------------] Text with two spaces at the beginning'). + $this->generateOutput(' 3 [=====================>------]'). + $this->generateOutput(' 4 [============================] Finish'), + ); + // As this test is very lightweight, it should be executed less than 1 second + // So `elapsed` and `estimated` placeholders are `< 1 sec` here + $data[] = array( + 'verbose', + $this->generateOutput(' 0/4 [>---------------------------] Text without a space at the beginning 0% < 1 sec'). + $this->generateOutput(' 1/4 [=======>--------------------] Text with a space at the beginning 25% < 1 sec'). + $this->generateOutput(' 2/4 [==============>-------------] Text with two spaces at the beginning 50% < 1 sec'). + $this->generateOutput(' 3/4 [=====================>------] 75% < 1 sec'). + $this->generateOutput(' 4/4 [============================] Finish 100% < 1 sec'), + ); + $data[] = array( + 'verbose_nomax', + $this->generateOutput(' 0 [>---------------------------] Text without a space at the beginning < 1 sec'). + $this->generateOutput(' 1 [=======>--------------------] Text with a space at the beginning < 1 sec'). + $this->generateOutput(' 2 [==============>-------------] Text with two spaces at the beginning < 1 sec'). + $this->generateOutput(' 3 [=====================>------] < 1 sec'). + $this->generateOutput(' 4 [============================] Finish < 1 sec'), + ); + $data[] = array( + 'very_verbose', + $this->generateOutput(' 0/4 [>---------------------------] Text without a space at the beginning 0% < 1 sec/< 1 sec'). + $this->generateOutput(' 1/4 [=======>--------------------] Text with a space at the beginning 25% < 1 sec/< 1 sec'). + $this->generateOutput(' 2/4 [==============>-------------] Text with two spaces at the beginning 50% < 1 sec/< 1 sec'). + $this->generateOutput(' 3/4 [=====================>------] 75% < 1 sec/< 1 sec'). + $this->generateOutput(' 4/4 [============================] Finish 100% < 1 sec/< 1 sec'), + ); + $data[] = array( + 'very_verbose_nomax', + $this->generateOutput(' 0 [>---------------------------] Text without a space at the beginning < 1 sec'). + $this->generateOutput(' 1 [=======>--------------------] Text with a space at the beginning < 1 sec'). + $this->generateOutput(' 2 [==============>-------------] Text with two spaces at the beginning < 1 sec'). + $this->generateOutput(' 3 [=====================>------] < 1 sec'). + $this->generateOutput(' 4 [============================] Finish < 1 sec'), + ); + // `debug` and `debug_nomax` are not tested because memory usage can be different on different systems and versions + return $data; + } + /** * Provides each defined format. *