Skip to content

Commit da7fd2d

Browse files
Reworded some explanations and expanded examples
1 parent 89a2843 commit da7fd2d

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

components/console/helpers/progressbar.rst

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +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``: used to display arbitrary messages in the progress bar (as explained later).
178179

179180
For instance, here is how you could set the format to be the same as the
180181
``debug`` one::
@@ -298,32 +299,41 @@ that displays the number of remaining steps::
298299
Custom Messages
299300
~~~~~~~~~~~~~~~
300301

301-
If you want to show some fixed text or generic message, you can define custom
302-
placeholders in a custom format to be displayed with the progress bar, and use
303-
them afterwards.
304-
305-
By default, the ``setMessage()`` method implies ``message`` as the name of the
306-
placeholder, but if you need more than one, you have just to define 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::
307305

308306
$progressBar = new ProgressBar($output, 100);
309-
$progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message% %filename%');
307+
$progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%');
310308
$progressBar->setFormat('custom');
311-
$progressBar->setMessage('Start');
312309

310+
Now, use the ``setMessage()`` method to set the value of the ``%message%``
311+
placeholder before displaying the progress bar:
312+
313+
// ...
314+
$progressBar->setMessage('Start');
313315
$progressBar->start();
314316
// 0/100 -- Start
315317

316318
$progressBar->advance();
317319
$progressBar->setMessage('Task is in progress...');
318320
// 1/100 -- Task is in progress...
319321

320-
while ($file = array_pop($files)) {
321-
$bar->setMessage($filename, 'filename');
322-
$bar->advance();
323-
// 2/100 -- Task is in progress... $filename
324-
}
322+
Messages can be combined with custom placeholders too. In this example, the
323+
progress bar uses the ``%message%`` and ``%filename%`` placeholders::
325324

326-
$bar->setMessage('Task is finished');
327-
$bar->setMessage('', 'filename');
328-
$bar->finish();
329-
// 100/100 -- Task is finished
325+
$progressBar = new ProgressBar($output, 100);
326+
$progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message% (%filename%)');
327+
$progressBar->setFormat('custom');
328+
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)