|
| 1 | +Progress Indicator |
| 2 | +================== |
| 3 | + |
| 4 | +When executing longer-running commands without knowing if the the processing |
| 5 | +is nearly done or not, it may be helpful to show that something is actually |
| 6 | +happening and that updates as your command runs. |
| 7 | + |
| 8 | +To do so, use the |
| 9 | +:class:`Symfony\\Component\\Console\\Helper\\ProgressIndicator` and advance the |
| 10 | +progress as the command executes:: |
| 11 | + |
| 12 | + use Symfony\Component\Console\Helper\ProgressIndicator; |
| 13 | + |
| 14 | + // creates a new progress indicator |
| 15 | + $progressIndicator = new ProgressIndicator($output); |
| 16 | + |
| 17 | + // starts and displays the progress indicator with a custom message |
| 18 | + $progressIndicator->start('Processing...'); |
| 19 | + |
| 20 | + $i = 0; |
| 21 | + while ($i++ < 50) { |
| 22 | + // ... do some work |
| 23 | + |
| 24 | + // advances the progress indicator |
| 25 | + $progressIndicator->advance(); |
| 26 | + } |
| 27 | + |
| 28 | + // ensures that the progress indicator shows a final message |
| 29 | + $progressIndicator->finish('Finished'); |
| 30 | + |
| 31 | +Customizing the Progress Indicator |
| 32 | +---------------------------------- |
| 33 | + |
| 34 | +Built-in Formats |
| 35 | +~~~~~~~~~~~~~~~~ |
| 36 | + |
| 37 | +By default, the information rendered on a progress indicator depends on the current |
| 38 | +level of verbosity of the ``OutputInterface`` instance: |
| 39 | + |
| 40 | +.. code-block:: text |
| 41 | +
|
| 42 | + # OutputInterface::VERBOSITY_NORMAL (CLI with no verbosity flag) |
| 43 | + \ Processing... |
| 44 | + | Processing... |
| 45 | + / Processing... |
| 46 | + - Processing... |
| 47 | +
|
| 48 | + # OutputInterface::VERBOSITY_VERBOSE (-v) |
| 49 | + \ Processing... (1 sec) |
| 50 | + | Processing... (1 sec) |
| 51 | + / Processing... (1 sec) |
| 52 | + - Processing... (1 sec) |
| 53 | +
|
| 54 | + # OutputInterface::VERBOSITY_VERY_VERBOSE (-vv) and OutputInterface::VERBOSITY_DEBUG (-vvv) |
| 55 | + \ Processing... (1 sec, 6.0 MiB) |
| 56 | + | Processing... (1 sec, 6.0 MiB) |
| 57 | + / Processing... (1 sec, 6.0 MiB) |
| 58 | + - Processing... (1 sec, 6.0 MiB) |
| 59 | +
|
| 60 | +.. note:: |
| 61 | + |
| 62 | + If you call a command with the quiet flag (``-q``), the progress indicator won't |
| 63 | + be displayed. |
| 64 | + |
| 65 | +Instead of relying on the verbosity mode of the current command, you can also |
| 66 | +force a format via the second argument of the ``ProgressIndicator`` |
| 67 | +constructor:: |
| 68 | + |
| 69 | + $progressIndicator = new ProgressIndicator($output, 'verbose'); |
| 70 | + |
| 71 | +The built-in formats are the following: |
| 72 | + |
| 73 | +* ``normal`` |
| 74 | +* ``verbose`` |
| 75 | +* ``very_verbose`` |
| 76 | + |
| 77 | +If your terminal doesn't support ANSI, use the ``no_ansi`` variants: |
| 78 | + |
| 79 | +* ``normal_no_ansi`` |
| 80 | +* ``verbose_no_ansi`` |
| 81 | +* ``very_verbose_no_ansi`` |
| 82 | + |
| 83 | +Custom Indicator Values |
| 84 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 85 | + |
| 86 | +Instead of using the built-in indicator values, you can also set your own:: |
| 87 | + |
| 88 | + $progressIndicator = new ProgressIndicator($output, 'verbose', 100, ['⠏', '⠛', '⠹', '⢸', '⣰', '⣤', '⣆', '⡇']); |
| 89 | + |
| 90 | +The progress indicator will now look like this: |
| 91 | + |
| 92 | +.. code-block:: text |
| 93 | +
|
| 94 | +<
57AE
div class="diff-text-inner"> ⠏ Processing... |
| 95 | + ⠛ Processing... |
| 96 | + ⠹ Processing... |
| 97 | + ⢸ Processing... |
| 98 | +
|
| 99 | +Customize Placeholders |
| 100 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 101 | + |
| 102 | +A progress indicator uses placeholders (a name enclosed with the ``%`` |
| 103 | +character) to determine the output format. Here is a list of the |
| 104 | +built-in placeholders: |
| 105 | + |
| 106 | +* ``indicator``: The current indicator; |
| 107 | +* ``elapsed``: The time elapsed since the start of the progress indicator; |
| 108 | +* ``memory``: The current memory usage; |
| 109 | +* ``message``: used to display arbitrary messages in the progress indicator. |
| 110 | + |
| 111 | +If you want to customize a placeholder, for example the ``message`` one, here |
| 112 | +is how you should do this:: |
| 113 | + |
| 114 | + ProgressIndicator::setPlaceholderFormatterDefinition( |
| 115 | + 'message', |
| 116 | + static function (ProgressIndicator $progressIndicator): string { |
| 117 | + // Return any arbitrary string |
| 118 | + return 'My custom message'; |
| 119 | + } |
| 120 | + ); |
| 121 | + |
| 122 | +.. note:: |
| 123 | + |
| 124 | + Placeholders customization is applied globally, which means that any |
| 125 | + progress indicator displayed after the |
| 126 | + ``setPlaceholderFormatterDefinition()`` call will be affected. |
0 commit comments