8000 feature #26449 Make ProgressBar::setMaxSteps public (ostrolucky) · symfony/symfony@4cc8cf6 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 4cc8cf6

Browse files
committed
feature #26449 Make ProgressBar::setMaxSteps public (ostrolucky)
This PR was merged into the 4.1-dev branch. Discussion ---------- Make ProgressBar::setMaxSteps public | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24100 | License | MIT | Doc PR | - This is useful in cases when target of tracking changes its size during progress advancement. My exact use case is showing progress of file upload for file which is still being downloading. Commits ------- 2b3c37a Make ProgressBar::setMaxSteps public
2 parents 7101893 + 2b3c37a commit 4cc8cf6

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ public function setProgress(int $step)
295295
}
296296
}
297297

298+
public function setMaxSteps(int $max)
299+
{
300+
$this->format = null;
301+
$this->max = max(0, $max);
302+
$this->stepWidth = $this->max ? Helper::strlen((string) $this->max) : 4;
303+
}
304+
298305
/**
299306
* Finishes the progress output.
300307
*/
@@ -362,12 +369,6 @@ private function setRealFormat(string $format)
362369
$this->formatLineCount = substr_count($this->format, "\n");
363370
}
364371

365-
private function setMaxSteps(int $max)
366-
{
367-
$this->max = max(0, $max);
368-
$this->stepWidth = $this->max ? Helper::strlen((string) $this->max) : 4;
369-
}
370-
371372
/**
372373
* Overwrites a previous message to the output.
373374
*/

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,29 @@ public function testWithoutMax()
592592
);
593593
}
594594

595+
public function testSettingMaxStepsDuringProgressing()
596+
{
597+
$output = $this->getOutputStream();
598+
$bar = new ProgressBar($output);
599+
$bar->start();
600+
$bar->setProgress(2);
601+
$bar->setMaxSteps(10);
602+
$bar->setProgress(5);
603+
$bar->setMaxSteps(100);
604+
$bar->setProgress(10);
605+
$bar->finish();
606+
607+
rewind($output->getStream());
608+
$this->assertEquals(
609+
rtrim(' 0 [>---------------------------]').
610+
rtrim($this->generateOutput(' 2 [-->-------------------------]')).
611+
rtrim($this->generateOutput(' 5/10 [==============>-------------] 50%')).
612+
rtrim($this->generateOutput(' 10/100 [==>-------------------------] 10%')).
613+
rtrim($this->generateOutput(' 100/100 [============================] 100%')),
614+
stream_get_contents($output->getStream())
615+
);
616+
}
617+
595618
public function testWithSmallScreen()
596619
{
597620
$output = $this->getOutputStream();

0 commit comments

Comments
 (0)
0