Skip to content

Commit

Permalink
bug #21958 [Console] Fix bar width with multilines ProgressBar's form…
Browse files Browse the repository at this point in the history
…at (maidmaid)

This PR was squashed before being merged into the 3.2 branch (closes #21958).

Discussion
----------

[Console] Fix bar width with multilines ProgressBar's format

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

The bar width is badly recalculated when we use multilines (``\n``) and bar placeholer (``%bar%``) in ProgressBar's format. The bar width is reduced because multilines is considered as a big oneline. This PR fixes this.

Commits
-------

3d58ab5 [Console] Fix bar width with multilines ProgressBar's format
  • Loading branch information
fabpot committed Apr 25, 2017
2 parents 8fcc9e6 + 3d58ab5 commit 2145f56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Expand Up @@ -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');
}
}

0 comments on commit 2145f56

Please sign in to comment.