Skip to content

Commit 963fe1d

Browse files
chalasrfabpot
authored andcommitted
[Console] SymfonyStyle: Align multi-line/very-long-line blocks
Remove SymfonyStyle::comment() changes (out of scope) CS Fixes Add tests
1 parent 5dcbbd0 commit 963fe1d

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,36 @@ public function block($messages, $type = null, $style = null, $prefix = ' ', $pa
6767
{
6868
$this->autoPrependBlock();
6969
$messages = is_array($messages) ? array_values($messages) : array($messages);
70+
$indentLength = 0;
7071
$lines = array();
7172

72-
// add type
7373
if (null !== $type) {
74-
$messages[0] = sprintf('[%s] %s', $type, $messages[0]);
74+
$typePrefix = sprintf('[%s] ', $type);
75+
$indentLength = strlen($typePrefix);
76+
$lineIndentation = str_repeat(' ', $indentLength);
7577
}
7678

7779
// wrap and add newlines for each element
7880
foreach ($messages as $key => $message) {
7981
$message = OutputFormatter::escape($message);
80-
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix), PHP_EOL, true)));
82+
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix) - $indentLength, PHP_EOL, true)));
83+
84+
// prefix each line with a number of spaces equivalent to the type length
85+
if (null !== $type) {
86+
foreach ($lines as &$line) {
87+
$line = $lineIndentation === substr($line, 0, $indentLength) ? $line : $lineIndentation.$line;
88+
}
89+
}
8190

8291
if (count($messages) > 1 && $key < count($messages) - 1) {
8392
$lines[] = '';
8493
}
8594
}
8695

96+
if (null !== $type) {
97+
$lines[0] = substr_replace($lines[0], $typePrefix, 0, $indentLength);
98+
}
99+
87100
if ($padding && $this->isDecorated()) {
88101
array_unshift($lines, '');
89102
$lines[] = '';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Input\InputInterface;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
6+
7+
//Ensure that all lines are aligned to the begin of the first line in a very long line block
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output = new SymfonyStyleWithForcedLineLength($input, $output);
10+
$output->block(
11+
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum',
12+
'CUSTOM',
13+
'fg=white;bg=green',
14+
'X ',
15+
true
16+
);
17+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Input\InputInterface;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
6+
7+
//Ensure that all lines are aligned to the begin of the first line in a multi-line block
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output = new SymfonyStyleWithForcedLineLength($input, $output);
10+
$output->block(['Custom block', 'Second custom block line'], 'CUSTOM', 'fg=white;bg=green', 'X ', true);
11+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
X [CUSTOM] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
3+
X dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
4+
X commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
5+
X nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
6+
X anim id est laborum
7+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
X [CUSTOM] Custom block
3+
X
4+
X Second custom block line
5+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function inputCommandToOutputFilesProvider()
5757

5858
public function testLongWordsBlockWrapping()
5959
{
60-
$word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon';
60+
$word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygovgollhjvhvljfezefeqifzeiqgiqzhrsdgihqzridghqridghqirshdghdghieridgheirhsdgehrsdvhqrsidhqshdgihrsidvqhneriqsdvjzergetsrfhgrstsfhsetsfhesrhdgtesfhbzrtfbrztvetbsdfbrsdfbrn';
6161
$wordLength = strlen($word);
6262
$maxLineLength = SymfonyStyle::MAX_LINE_LENGTH - 3;
6363

0 commit comments

Comments
 (0)