8000 [Console] SymfonyStyle : Fix blocks wordwrapping · symfony/symfony@58f2fad · GitHub
[go: up one dir, main page]

Skip to content

Commit 58f2fad

Browse files
ogizanagimaxime.steinhausser
authored andcommitted
[Console] SymfonyStyle : Fix blocks wordwrapping
Allow to print strings with a length greater than the terminal length in a block by allowing to cut words.
1 parent e09874b commit 58f2fad

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines 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

Lines 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