Skip to content

Commit 16f806e

Browse files
committed
[Console] Fix for block() padding formatting after symfony#19189
1 parent f4dbd30 commit 16f806e

File tree

3 files changed

+55
-51
lines changed

3 files changed

+55
-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: 53 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,52 @@ 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+
$firstLineIndex = 0;
423+
if ($padding && $this->isDecorated()) {
424+
$firstLineIndex = 1;
425+
array_unshift($lines, '');
426+
$lines[] = '';
427+
}
428+
429+
foreach ($lines as $i => &$line) {
430+
if (null !== $type) {
431+
$line = $firstLineIndex === $i ? $type.$line : $lineIndentation.$line;
432+
}
433+
434+
$line = $prefix.$line;
435+
$line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
436+
437+
if ($style) {
438+
$line = sprintf('<%s>%s</>', $style, $line);
439+
}
440+
}
441+
442+
return $lines;
443+
}
440444
}

0 commit comments

Comments
 (0)