8000 [Console] Add default behavior for the %message% placeholder in the ProgressBar by fre5h · Pull Request #19031 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add default behavior for the %message% placeholder in the ProgressBar #19031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension 8000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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%',
);
}
}
81 changes: 81 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
0