Skip to content

Commit c77f4a4

Browse files
committed
merged branch mpartel/progresshelper-clear (PR #8074)
This PR was merged into the master branch. Discussion ---------- [Console] Add clear() to ProgressHelper. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | symfony/symfony-docs#2642 One may want to print something else while the progress bar is running. The output will be messy if the progress bar is not removed first. One may also want to remove the progress bar after the work is complete. Commits ------- 29c71a5 [Console] Add clear() to ProgressHelper.
2 parents b044be5 + f5c335f commit c77f4a4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Helper/ProgressHelper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,18 @@ public function display($finish = false)
289289
$this->overwrite($this->output, $message);
290290
}
291291

292+
/**
293+
* Removes the progress bar from the current line.
294+
*
295+
* This is useful if you wish to write some output
296+
* while a progress bar is running.
297+
* Call display() to show the progress bar again.
298+
*/
299+
public function clear()
300+
{
301+
$this->overwrite($this->output, '');
302+
}
303+
292304
/**
293305
* Finishes the progress output.
294306
*/

Tests/Helper/ProgressHelperTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ public function testMultiByteSupport()
151151
$this->assertEquals($this->generateOutput(' 3 [■■■>------------------------]'), stream_get_contents($output->getStream()));
152152
}
153153

154+
public function testClear()
155+
{
156+
$progress = new ProgressHelper();
157+
$progress->start($output = $this->getOutputStream(), 50);
158+
$progress->setCurrent(25);
159+
$progress->clear();
160+
161+
rewind($output->getStream());
162+
$this->assertEquals(
163+
$this->generateOutput(' 25/50 [==============>-------------] 50%') . $this->generateOutput(''),
164+
stream_get_contents($output->getStream())
165+
);
166+
}
167+
154168
protected function getOutputStream()
155169
{
156170
return new StreamOutput(fopen('php://memory', 'r+', false));

0 commit comments

Comments
 (0)