@@ -164,7 +164,7 @@ current progress of the bar. Here is a list of the built-in placeholders:
164164* ``remaining ``: The remaining time to complete the task (not available if no max is defined);
165165* ``estimated ``: The estimated time to complete the task (not available if no max is defined);
166166* ``memory ``: The current memory usage;
167- * ``message ``: The current message attached to the progress bar.
167+ * ``message ``: used to display arbitrary messages in the progress bar (as explained later) .
168168
169169For instance, here is how you could set the format to be the same as the
170170``debug `` one::
@@ -175,20 +175,6 @@ Notice the ``:6s`` part added to some placeholders? That's how you can tweak
175175the appearance of the bar (formatting and alignment). The part after the colon
176176(``: ``) is used to set the ``sprintf `` format of the string.
177177
178- The ``message `` placeholder is a bit special as you must set the value
179- yourself::
180-
181- $bar->setMessage('Task starts');
182- $bar->start();
183-
184- $bar->setMessage('Task in progress...');
185- $bar->advance();
186-
187- // ...
188-
189- $bar->setMessage('Task is finished');
190- $bar->finish();
191-
192178Instead of setting the format for a given instance of a progress bar, you can
193179also define global formats::
194180
@@ -299,25 +285,41 @@ that displays the number of remaining steps::
299285Custom Messages
300286~~~~~~~~~~~~~~~
301287
302- The `` %message% `` placeholder allows you to specify a custom message to be
303- displayed with the progress bar. But if you need more than one, just define
304- your own::
288+ Progress bars define a placeholder called `` message `` to display arbitrary
289+ messages. However, none of the built-in formats include that placeholder, so
290+ before displaying these messages, you must define your own custom format ::
305291
306- $bar->setMessage('Task starts' );
307- $bar->setMessage(' ', 'filename ');
308- $bar->start( );
292+ $progressBar = new ProgressBar($output, 100 );
293+ $progressBar->setFormatDefinition('custom ', ' %current%/%max% -- %message% ');
294+ $progressBar->setFormat('custom' );
309295
310- $bar->setMessage('Task is in progress...');
311- while ($file = array_pop($files)) {
312- $bar->setMessage($filename, 'filename');
313- $bar->advance();
314- }
296+ Now, use the ``setMessage() `` method to set the value of the ``%message% ``
297+ placeholder before displaying the progress bar:
315298
316- $bar->setMessage('Task is finished');
317- $bar->setMessage('', 'filename');
318- $bar->finish();
299+ // ...
300+ $progressBar->setMessage('Start');
301+ $progressBar->start();
302+ // 0/100 -- Start
303+
304+ $progressBar->advance();
305+ $progressBar->setMessage('Task is in progress...');
306+ // 1/100 -- Task is in progress...
307+
308+ Messages can be combined with custom placeholders too. In this example, the
309+ progress bar uses the ``%message% `` and ``%filename% `` placeholders::
319310
320- For the ``filename `` to be part of the progress bar, just add the
321- ``%filename% `` placeholder in your format::
311+ $progressBar = new ProgressBar($output, 100);
312+ $progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message% (%filename%)');
313+ $progressBar->setFormat('custom');
322314
323- $bar->setFormat(" %message%\n %current%/%max%\n Working on %filename%");
315+ The ``setMessage() `` method accepts a second optional argument to set the value
316+ of the custom placeholders::
317+
318+ // ...
319+ // $files = array('client-001/invoices.xml', '...');
320+ foreach ($files as $filename) {
321+ $progressBar->setMessage('Importing invoices...');
322+ $progressBar->setMessage($filename, 'filename');
323+ $progressBar->advance();
324+ // 2/100 -- Importing invoices... (client-001/invoices.xml)
325+ }
0 commit comments