@@ -164,7 +164,7 @@ current progress of the bar. Here is a list of the built-in placeholders:
164
164
* ``remaining ``: The remaining time to complete the task (not available if no max is defined);
165
165
* ``estimated ``: The estimated time to complete the task (not available if no max is defined);
166
166
* ``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) .
168
168
169
169
For instance, here is how you could set the format to be the same as the
170
170
``debug `` one::
@@ -175,20 +175,6 @@ Notice the ``:6s`` part added to some placeholders? That's how you can tweak
175
175
the appearance of the bar (formatting and alignment). The part after the colon
176
176
(``: ``) is used to set the ``sprintf `` format of the string.
177
177
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
-
192
178
Instead of setting the format for a given instance of a progress bar, you can
193
179
also define global formats::
194
180
@@ -299,25 +285,41 @@ that displays the number of remaining steps::
299
285
Custom Messages
300
286
~~~~~~~~~~~~~~~
301
287
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 ::
305
291
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' );
309
295
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:
315
298
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::
319
310
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');
322
314
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