8000 Add placeholder formatters per ProgressBar instance · symfony/symfony@c5fdfbc · GitHub
[go: up one dir, main page]

Skip to content

Commit c5fdfbc

Browse files
GromNaNfabpot
authored andcommitted
Add placeholder formatters per ProgressBar instance
1 parent 96993d9 commit c5fdfbc

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

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

910
6.2
1011
---

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ final class ProgressBar
6060
private Terminal $terminal;
6161
private ?string $previousMessage = null;
6262
private Cursor $cursor;
63+
private array $placeholders = [];
6364

6465
private static array $formatters;
6566
private static array $formats;
@@ -95,12 +96,12 @@ public function __construct(OutputInterface $output, int $max = 0, float $minSec
9596
}
9697

9798
/**
98-
* Sets a placeholder formatter for a given name.
99+
* Sets a placeholder formatter for a given name, globally for all instances of ProgressBar.
99100
*
100101
* This method also allow you to override an existing placeholder.
101102
*
102-
* @param string $name The placeholder name (including the delimiter char like %)
103-
* @param callable $callable A PHP callable
103+
* @param string $name The placeholder name (including the delimiter char like %)
104+
* @param callable(ProgressBar):string $callable A PHP callable
104105
*/
105106
public static function setPlaceholderFormatterDefinition(string $name, callable $callable): void
106107
{
@@ -121,6 +122,26 @@ public static function getPlaceholderFormatterDefinition(string $name): ?callabl
121122
return self::$formatters[$name] ?? null;
122123
}
123124

125+
/**
126+
* Sets a placeholder formatter for a given name, for this instance only.
127+
*
128+
* @param callable(ProgressBar):string $callable A PHP callable
129+
*/
130+
public function setPlaceholderFormatter(string $name, callable $callable): void
131+
{
132+
$this->placeholders[$name] = $callable;
133+
}
134+
135+
/**
136+
* Gets the placeholder formatter for a given name.
137+
*
138+
* @param string $name The placeholder name (including the delimiter char like %)
139+
*/
140+
public function getPlaceholderFormatter(string $name): ?callable
141+
{
142+
return $this->placeholders[$name] ?? $this::getPlaceholderFormatterDefinition($name);
143+
}
144+
124145
/**
125146
* Sets a format for a given name.
126147
*
@@ -573,7 +594,7 @@ private function buildLine(): string
573594

574595
$regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
575596
$callback = function ($matches) {
576-
if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
597+
if ($formatter = $this->getPlaceholderFormatter($matches[1])) {
577598
$text = $formatter($this, $this->output);
578599
} elseif (isset($this->messages[$matches[1]])) {
579600
$text = $this->messages[$matches[1]];

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,29 @@ public function testAddingPlaceholderFormatter()
861861
);
862862
}
863863

864+
public function testAddingInstancePlaceholderFormatter()
865+
{
866+
$bar = new ProgressBar($output = $this->getOutputStream(), 3, 0);
867+
$bar->setFormat(' %countdown% [%bar%]');
868+
$bar->setPlaceholderFormatter('countdown', $function = function (ProgressBar $bar) {
869+
return $bar->getMaxSteps() - $bar->getProgress();
870+
});
871+
872+
$this->assertSame($function, $bar->getPlaceholderFormatter('countdown'));
873+
874+
$bar->start();
875+
$bar->advance();
876+
$bar->finish();
877+
878+
rewind($output->getStream());
879+
$this->assertEquals(
880+
' 3 [>---------------------------]'.
881+
$this->generateOutput(' 2 [=========>------------------]').
882+
$this->generateOutput(' 0 [============================]'),
883+
stream_get_contents($output->getStream())
884+
);
885+
}
886+
864887
public function testMultilineFormat()
865888
{
866889
$bar = new ProgressBar($output = $this->getOutputStream(), 3, 0);

0 commit comments

Comments
 (0)
0