8000 bug #19189 [Console] Fix formatting of SymfonyStyle::comment() (chalasr) · symfony/symfony@c278399 · GitHub
[go: up one dir, main page]

Skip to content

Commit c278399

Browse files
committed
bug #19189 [Console] Fix formatting of SymfonyStyle::comment() (chalasr)
This PR was merged into the 2.8 branch. Discussion ---------- [Console] Fix formatting of SymfonyStyle::comment() | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19172 | License | MIT | Doc PR | n/a This: ```php $io->comment('Lorem ipsum dolor sit amet, consectetur adipisicing elit, <comment>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 </comment>cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'); ``` Before outputs: ![](http://image.prntscr.com/image/1d2ea9de42024b53a77120c482be51d4.png) After: ![](http://image.prntscr.com/image/36de23ec14b64804b0cbae7a431185be.png) This moves the lines-cutting logic from `block()` into a specific `createBlock`, used from both `comment()` and `block()`, sort as `comment()` can take messages containing nested tags and outputs a correctly formatted block without escaping tags. Commits ------- 0a53e1d [Console] Fix formatting of SymfonyStyle::comment()
2 parents c68508f + 0a53e1d commit c278399

File tree

9 files changed

+119
-51
lines changed

9 files changed

+119
-51
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
// This service is an alias for the service service_1
2+
 // This service is an alias for the service service_1
33

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
// This service is an alias for the service service_2
2+
 // This service is an alias for the service service_2
33

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

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -66,53 +66,10 @@ public function __construct(InputInterface $input, OutputInterface $output)
6666
*/
6767
public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
6868
{
69-
$this->autoPrependBlock();
7069
$messages = is_array($messages) ? array_values($messages) : array($messages);
71-
$indentLength = 0;
72-
$lines = array();
73-
74-
if (null !== $type) {
75-
$typePrefix = sprintf('[%s] ', $type);
76-
$indentLength = strlen($typePrefix);
77-
$lineIndentation = str_repeat(' ', $indentLength);
78-
}
79-
80-
// wrap and add newlines for each element
81-
foreach ($messages as $key => $message) {
82-
$message = OutputFormatter::escape($message);
83-
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix) - $indentLength, PHP_EOL, true)));
84-
85-
// prefix each line with a number of spaces equivalent to the type length
86-
if (null !== $type) {
87-
foreach ($lines as &$line) {
88-
$line = $lineIndentation === substr($line, 0, $indentLength) ? $line : $lineIndentation.$line;
89-
}
90-
}
91-
92-
if (count($messages) > 1 && $key < count($messages) - 1) {
93-
$lines[] = '';
94-
}
95-
}
9670

97-
if (null !== $type) {
98-
$lines[0] = substr_replace($lines[0], $typePrefix, 0, $indentLength);
99-
}
100-
101-
if ($padding && $this->isDecorated()) {
102-
array_unshift($lines, '');
103-
$lines[] = '';
104-
}
105-
106-
foreach ($lines as &$line) {
107-
$line = sprintf('%s%s', $prefix, $line);
108-
$line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
109-
110-
if ($style) {
111-
$line = sprintf('<%s>%s</>', $style, $line);
112-
}
113-
}
114-
115-
$this->writeln($lines);
71+
$this->autoPrependBlock();
72+
$this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, true));
11673
$this->newLine();
11774
}
11875

@@ -177,11 +134,10 @@ public function text($message)
177134
public function comment($message)
178135
{
179136
$messages = is_array($message) ? array_values($message) : array($message);
180-
foreach ($messages as &$message) {
181-
$message = $this->getFormatter()->format($message);
182-
}
183137

184-
$this->block($messages, null, null, ' // ');
138+
$this->autoPrependBlock();
139+
$this->writeln($this->createBlock($messages, null, null, '<fg=default;bg=default> // </>'));
140+
$this->newLine();
185141
}
186142

187143
/**
@@ -437,4 +393,50 @@ private function reduceBuffer($messages)
437393
return substr($value, -4);
438394
}, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));
439395
}
396+
397+
private function createBlock($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = false)
398+
{
399+
$indentLength = 0;
400+
$prefixLength = Helper::strlenWithoutDecoration($this->getFormatter(), $prefix);
401+
$lines = array();
402+
403+
if (null !== $type) {
404+
$type = sprintf('[%s] ', $type);
405+
$indentLength = strlen($type);
406+
$lineIndentation = str_repeat(' ', $indentLength);
407+
}
408+
409+
// wrap and add newlines for each element
410+
foreach ($messages as $key => $message) {
411+
if ($escape) {
412+
$message = OutputFormatter::escape($message);
413+
}
414+
415+
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, PHP_EOL, true)));
416+
417+
if (count($messages) > 1 && $key < count($messages) - 1) {
418+
$lines[] = '';
419+
}
420+
}
421+
422+
foreach ($lines as $i => &$line) {
423+
if (null !== $type) {
424+
$line = 0 === $i ? $type.$line : $lineIndentation.$line;
425+
}
426+
427+
$line = $prefix.$line;
428+
$line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
429+
430+
if ($style) {
431+
$line = sprintf('<%s>%s</>', $style, $line);
432+
}
433+
}
434+
435+
if ($padding && $this->isDecorated()) {
436+
array_unshift($lines, '');
437+
$lines[] = '';
438+
}
439+
440+
return $lines;
441+
}
440442
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
use Symfony\Component\Console\Style\SymfonyStyle;
7+
8+
// ensure that nested tags have no effect on the color of the '//' prefix
9+
return function (InputInterface $input, OutputInterface $output) {
10+
$output->setDecorated(true);
11+
$output = new SymfonyStyle($input, $output);
12+
$output->comment(
13+
'Lorem ipsum dolor sit <comment>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.</comment> Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum'
14+
);
15+
};
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 block() behaves properly with a prefix and without type
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+
null,
13+
null,
14+
'$ ',
15+
true
16+
);
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 block() behaves properly with a type and without prefix
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+
'TEST'
13+
);
14+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
 // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 
3+
 // dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea 
4+
 // commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
5+
 // pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
6+
 // id est laborum
7+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
$ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
3+
$ aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
4+
$ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
5+
$ occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
[TEST] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
3+
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
4+
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
5+
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
6+
laborum
7+

0 commit comments

Comments
 (0)
0