Skip to content

[Console] Extract customizable BaseOutputStyle and reusable MessageBlock #42093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/Symfony/Component/Console/Block/MessageBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Block;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Terminal;

/**
* @author Vadim Zharkov <hushker@gmail.com>
*/
class MessageBlock
{
private const MAX_LINE_LENGTH = 120;

public static function createBlock(
array $messages,
OutputFormatterInterface $formatter,
string $type = null,
string $style = null,
string $prefix = ' ',
bool $padding = false,
bool $escape = false,
int $lineLength = null,
bool $isDecorated = false
): array {
$lineLength = $lineLength ?: self::getTerminalLineLength();
$indentLength = 0;
$prefixLength = Helper::width(Helper::removeDecoration($formatter, $prefix));
$lines = [];

if (null !== $type) {
$type = sprintf('[%s] ', $type);
$indentLength = \strlen($type);
$lineIndentation = str_repeat(' ', $indentLength);
}

// wrap and add newlines for each element
foreach ($messages as $key => $message) {
if ($escape) {
$message = OutputFormatter::escape($message);
}

$decorationLength = Helper::width($message) - Helper::width(
Helper::removeDecoration($formatter, $message)
);
$messageLineLength = min(
$lineLength - $prefixLength - $indentLength + $decorationLength,
$lineLength
);
$messageLines = explode(\PHP_EOL, wordwrap($message, $messageLineLength, \PHP_EOL, true));
foreach ($messageLines as $messageLine) {
$lines[] = $messageLine;
}

if (\count($messages) > 1 && $key < \count($messages) - 1) {
$lines[] = '';
}
}

$firstLineIndex = 0;
if ($padding && $isDecorated) {
$firstLineIndex = 1;
array_unshift($lines, '');
$lines[] = '';
}

foreach ($lines as $i => &$line) {
if (null !== $type) {
$line = $firstLineIndex === $i ? $type.$line : $lineIndentation.$line;
}

$line = $prefix.$line;
$line .= str_repeat(
' ',
max(
$lineLength - Helper::width(
Helper::removeDecoration($formatter, $line)
),
0
)
);

if ($style) {
$line = sprintf('<%s>%s</>', $style, $line);
}
}

return $lines;
}

private static function getTerminalLineLength(): int
{
$width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;

return $width - (int) (\DIRECTORY_SEPARATOR === '\\');
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `TesterTrait::assertCommandIsSuccessful()` to test command
* `SymfonyStyle` is final since Symfony 5.4

5.3
---
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function write($messages, bool $newline = false, int $options = 0);
* @param string|iterable $messages The message as an iterable of strings or a single string
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
*/
public function writeln($messages, int $options = 0);
public function writeln($messages, int $options = self::OUTPUT_NORMAL);

/**
* Sets the verbosity of the output.
Expand Down
Loading