Skip to content

Commit 336b086

Browse files
committed
[Console] Fix formatting of SymfonyStyle::comment()
Remove decoration from frameworkbundle test (avoid testing the Console behaviour) Set background to default
1 parent 8ec92f7 commit 336b086

File tree

4 files changed

+60
-55
lines changed

4 files changed

+60
-55
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ protected function describeContainerDefinition(Definition $definition, array $op
339339
*/
340340
protected function describeContainerAlias(Alias $alias, array $options = array())
341341
{
342+
$options['output']->setDecorated(false);
342343
$options['output']->comment(sprintf('This service is an alias for the service <info>%s</info>', (string) $alias));
343344
}
344345

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
2-
// This service is an alias for the service service_1
3-
1+
// This service is an alias for the service service_1
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
2-
// This service is an alias for the service service_2
3-
1+
// 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;bg=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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy