8000 Fix ProgressBar to correctly clear multi-line formats · symfony/symfony@ca78764 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca78764

Browse files
committed
Fix ProgressBar to correctly clear multi-line formats
1 parent 75ace35 commit ca78764

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,10 @@ private function overwrite(string $message): void
482482
}
483483
$this->output->clear($lineCount);
484484
} else {
485-
if ($this->formatLineCount > 0) {
486-
$this->cursor->moveUp($this->formatLineCount);
485+
for ($i = 0; $i < $this->formatLineCount; ++$i) {
486+
$this->cursor->moveToColumn(1);
487+
$this->cursor->clearLine();
488+
$this->cursor->moveUp();
487489
}
488490

489491
$this->cursor->moveToColumn(1);

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ public function testMultilineFormat()
812812
$this->assertEquals(
813813
">---------------------------\nfoobar".
814814
$this->generateOutput("=========>------------------\nfoobar").
815-
"\x1B[1A\x1B[1G\x1B[2K".
815+
"\x1B[1G\x1B[2K\x1B[1A\x1B[1G\x1B[2K".
816816
$this->generateOutput("============================\nfoobar"),
817817
stream_get_contents($output->getStream())
818818
);
@@ -983,7 +983,7 @@ protected function generateOutput($expected)
983983
{
984984
$count = substr_count($expected, "\n");
985985

986-
return ($count ? sprintf("\x1B[%dA\x1B[1G\x1b[2K", $count) : "\x1B[1G\x1B[2K").$expected;
986+
return ($count ? str_repeat("\x1B[1G\x1b[2K\x1B[1A", $count) : '')."\x1B[1G\x1B[2K".$expected;
987987
}
988988

989989
public function testBarWidthWithMultilineFormat()
@@ -1095,4 +1095,33 @@ public function testNoWriteWhenMessageIsSame()
10951095
stream_get_contents($output->getStream())
10961096
);
10971097
}
1098+
1099+
public function testMultiLineFormatIsFullyCleared()
1100+
{
1101+
$bar = new ProgressBar($output = $this->getOutputStream(), 3);
1102+
$bar->setFormat("%current%/%max%\n%message%\nFoo");
1103+
1104+
$bar->setMessage('1234567890');
1105+
$bar->start();
1106+
$bar->display();
1107+
1108+
$bar->setMessage('ABC');
1109+
$bar->advance();
1110+
$bar->display();
1111+
1112+
$bar->setMessage('A');
1113+
$bar->advance();
1114+
$bar->display();
1115+
1116+
$bar->finish();
1117+
1118+
rewind($output->getStream());
1119+
$this->assertEquals(
1120+
"0/3\n1234567890\nFoo".
1121+
$this->generateOutput("1/3\nABC\nFoo").
1122+
$this->generateOutput("2/3\nA\nFoo").
1123+
$this->generateOutput("3/3\nA\nFoo"),
1124+
stream_get_contents($output->getStream())
1125+
);
1126+
}
10981127
}

0 commit comments

Comments
 (0)
0