[Console] Add default behavior for the %message% placeholder in the ProgressBar #19031
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When I was reading the documentation for the ProgressBar helper http://symfony.com/doc/current/components/console/helpers/progressbar.html I found a nice feature - showing messages by using
%message%
placeholder. But when I tried the example from the docs, I found that it does not work as expected.So this code will give the next output
So as you see no message is displayed. That is because all built-in formats do not have a
%message%
placeholder. You can check the master branch https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Helper/ProgressBar.php#L569 to be sure. Same is in the older versions.Or you can just quickly look at the
initFormat 8000 s()
which I copy-pasted here:So that's why out of box without customizing your format - messages won't be shown. For example in the current implementation of the ProgressBar if I want to display the message I have to add the next code:
And it works...
But as I described here symfony/symfony-docs#6544 It is not clear from the docs. After the reading of docs I thought that message should be displayed out of box without customizing the output.
After that I investigated the code of ProgressBar and found the cause.
So I see two ways how to solve it.
%message%
placeholder which doesn't break the backwards compatibility.So in this patch I propose a small tweak. I propose to add a placeholder formatter for
%message%
placeholder. It will check if there is a message with namemessage
in the array of messages - then it will take it, otherwise - it will ignore it.Also I propose to add a
%message%
to all built-in formats, e.g.'normal' => ' %current%/%max% [%bar%]%message% %percent:3s%%',
But don't surround it with spaces, so in this case if user doesn't set the custom message, the message placeholder formatter will convert it to the empty string, so it behaves like in the current implementation.
If user adds a custom message
$bar->setMessage('Starting');
the formatter will replace placeholder with the current text.There is one additional tweak I have added there. As the
%message%
placeholder is set next to the[%bar%]
without a space, if user adds the message without at least one space at the beginning, the progress bar will look a bit ugly4/4 [============================]Finish... 100%
To prevent it I decided to add a space to the beginning of the message, if message doesn't start with a space. So in my implementation if user sets the message like this
The output will be next (with extra space)
If user manually adds some spaces at the beginning then no extra space will be added.
So with the changes which I propose to the ProgressBar the next code will give the next output
I didn't update existing tests, because this patch doesn't break them. But I added new tests to check the default behavior of the
%message%
placeholder.That's all. Waiting for your feedback.