8000 minor #7251 Fix invalid ProgressBar message examples (Jean85, javiere… · symfony/symfony-docs@7f36089 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f36089

Browse files
committed
minor #7251 Fix invalid ProgressBar message examples (Jean85, javiereguiluz)
This PR was merged into the 2.7 branch. Discussion ---------- Fix invalid ProgressBar message examples This fixes #6544. I've removed the confusing bits at the top, where the message placeholder is mentioned, and changed the last part, making the example more meaningful. Commits ------- da7fd2d Reworded some explanations and expanded examples 89a2843 Fix explanation of custom placeholder 771f952 Fix invalid ProgressBar message examples
2 parents 2e5aae1 + da7fd2d commit 7f36089

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

components/console/helpers/progressbar.rst

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ current progress of the bar. Here is a list of the built-in placeholders:
175175
* ``remaining``: The remaining time to complete the task (not available if no max is defined);
176176
* ``estimated``: The estimated time to complete the task (not available if no max is defined);
177177
* ``memory``: The current memory usage;
178-
* ``message``: The current message attached to the progress bar.
178+
* ``message``: used to display arbitrary messages in the progress bar (as explained later).
179179

180180
For instance, here is how you could set the format to be the same as the
181181
``debug`` one::
@@ -186,20 +186,6 @@ Notice the ``:6s`` part added to some placeholders? That's how you can tweak
186186
the appearance of the bar (formatting and alignment). The part after the colon
187187
(``:``) is used to set the ``sprintf`` format of the string.
188188

189-
The ``message`` placeholder is a bit special as you must set the value
190-
yourself::
191-
192-
$bar->setMessage('Task starts');
193-
$bar->start();
194-
195-
$bar->setMessage('Task in progress...');
196-
$bar->advance();
197-
198-
// ...
199-
200-
$bar->setMessage('Task is finished');
201-
$bar->finish();
202-
203189
Instead of setting the format for a given instance of a progress bar, you can
204190
also define global formats::
205191

@@ -313,25 +299,41 @@ that displays the number of remaining steps::
313299
Custom Messages
314300
~~~~~~~~~~~~~~~
315301

316-
The ``%message%`` placeholder allows you to specify a custom message to be
317-
displayed with the progress bar. But if you need more than one, just define
318-
your own::
302+
Progress bars define a placeholder called ``message`` to display arbitrary
303+
messages. However, none of the built-in formats include that placeholder, so
304+
before displaying these messages, you must define your own custom format::
319305

320-
$bar->setMessage('Task starts');
321-
$bar->setMessage('', 'filename');
322-
$bar->start();
306+
$progressBar = new ProgressBar($output, 100);
307+
$progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%');
308+
$progressBar->setFormat('custom');
323309

324-
$bar->setMessage('Task is in progress...');
325-
while ($file = array_pop($files)) {
326-
$bar->setMessage($filename, 'filename');
327-
$bar->advance();
328-
}
310+
Now, use the ``setMessage()`` method to set the value of the ``%message%``
311+
placeholder before displaying the progress bar:
329312

330-
$bar->setMessage('Task is finished');
331-
$bar->setMessage('', 'filename');
332-
$bar->finish();
313+
// ...
314+
$progressBar->setMessage('Start');
315+
$progressBar->start();
316+
// 0/100 -- Start
317+
318+
$progressBar->advance();
319+
$progressBar->setMessage('Task is in progress...');
320+
// 1/100 -- Task is in progress...
321+
322+
Messages can be combined with custom placeholders too. In this example, the
323+
progress bar uses the ``%message%`` and ``%filename%`` placeholders::
333324

334-
For the ``filename`` to be part of the progress bar, just add the
335-
``%filename%`` placeholder in your format::
325+
$progressBar = new ProgressBar($output, 100);
326+
$progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message% (%filename%)');
327+
$progressBar->setFormat('custom');
336328

337-
$bar->setFormat(" %message%\n %current%/%max%\n Working on %filename%");
329+
The ``setMessage()`` method accepts a second optional argument to set the value
330+
of the custom placeholders::
331+
332+
// ...
333+
// $files = array('client-001/invoices.xml', '...');
334+
foreach ($files as $filename) {
335+
$progressBar->setMessage('Importing invoices...');
336+
$progressBar->setMessage($filename, 'filename');
337+
$progressBar->advance();
338+
// 2/100 -- Importing invoices... (client-001/invoices.xml)
339+
}

0 commit comments

Comments
 (0)
0