Skip to content

Commit 03c1c23

Browse files
committed
[Console] Fix formatting of SymfonyStyle::comment()
Adapt frameworkbundle test
1 parent 8ec92f7 commit 03c1c23

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11

2-
// This service is an alias for the service service_1
3-
2+
 // This service is an alias for the service service_1
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11

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

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

Lines changed: 57 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> // </>'));
140+
$this->newLine();
185141
}
186142

187143
/**
@@ -437,4 +393,56 @@ 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+
$lines = array();
401+
402+
if (null !== $type) {
403+
$typePrefix = sprintf('[%s] ', $type);
404+
$indentLength = strlen($typePrefix);
405+
$lineIndentation = str_repeat(' ', $indentLength);
406+
}
407+
408+
// wrap and add newlines for each element
409+
foreach ($messages as $key => $message) {
410+
if ($escape) {
411+
$message = OutputFormatter::escape($message);
412+
}
413+
414+
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen(strip_tags($prefix)) - $indentLength, PHP_EOL, true)));
415+
416+
// prefix each line with a number of spaces equivalent to the type length
417+
if (null !== $type) {
418+
foreach ($lines as &$line) {
419+
$line = $lineIndentation === substr($line, 0, $indentLength) ? $line : $lineIndentation.$line;
420+
}
421+
}
422+
423+
if (count($messages) > 1 && $key < count($messages) - 1) {
424+
$lines[] = '';
425+
}
426+
}
427+
428+
if (null !== $type) {
429+
$lines[0] = substr_replace($lines[0], $typePrefix, 0, $indentLength);
430+
}
431+
432+
if ($padding && $this->isDecorated()) {
433+
array_unshift($lines, '');
434+
$lines[] = '';
435+
}
436+
437+
foreach ($lines as &$line) {
438+
$line = sprintf('%s%s', $prefix, $line);
439+
$line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
440+
441+
if ($style) {
442+
$line = sprintf('<%s>%s</>', $style, $line);
443+
}
444+
}
445+
446+
return $lines;
447+
}
440448
}

0 commit comments

Comments
 (0)