diff --git a/README.md b/README.md index bddcd21f97..d3f5b5588d 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Documentation * Read the [Getting Started guide][7] if you are new to Symfony. * Try the [Symfony Demo application][23] to learn Symfony in practice. +* Discover Symfony ecosystem in detail with [Symfony The Fast Track][26]. * Master Symfony with the [Guides and Tutorials][8], the [Components docs][9] and the [Best Practices][10] reference. @@ -74,3 +75,4 @@ Symfony development is sponsored by [SensioLabs][21], led by the [23]: https://github.com/symfony/symfony-demo [24]: https://symfony.com/coc [25]: https://symfony.com/doc/current/contributing/code_of_conduct/care_team.html +[26]: https://symfony.com/book diff --git a/src/Symfony/Component/Console/Helper/Helper.php b/src/Symfony/Component/Console/Helper/Helper.php index acec994db8..e4f9ca9073 100644 --- a/src/Symfony/Component/Console/Helper/Helper.php +++ b/src/Symfony/Component/Console/Helper/Helper.php @@ -45,6 +45,17 @@ abstract class Helper implements HelperInterface * @return int The length of the string */ public static function strlen(?string $string) + { + return self::width($string); + } + + /** + * Returns the width of a string, using mb_strwidth if it is available. + * The width is how many characters positions the string will use. + * + * @internal in Symfony 5.2 + */ + public static function width(?string $string): int { $string ?? $string = ''; @@ -59,6 +70,27 @@ abstract class Helper implements HelperInterface return mb_strwidth($string, $encoding); } + /** + * Returns the length of a string, using mb_strlen if it is available. + * The length is related to how many bytes the string will use. + * + * @internal in Symfony 5.2 + */ + public static function length(?string $string): int + { + $string ?? $string = ''; + + if (preg_match('//u', $string)) { + return (new UnicodeString($string))->length(); + } + + if (false === $encoding = mb_detect_encoding($string, null, true)) { + return \strlen($string); + } + + return mb_strlen($string, $encoding); + } + /** * Returns the subset of a string, using mb_substr if it is available. * @@ -123,13 +155,7 @@ abstract class Helper implements HelperInterface public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string) { - $string = self::removeDecoration($formatter, $string); - - if (preg_match('//u', $string)) { - return (new UnicodeString($string))->width(true); - } - - return self::strlen($string); + return self::width(self::removeDecoration($formatter, $string)); } public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 66f66061dd..ab705dc641 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -523,7 +523,7 @@ final class ProgressBar $completeBars = $bar->getBarOffset(); $display = str_repeat($bar->getBarCharacter(), $completeBars); if ($completeBars < $bar->getBarWidth()) { - $emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter()); + $emptyBars = $bar->getBarWidth() - $completeBars - Helper::length(Helper::removeDecoration($output->getFormatter(), $bar->getProgressCharacter())); $display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars); } diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 0411454035..ee969bcffb 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -511,7 +511,7 @@ class Table return sprintf($style->getBorderFormat(), str_repeat($style->getBorderChars()[2], $width)); } - $width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell); + $width += Helper::length($cell) - Helper::length(Helper::removeDecoration($this->output->getFormatter(), $cell)); $content = sprintf($style->getCellRowContentFormat(), $cell); $padType = $style->getPadType(); diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 1f512ef411..701b509609 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -899,6 +899,21 @@ And, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whi ); } + public function testUnicode() + { + $bar = new ProgressBar($output = $this->getOutputStream(), 10, 0); + ProgressBar::setFormatDefinition('test', '%current%/%max% [%bar%] %percent:3s%% %message% Fruitcake marzipan toffee. Cupcake gummi bears tart dessert ice cream chupa chups cupcake chocolate bar sesame snaps. Croissant halvah cookie jujubes powder macaroon. Fruitcake bear claw bonbon jelly beans oat cake pie muffin Fruitcake marzipan toffee.'); + $bar->setFormat('test'); + $bar->setProgressCharacter('💧'); + $bar->start(); + rewind($output->getStream()); + $this->assertStringContainsString( + ' 0/10 [💧] 0%', + stream_get_contents($output->getStream()) + ); + $bar->finish(); + } + /** * @dataProvider provideFormat */