Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Support ProgressBar::iterate() on empty array #52605

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
Nov 16, 2023
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
59 changes: 43 additions & 16 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function getStartTime(): int

public function getMaxSteps(): int
{
return $this->max;
return $this->max ?? 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

$this->max can be null, but keep returning 0 for backward compatibility.

}

public function getProgress(): int
Expand All @@ -215,7 +215,7 @@ public function getProgressPercent(): float

public function getBarOffset(): float
{
return floor($this->max ? $this->percent * $this->barWidth : (null === $this->redrawFreq ? (int) (min(5, $this->barWidth / 15) * $this->writeCount) : $this->step) % $this->barWidth);
return floor(null !== $this->max ? $this->percent * $this->barWidth : (null === $this->redrawFreq ? (int) (min(5, $this->barWidth / 15) * $this->writeCount) : $this->step) % $this->barWidth);
}

public function getEstimated(): float
Expand Down Expand Up @@ -253,7 +253,7 @@ public function setBarCharacter(string $char): void

public function getBarCharacter(): string
{
return $this->barChar ?? ($this->max ? '=' : $this->emptyBarChar);
return $this->barChar ?? (null !== $this->max ? '=' : $this->emptyBarChar);
}

public function setEmptyBarCharacter(string $char): void
Expand Down Expand Up @@ -315,7 +315,21 @@ public function maxSecondsBetweenRedraws(float $seconds): void
*/
public function iterate(iterable $iterable, int $max = null): iterable
{
$this->start($max ?? (is_countable($iterable) ? \count($iterable) : 0));
if (0 === $max) {
$max = null;
}

$max ??= is_countable($iterable) ? \count($iterable) : null;

if (0 === $max) {
$this->max = 0;
$this->stepWidth = 2;
$this->finish();

return;
}

$this->start($max);

foreach ($iterable as $key => $value) {
yield $key => $value;
Expand Down Expand Up @@ -373,11 +387,15 @@ public function setProgress(int $step): void
$step = 0;
}

$redrawFreq = $this->redrawFreq ?? (($this->max ?: 10) / 10);
$prevPeriod = (int) ($this->step / $redrawFreq);
$currPeriod = (int) ($step / $redrawFreq);
$redrawFreq = $this->redrawFreq ?? (($this->max ?? 10) / 10);
$prevPeriod = $redrawFreq ? (int) ($this->step / $redrawFreq) : 0;
$currPeriod = $redrawFreq ? (int) ($step / $redrawFreq) : 0;
$this->step = $step;
$this->percent = $this->max ? (float) $this->step / $this->max : 0;
$this->percent = match ($this->max) {
null => 0,
0 => 1,
default => (float) $this->step / $this->max,
};
$timeInterval = microtime(true) - $this->lastWriteTime;

// Draw regardless of other limits
Expand All @@ -398,28 +416,37 @@ public function setProgress(int $step): void
}
}

public function setMaxSteps(int $max): void
public function setMaxSteps(?int $max): void
{
if (0 === $max) {
$max = null;
}

$this->format = null;
$this->max = max(0, $max);
$this->stepWidth = $this->max ? Helper::width((string) $this->max) : 4;
if (null === $max) {
$this->max = null;
$this->stepWidth = 4;
} else {
$this->max = max(0, $max);
$this->stepWidth = Helper::width((string) $this->max);
}
}

/**
* Finishes the progress output.
*/
public function finish(): void
{
if (!$this->max) {
if (null === $this->max) {
$this->max = $this->step;
}

if ($this->step === $this->max && !$this->overwrite) {
if (($this->step === $this->max || null === $this->max) && !$this->overwrite) {
// prevent double 100% output
return;
}

$this->setProgress($this->max);
$this->setProgress($this->max ?? $this->step);
}

/**
Expand Down Expand Up @@ -542,14 +569,14 @@ private static function initPlaceholderFormatters(): array
},
'elapsed' => fn (self $bar) => Helper::formatTime(time() - $bar->getStartTime(), 2),
'remaining' => function (self $bar) {
if (!$bar->getMaxSteps()) {
if (null === $bar->getMaxSteps()) {
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
}

return Helper::formatTime($bar->getRemaining(), 2);
},
'estimated' => function (self $bar) {
if (!$bar->getMaxSteps()) {
if (null === $bar->getMaxSteps()) {
throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
}

Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,20 @@ public function testIterateUncountable()
);
}

public function testEmptyInputWithDebugFormat()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->setFormat('%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%');

$this->assertEquals([], iterator_to_array($bar->iterate([])));

rewind($output->getStream());
$this->assertEquals(
' 0/0 [============================] 100% < 1 sec/< 1 sec',
stream_get_contents($output->getStream())
);
}

protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
{
return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
Expand Down Expand Up @@ -1263,7 +1277,7 @@ public function testMultiLineFormatIsFullyCorrectlyWithManuallyCleanup()
'Foo!'.\PHP_EOL.
$this->generateOutput('[--->------------------------]').
"\nProcessing \"foobar\"...".
$this->generateOutput("[----->----------------------]\nProcessing \"foobar\"..."),
$this->generateOutput("[============================]\nProcessing \"foobar\"..."),
stream_get_contents($output->getStream())
);
}
Expand Down