@@ -170,7 +170,7 @@ current progress of the bar. Here is a list of the built-in placeholders:
170170* ``remaining ``: The remaining time to complete the task (not available if no max is defined);
171171* ``estimated ``: The estimated time to complete the task (not available if no max is defined);
172172* ``memory ``: The current memory usage;
173- * ``message ``: The current message attached to the progress bar.
173+ * ``message ``: used to display arbitrary messages in the progress bar (as explained later) .
174174
175175For instance, here is how you could set the format to be the same as the
176176``debug `` one::
@@ -181,20 +181,6 @@ Notice the ``:6s`` part added to some placeholders? That's how you can tweak
181181the appearance of the bar (formatting and alignment). The part after the colon
182182(``: ``) is used to set the ``sprintf `` format of the string.
183183
184- The ``message `` placeholder is a bit special as you must set the value
185- yourself::
186-
187- $bar->setMessage('Task starts');
188- $bar->start();
189-
190- $bar->setMessage('Task in progress...');
191- $bar->advance();
192-
193- // ...
194-
195- $bar->setMessage('Task is finished');
196- $bar->finish();
197-
198184Instead of setting the format for a given instance of a progress bar, you can
199185also define global formats::
200186
@@ -305,25 +291,41 @@ that displays the number of remaining steps::
305291Custom Messages
306292~~~~~~~~~~~~~~~
307293
308- The `` %message% `` placeholder allows you to specify a custom message to be
309- displayed with the progress bar. But if you need more than one, just define
310- your own::
294+ Progress bars define a placeholder called `` message `` to display arbitrary
295+ messages. However, none of the built-in formats include that placeholder, so
296+ before displaying these messages, you must define your own custom format ::
311297
312- $bar->setMessage('Task starts' );
313- $bar->setMessage(' ', 'filename ');
314- $bar->start( );
298+ $progressBar = new ProgressBar($output, 100 );
299+ $progressBar->setFormatDefinition('custom ', ' %current%/%max% -- %message% ');
300+ $progressBar->setFormat('custom' );
315301
316- $bar->setMessage('Task is in progress...');
317- while ($file = array_pop($files)) {
318- $bar->setMessage($filename, 'filename');
319- $bar->advance();
320- }
302+ Now, use the ``setMessage() `` method to set the value of the ``%message% ``
303+ placeholder before displaying the progress bar:
321304
322- $bar->setMessage('Task is finished');
323- $bar->setMessage('', 'filename');
324- $bar->finish();
305+ // ...
306+ $progressBar->setMessage('Start');
307+ $progressBar->start();
308+ // 0/100 -- Start
309+
310+ $progressBar->advance();
311+ $progressBar->setMessage('Task is in progress...');
312+ // 1/100 -- Task is in progress...
313+
314+ Messages can be combined with custom placeholders too. In this example, the
315+ progress bar uses the ``%message% `` and ``%filename% `` placeholders::
325316
326- For the ``filename `` to be part of the progress bar, just add the
327- ``%filename% `` placeholder in your format::
317+ $progressBar = new ProgressBar($output, 100);
318+ $progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message% (%filename%)');
319+ $progressBar->setFormat('custom');
328320
329- $bar->setFormat(" %message%\n %current%/%max%\n Working on %filename%");
321+ The ``setMessage() `` method accepts a second optional argument to set the value
322+ of the custom placeholders::
323+
324+ // ...
325+ // $files = array('client-001/invoices.xml', '...');
326+ foreach ($files as $filename) {
327+ $progressBar->setMessage('Importing invoices...');
328+ $progressBar->setMessage($filename, 'filename');
329+ $progressBar->advance();
330+ // 2/100 -- Importing invoices... (client-001/invoices.xml)
331+ }
0 commit comments