Skip to content

[Console] Add placeholder formatters per ProgressBar instance #48206

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
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Remove `exit` call in `Application` signal handlers. Commands will no longer be automatically interrupted after receiving signal other than `SIGUSR1` or `SIGUSR2`
* Add `ProgressBar::setPlaceholderFormatter` to set a placeholder attached to a instance, instead of being global.

6.2
---
Expand Down
29 changes: 25 additions & 4 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ final class ProgressBar
private Terminal $terminal;
private ?string $previousMessage = null;
private Cursor $cursor;
private array $placeholders = [];

private static array $formatters;
private static array $formats;
Expand Down Expand Up @@ -95,12 +96,12 @@ public function __construct(OutputInterface $output, int $max = 0, float $minSec
}

/**
* Sets a placeholder formatter for a given name.
* Sets a placeholder formatter for a given name, globally for all instances of ProgressBar.
*
* This method also allow you to override an existing placeholder.
*
* @param string $name The placeholder name (including the delimiter char like %)
* @param callable $callable A PHP callable
* @param string $name The placeholder name (including the delimiter char like %)
* @param callable(ProgressBar):string $callable A PHP callable
*/
public static function setPlaceholderFormatterDefinition(string $name, callable $callable): void
{
Expand All @@ -121,6 +122,26 @@ public static function getPlaceholderFormatterDefinition(string $name): ?callabl
return self::$formatters[$name] ?? null;
}

/**
* Sets a placeholder formatter for a given name, for this instance only.
*
* @param callable(ProgressBar):string $callable A PHP callable
*/
public function setPlaceholderFormatter(string $name, callable $callable): void
{
$this->placeholders[$name] = $callable;
}

/**
* Gets the placeholder formatter for a given name.
*
* @param string $name The placeholder name (including the delimiter char like %)
*/
public function getPlaceholderFormatter(string $name): ?callable
{
return $this->placeholders[$name] ?? $this::getPlaceholderFormatterDefinition($name);
}

/**
* Sets a format for a given name.
*
Expand Down Expand Up @@ -573,7 +594,7 @@ private function buildLine(): string

$regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
$callback = function ($matches) {
if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
if ($formatter = $this->getPlaceholderFormatter($matches[1])) {
$text = $formatter($this, $this->output);
} elseif (isset($this->messages[$matches[1]])) {
$text = $this->messages[$matches[1]];
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,29 @@ public function testAddingPlaceholderFormatter()
);
}

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

$this->assertSame($function, $bar->getPlaceholderFormatter('countdown'));

$bar->start();
$bar->advance();
$bar->finish();

rewind($output->getStream());
$this->assertEquals(
' 3 [>---------------------------]'.
$this->generateOutput(' 2 [=========>------------------]').
$this->generateOutput(' 0 [============================]'),
stream_get_contents($output->getStream())
);
}

public function testMultilineFormat()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 3, 0);
Expand Down