Skip to content

Commit

Permalink
feature #26449 Make ProgressBar::setMaxSteps public (ostrolucky)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Mar 19, 2018
2 parents 7101893 + 2b3c37a commit 4cc8cf6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Expand Up @@ -295,6 +295,13 @@ public function setProgress(int $step)
}
}

public function setMaxSteps(int $max)
{
$this->format = null;
$this->max = max(0, $max);
$this->stepWidth = $this->max ? Helper::strlen((string) $this->max) : 4;
}

/**
* Finishes the progress output.
*/
Expand Down Expand Up @@ -362,12 +369,6 @@ private function setRealFormat(string $format)
$this->formatLineCount = substr_count($this->format, "\n");
}

private function setMaxSteps(int $max)
{
$this->max = max(0, $max);
$this->stepWidth = $this->max ? Helper::strlen((string) $this->max) : 4;
}

/**
* Overwrites a previous message to the output.
*/
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Expand Up @@ -592,6 +592,29 @@ public function testWithoutMax()
);
}

public function testSettingMaxStepsDuringProgressing()
{
$output = $this->getOutputStream();
$bar = new ProgressBar($output);
$bar->start();
$bar->setProgress(2);
$bar->setMaxSteps(10);
$bar->setProgress(5);
$bar->setMaxSteps(100);
$bar->setProgress(10);
$bar->finish();

rewind($output->getStream());
$this->assertEquals(
rtrim(' 0 [>---------------------------]').
rtrim($this->generateOutput(' 2 [-->-------------------------]')).
rtrim($this->generateOutput(' 5/10 [==============>-------------] 50%')).
rtrim($this->generateOutput(' 10/100 [==>-------------------------] 10%')).
rtrim($this->generateOutput(' 100/100 [============================] 100%')),
stream_get_contents($output->getStream())
);
}

public function testWithSmallScreen()
{
$output = $this->getOutputStream();
Expand Down

0 comments on commit 4cc8cf6

Please sign in to comment.