8000 bug #18879 [Console] SymfonyStyle: Align multi-line/very-long-line bl… · symfony/symfony@71863ea · GitHub
[go: up one dir, main page]

Skip to content

Commit 71863ea

Browse files
committed
bug #18879 [Console] SymfonyStyle: Align multi-line/very-long-line blocks (chalasr)
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #18879). Discussion ---------- [Console] SymfonyStyle: Align multi-line/very-long-line blocks | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18564 | License | MIT | Doc PR | n/a This PR makes all lines aligned in multi-line blocks. Very-long-line block: ```php SymfonyStyle::warning('Lorem ipsum...'); ``` Before: ![before-1](http://image.prntscr.com/image/d8443d3a85924a0182a62bd6d3dc1086.png) After: ![after-1](http://image.prntscr.com/image/dbbdd275bff140bdad06de336f032ec1.png) Multi-line block: ```php SymfonyStyle::success(['Lorem ipsum...', 'Lorem ipsum...', 'Lorem ipsum...']); ``` Before: ![before-2](http://image.prntscr.com/image/6d7c05b4ab3a42f0b0be652527aed7c8.png) After: ![after-2](http://image.prntscr.com/image/bba017309f4a4dd09e0147d5917cb0ae.png) Also @javiereguiluz pointed the case of `SymfonyStyle::comment()` in #18564, I needed to make it calling `SymfonyStyle::block()` with ` // ` as prefix to fit the first intention of this one. So if this one is merged I'll propose the changes for comments in a second PR (out of this scope). Commits ------- 963fe1d [Console] SymfonyStyle: Align multi-line/very-long-line blocks
2 parents 5dcbbd0 + 963fe1d commit 71863ea

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)
0