Skip to content

[Console] Add placeholder formatters per ProgressBar instance #17638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions components/console/helpers/progressbar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,27 @@ display that are not available in the list of built-in placeholders, you can
create your own. Let's see how you can create a ``remaining_steps`` placeholder
that displays the number of remaining steps::

// This definition is globally registered for all ProgressBar instances
ProgressBar::setPlaceholderFormatterDefinition(
'remaining_steps',
function (ProgressBar $progressBar, OutputInterface $output) {
return $progressBar->getMaxSteps() - $progressBar->getProgress();
}
);

It is also possible to set a placeholder formatter per ProgressBar instance
with the ``setPlaceholderFormatter`` method::

$progressBar = new ProgressBar($output, 3, 0);
$progressBar->setFormat('%countdown% [%bar%]');
$progressBar->setPlaceholderFormatter('countdown', function (ProgressBar $progressBar) {
return $progressBar->getMaxSteps() - $progressBar->getProgress();
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add

.. versionadded:: 6.3

    The ``setPlaceholderFormatter()`` method was introduced in Symfony 6.3.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of this, but wasn't sure! Do you know if there is any "rule" to it, when to add it or not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexandre-daubois we add the versionadded directive for any new option, method, class, feature, etc. added to Symfony. We also have the deprecated directive to do the opposite.

.. versionadded:: 6.3

The ``setPlaceholderFormatter()`` method was introduced in Symfony 6.3.

Custom Messages
~~~~~~~~~~~~~~~

Expand Down