diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 8212bedbf804..5ac8640689bc 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -603,13 +603,19 @@ private function buildLine() }; $line = preg_replace_callback($regex, $callback, $this->format); - $lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line); + // gets string length for each sub line with multiline format + $linesLength = array_map(function ($subLine) { + return Helper::strlenWithoutDecoration($this->output->getFormatter(), rtrim($subLine, "\r")); + }, explode("\n", $line)); + + $linesWidth = max($linesLength); + $terminalWidth = $this->terminal->getWidth(); - if ($lineLength <= $terminalWidth) { + if ($linesWidth <= $terminalWidth) { return $line; } - $this->setBarWidth($this->barWidth - $lineLength + $terminalWidth); + $this->setBarWidth($this->barWidth - $linesWidth + $terminalWidth); return preg_replace_callback($regex, $callback, $this->format); } diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 64ad4338ad2a..f45016982e29 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -754,4 +754,22 @@ protected function generateOutput($expected) return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected; } + + public function testBarWidthWithMultilineFormat() + { + putenv('COLUMNS=10'); + + $bar = new ProgressBar($output = $this->getOutputStream()); + $bar->setFormat("%bar%\n0123456789"); + + // before starting + $bar->setBarWidth(5); + $this->assertEquals(5, $bar->getBarWidth()); + + // after starting + $bar->start(); + rewind($output->getStream()); + $this->assertEquals(5, $bar->getBarWidth(), stream_get_contents($output->getStream())); + putenv('COLUMNS=120'); + } }