8000 [Console] SymfonyStyle: Align multi-line/very-long-line blocks by chalasr · Pull Request #18879 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] SymfonyStyle: Align multi-line/very-long-line blocks #18879

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

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 16 additions & 3 deletions src/Symfony/Component/Console/Style/SymfonyStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,36 @@ public function block($messages, $type = null, $style = null, $prefix = ' ', $pa
{
$this->autoPrependBlock();
$messages = is_array($messages) ? array_values($messages) : array($messages);
$indentLength = 0;
$lines = array();

// add type
if (null !== $type) {
$messages[0] = sprintf('[%s] %s', $type, $messages[0]);
$typePrefix = sprintf('[%s] ', $type);
$indentLength = strlen($typePrefix);
$lineIndentation = str_repeat(' ', $indentLength);
}

// wrap and add newlines for each element
foreach ($messages as $key => $message) {
$message = OutputFormatter::escape($message);
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix), PHP_EOL, true)));
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix) - $indentLength, PHP_EOL, true)));

// prefix each line with a number of spaces equivalent to the type length
if (null !== $type) {
foreach ($lines as &$line) {
$line = $lineIndentation === substr($line, 0, $indentLength) ? $line : $lineIndentation.$line;
}
}

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

if (null !== $type) {
$lines[0] = substr_replace($lines[0], $typePrefix, 0, $indentLength);
}

if ($padding && $this->isDecorated()) {
array_unshift($lines, '');
$lines[] = '';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;

//Ensure that all lines are aligned to the begin of the first line in a very long line block
return function (InputInterface $input, OutputInterface $output) {
$output = new SymfonyStyleWithForcedLineLength($input, $output);
$output->block(
'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',
'CUSTOM',
'fg=white;bg=green',
'X ',
true
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;

//Ensure that all lines are aligned to the begin of the first line in a multi-line block
return function (InputInterface $input, OutputInterface $output) {
$output = new SymfonyStyleWithForcedLineLength($input, $output);
$output->block(['Custom block', 'Second custom block line'], 'CUSTOM', 'fg=white;bg=green', 'X ', true);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

X [CUSTOM] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
X dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
X commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
X nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
X anim id est laborum

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

X [CUSTOM] Custom block
X
X Second custom block line

2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function inputCommandToOutputFilesProvider()

public function testLongWordsBlockWrapping()
{
$word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon';
$word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygovgollhjvhvljfezefeqifzeiqgiqzhrsdgihqzridghqridghqirshdghdghieridgheirhsdgehrsdvhqrsidhqshdgihrsidvqhneriqsdvjzergetsrfhgrstsfhsetsfhesrhdgtesfhbzrtfbrztvetbsdfbrsdfbrn';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, this change should not have been necessary for the test to pass. So I suggest to see #18896

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank's for having made the change @ogizanagi

$wordLength = strlen($word);
$maxLineLength = SymfonyStyle::MAX_LINE_LENGTH - 3;

Expand Down
0