@@ -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
180180For 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
186186the 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-
203189Instead of setting the format for a given instance of a progress bar, you can
204190also define global formats::
205191
@@ -313,25 +299,41 @@ that displays the number of remaining steps::
313299Custom 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