-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #47062 [Console] Don't cut Urls wrapped in SymfonyStyle block…
… (fchris82, GromNaN) This PR was merged into the 6.2 branch. Discussion ---------- [Console] Don't cut Urls wrapped in SymfonyStyle block | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #30920 | License | MIT | Doc PR | symfony/symfony-docs#16476 Continuation of #45318 by fchris82 Commits ------- b317f06 Remove OutputWrapperInterface 2bc2571 Console output formatter improvement
- Loading branch information
Showing
9 changed files
with
194 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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\Helper; | ||
|
||
/** | ||
* Simple output wrapper for "tagged outputs" instead of wordwrap(). This solution is based on a StackOverflow | ||
* answer: https://stackoverflow.com/a/20434776/1476819 from user557597 (alias SLN). | ||
* | ||
* (?: | ||
* # -- Words/Characters | ||
* ( # (1 start) | ||
* (?> # Atomic Group - Match words with valid breaks | ||
* .{1,16} # 1-N characters | ||
* # Followed by one of 4 prioritized, non-linebreak whitespace | ||
* (?: # break types: | ||
* (?<= [^\S\r\n] ) # 1. - Behind a non-linebreak whitespace | ||
* [^\S\r\n]? # ( optionally accept an extra non-linebreak whitespace ) | ||
* | (?= \r? \n ) # 2. - Ahead a linebreak | ||
* | $ # 3. - EOS | ||
* | [^\S\r\n] # 4. - Accept an extra non-linebreak whitespace | ||
* ) | ||
* ) # End atomic group | ||
* | | ||
* .{1,16} # No valid word breaks, just break on the N'th character | ||
* ) # (1 end) | ||
* (?: \r? \n )? # Optional linebreak after Words/Characters | ||
* | | ||
* # -- Or, Linebreak | ||
* (?: \r? \n | $ ) # Stand alone linebreak or at EOS | ||
* ) | ||
* | ||
* @author Krisztián Ferenczi <ferenczi.krisztian@gmail.com> | ||
* | ||
* @see https://stackoverflow.com/a/20434776/1476819 | ||
*/ | ||
final class OutputWrapper | ||
{ | ||
private const TAG_OPEN_REGEX_SEGMENT = '[a-z](?:[^\\\\<>]*+ | \\\\.)*'; | ||
private const TAG_CLOSE_REGEX_SEGMENT = '[a-z][^<>]*+'; | ||
private const URL_PATTERN = 'https?://\S+'; | ||
|
||
public function __construct( | ||
private bool $allowCutUrls = false | ||
) { | ||
} | ||
|
||
public function wrap(string $text, int $width, string $break = "\n"): string | ||
{ | ||
if (!$width) { | ||
return $text; | ||
} | ||
|
||
$tagPattern = sprintf('<(?:(?:%s)|/(?:%s)?)>', self::TAG_OPEN_REGEX_SEGMENT, self::TAG_CLOSE_REGEX_SEGMENT); | ||
$limitPattern = "{1,$width}"; | ||
$patternBlocks = [$tagPattern]; | ||
if (!$this->allowCutUrls) { | ||
$patternBlocks[] = self::URL_PATTERN; | ||
} | ||
$patternBlocks[] = '.'; | ||
$blocks = implode('|', $patternBlocks); | ||
$rowPattern = "(?:$blocks)$limitPattern"; | ||
$pattern = sprintf('#(?:((?>(%1$s)((?<=[^\S\r\n])[^\S\r\n]?|(?=\r?\n)|$|[^\S\r\n]))|(%1$s))(?:\r?\n)?|(?:\r?\n|$))#imux', $rowPattern); | ||
$output = rtrim(preg_replace($pattern, '\\1'.$break, $text), $break); | ||
|
||
return str_replace(' '.$break, $break, $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_22.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
// ensure that nested tags have no effect on the color of the '//' prefix | ||
return function (InputInterface $input, OutputInterface $output) { | ||
$output->setDecorated(true); | ||
$output = new SymfonyStyle($input, $output); | ||
$output->block( | ||
'Árvíztűrőtükörfúrógép Lorem ipsum dolor sit <comment>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.</comment> Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum', | ||
'★', // UTF-8 star! | ||
null, | ||
'<fg=default;bg=default> ║ </>', // UTF-8 double line! | ||
false, | ||
false | ||
); | ||
}; |
10 changes: 5 additions & 5 deletions
10
src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
|
||
[39;49m // [39;49mLorem ipsum dolor sit [33mamet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore [39m | ||
[39;49m // [39;49m[33mmagna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo [39m | ||
[39;49m // [39;49m[33mconsequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla [39m | ||
[39;49m // [39;49m[33mpariatur.[39m Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id | ||
[39;49m // [39;49mest laborum | ||
[39;49m // [39;49mÁrvíztűrőtükörfúrógép 🎼 Lorem ipsum dolor sit [33m💕 amet, consectetur adipisicing elit, sed do eiusmod tempor incididu[39m | ||
[39;49m // [39;49m[33mlabore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex[39m | ||
[39;49m // [39;49m[33mea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla [39m | ||
[39;49m // [39;49m[33mpariatur.[39m Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est | ||
[39;49m // [39;49mlaborum | ||
|
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_22.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
[39;49m ║ [39;49m[★] Árvíztűrőtükörfúrógép Lorem ipsum dolor sit [33mamet, consectetur adipisicing elit, sed do eiusmod tempor incididunt [39m | ||
[39;49m ║ [39;49m[33m ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut [39m | ||
[39;49m ║ [39;49m[33m aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu [39m | ||
[39;49m ║ [39;49m[33m fugiat nulla pariatur.[39m Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit | ||
[39;49m ║ [39;49m anim id est laborum | ||
|
73 changes: 73 additions & 0 deletions
73
src/Symfony/Component/Console/Tests/Helper/OutputWrapperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?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\Tests\Helper; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Helper\OutputWrapper; | ||
|
||
class OutputWrapperTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider textProvider | ||
*/ | ||
public function testBasicWrap(string $text, int $width, bool $allowCutUrls, string $expected) | ||
{ | ||
$wrapper = new OutputWrapper($allowCutUrls); | ||
$result = $wrapper->wrap($text, $width); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public static function textProvider(): iterable | ||
{ | ||
$baseTextWithUtf8AndUrl = 'Árvíztűrőtükörfúrógép https://github.com/symfony/symfony Lorem ipsum <comment>dolor</comment> sit amet, consectetur adipiscing elit. Praesent vestibulum nulla quis urna maximus porttitor. Donec ullamcorper risus at <error>libero ornare</error> efficitur.'; | ||
|
||
yield 'Default URL cut' => [ | ||
$baseTextWithUtf8AndUrl, | ||
20, | ||
false, | ||
<<<'EOS' | ||
Árvíztűrőtükörfúrógé | ||
p https://github.com/symfony/symfony Lorem ipsum | ||
<comment>dolor</comment> sit amet, | ||
consectetur | ||
adipiscing elit. | ||
Praesent vestibulum | ||
nulla quis urna | ||
maximus porttitor. | ||
Donec ullamcorper | ||
risus at <error>libero | ||
ornare</error> efficitur. | ||
EOS, | ||
]; | ||
|
||
yield 'Allow URL cut' => [ | ||
$baseTextWithUtf8AndUrl, | ||
20, | ||
true, | ||
<<<'EOS' | ||
Árvíztűrőtükörfúrógé | ||
p | ||
https://github.com/s | ||
ymfony/symfony Lorem | ||
ipsum <comment>dolor</comment> sit | ||
amet, consectetur | ||
adipiscing elit. | ||
Praesent vestibulum | ||
nulla quis urna | ||
maximus porttitor. | ||
Donec ullamcorper | ||
risus at <error>libero | ||
ornare</error> efficitur. | ||
EOS, | ||
]; | ||
} | ||
} |