8000 [Console] added support for multiline formats in ProgressBar · symfony/symfony@7a30e50 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a30e50

Browse files
committed
[Console] added support for multiline formats in ProgressBar
1 parent 1aa7b8c commit 7a30e50

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ProgressBar
4848
private $percent;
4949
private $lastMessagesLength;
5050
private $barCharOriginal;
51+
private $formatLineCount;
5152

5253
static private $formatters;
5354

@@ -224,6 +225,7 @@ public function getProgressCharacter()
224225
public function setFormat($format)
225226
{
226227
$this->format = $format;
228+
$this->formatLineCount = substr_count($format, "\n");
227229
}
228230

229231
/**
@@ -248,7 +250,7 @@ public function start()
248250
$this->barCharOriginal = '';
249251

250252
if (null === $this->format) {
251-
$this->format = $this->determineBestFormat();
253+
$this->setFormat($this->determineBestFormat());
252254
}
253255

254256
if (!$this->max) {
@@ -351,7 +353,7 @@ public function display()
351353
*/
352354
public function clear()
353355
{
354-
$this->overwrite('');
356+
$this->overwrite(str_repeat("\n", $this->formatLineCount));
355357
}
356358

357359
/**
@@ -364,12 +366,17 @@ private function overwrite($message)
364366
$length = Helper::strlen($message);
365367

366368
// append whitespace to match the last line's length
369+
// FIXME: on each line!!!!!
370+
// FIXME: max of each line for lastMessagesLength or an array?
367371
if (null !== $this->lastMessagesLength && $this->lastMessagesLength > $length) {
368372
$message = str_pad($message, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT);
369373
}
370374

371-
// carriage return
375+
// move back to the beginning of the progress bar before redrawing it
372376
$this->output->write("\x0D");
377+
if ($this->formatLineCount) {
378+
$this->output->write(sprintf("\033[%dA", $this->formatLineCount));
379+
}
373380
$this->output->write($message);
374381

375382
$this->lastMessagesLength = Helper::strlen($message);

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,26 @@ public function testAddingPlaceholderFormatter()
319319
);
320320
}
321321

322+
public function testMultilineFormat()
323+
{
324+
$bar = new ProgressBar($output = $this->getOutputStream(), 3);
325+
$bar->setFormat("%bar%\nfoobar");
326+
327+
$bar->start();
328+
$bar->advance();
329+
$bar->clear();
330+
$bar->finish();
331+
332+
rewind($output->getStream());
333+
$this->assertEquals(
334+
$this->generateOutput(">---------------- 8000 -----------\nfoobar").
335+
$this->generateOutput("=========>------------------\nfoobar").
336+
$this->generateOutput("\n").
337+
$this->generateOutput("============================\nfoobar"),
338+
stream_get_contents($output->getStream())
339+
);
340+
}
341+
322342
protected function getOutputStream($decorated = true)
323343
{
324344
return new StreamOutput(fopen('php://memory', 'r+', false), StreamOutput::VERBOSITY_NORMAL, $decorated);
@@ -334,6 +354,8 @@ protected function generateOutput($expected)
334354

335355
$this->lastMessagesLength = strlen($expectedout);
336356

337-
return "\x0D".$expectedout;
357+
$count = substr_count($expected, "\n");
358+
359+
return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expectedout;
338360
}
339361
}

0 commit comments

Comments
 (0)
0