@@ -175,6 +175,7 @@ current progress of the bar. Here is a list of the built-in placeholders:
175
175
* ``remaining ``: The remaining time to complete the task (not available if no max is defined);
176
176
* ``estimated ``: The estimated time to complete the task (not available if no max is defined);
177
177
* ``memory ``: The current memory usage;
178
+ * ``message ``: used to display arbitrary messages in the progress bar (as explained later).
178
179
179
180
For instance, here is how you could set the format to be the same as the
180
181
``debug `` one::
@@ -298,32 +299,41 @@ that displays the number of remaining steps::
298
299
Custom Messages
299
300
~~~~~~~~~~~~~~~
300
301
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::
307
305
308
306
$progressBar = new ProgressBar($output, 100);
309
- $progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message% %filename% ');
307
+ $progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%');
310
308
$progressBar->setFormat('custom');
311
- $progressBar->setMessage('Start');
312
309
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');
313
315
$progressBar->start();
314
316
// 0/100 -- Start
315
317
316
318
$progressBar->advance();
317
319
$progressBar->setMessage('Task is in progress...');
318
320
// 1/100 -- Task is in progress...
319
321
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::
325
324
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