Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit acb5528

Browse filesBrowse files
committed
bug #14739 [Console] SymfonyStyle : fix blocks wordwrapping (ogizanagi)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] SymfonyStyle : fix blocks wordwrapping | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Allow to print strings with a length greater than the terminal length in a block by allowing to cut words. Indeed, currently such code won't work: ```php //Terminal is 80 chars wide $sfStyle->caution(str_repeat('#', 78)); //Caution uses 3 chars as prefix (' ! ') ``` > [Symfony\Component\Debug\Exception\ContextErrorException] Warning: str_repeat(): Second argument has to be greater than or equal to 0 Of course, this is only a foolish example. But there is no reason the command should fail when trying to output a long word (as we could imagine it could be a FQCN, absolute path or anything else), nor just because the terminal width is too small. Commits ------- 58f2fad [Console] SymfonyStyle : Fix blocks wordwrapping
2 parents e09874b + 58f2fad commit acb5528
Copy full SHA for acb5528

File tree

Expand file treeCollapse file tree

2 files changed

+20
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+20
-1
lines changed

‎src/Symfony/Component/Console/Style/SymfonyStyle.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Style/SymfonyStyle.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function block($messages, $type = null, $style = null, $prefix = ' ', $pa
7676
// wrap and add newlines for each element
7777
foreach ($messages as $key => $message) {
7878
$message = OutputFormatter::escape($message);
79-
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix), PHP_EOL)));
79+
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix), PHP_EOL, true)));
8080

8181
if (count($messages) > 1 && $key < count($messages) - 1) {
8282
$lines[] = '';

‎src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PHPUnit_Framework_TestCase;
66
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\Console\Style\SymfonyStyle;
710
use Symfony\Component\Console\Tester\CommandTester;
811

912
class SymfonyStyleTest extends PHPUnit_Framework_TestCase
@@ -42,4 +45,20 @@ public function inputCommandToOutputFilesProvider()
4245

4346
return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
4447
}
48+
49+
public function testLongWordsBlockWrapping()
50+
{
51+
$word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon';
52+
$wordLength = strlen($word);
53+
$maxLineLength = SymfonyStyle::MAX_LINE_LENGTH - 3;
54+
55+
$this->command->setCode(function (InputInterface $input, OutputInterface $output) use ($word) {
56+
$sfStyle = new SymfonyStyle($input, $output);
57+
$sfStyle->block($word, 'CUSTOM', 'fg=white;bg=blue', ' § ', false);
58+
});
59+
60+
$this->tester->execute(array(), array('interactive' => false, 'decorated' => false));
61+
$expectedCount = (int) ceil($wordLength / ($maxLineLength)) + (int) ($wordLength > $maxLineLength - 5);
62+
$this->assertSame($expectedCount, substr_count($this->tester->getDisplay(true), ' § '));
63+
}
4564
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.