8000 bug #40698 [Console] Add Helper::width() and Helper::length() (Nyholm… · symfony/symfony@74056a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74056a7

Browse files
committed
bug #40698 [Console] Add Helper::width() and Helper::length() (Nyholm, grasmash)
This PR was merged into the 5.2 branch. Discussion ---------- [Console] Add Helper::width() and Helper::length() | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Close #40697 Fix #40634, fix #40635 | License | MIT | Doc PR | This PR will add add a Helper::strwidth() and a Helper::strlength(). Same with with the Helper::strlenWithoutDecoration(). It does not deprecate anything. That is done in #40695 With this PR we dont have to revert the emoji issue (ie close #40697) FYI @grasmash, I used your tests from #40635 Commits ------- d9ea4c5 Add test. dc02ab3 [Console] Add Helper::strwidth() and Helper::strlength()
2 parents f2de7d5 + d9ea4c5 commit 74056a7

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

src/Symfony/Component/Console/Helper/Helper.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public function getHelperSet()
4545
* @return int The length of the string
4646
*/
4747
public static function strlen(?string $string)
48+
{
49+
return self::width($string);
50+
}
51+
52+
/**
53+
* Returns the width of a string, using mb_strwidth if it is available.
54+
* The width is how many characters positions the string will use.
55+
*
56+
* @internal in Symfony 5.2
57+
*/
58+
public static function width(?string $string): int
4859
{
4960
$string ?? $string = '';
5061

@@ -59,6 +70,27 @@ public static function strlen(?string $string)
5970
return mb_strwidth($string, $encoding);
6071
}
6172

73+
/**
74+
* Returns the length of a string, using mb_strlen if it is available.
75+
* The length is related to how many bytes the string will use.
76+
*
77+
* @internal in Symfony 5.2
78+
*/
79+
public static function length(?string $string): int
80+
{
81+
$string ?? $string = '';
82+
83+
if (preg_match('//u', $string)) {
84+
return (new UnicodeString($string))->length();
85+
}
86+
87+
if (false === $encoding = mb_detect_encoding($string, null, true)) {
88+
return \strlen($string);
89+
}
90+
91+
return mb_strlen($string, $encoding);
92+
}
93+
6294
/**
6395
* Returns the subset of a string, using mb_substr if it is available.
6496
*
@@ -123,13 +155,7 @@ public static function formatMemory(int $memory)
123155

124156
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string)
125157
{
126-
$string = self::removeDecoration($formatter, $string);
127-
128-
if (preg_match('//u', $string)) {
129-
return (new UnicodeString($string))->width(true);
130-
}
131-
132-
return self::strlen($string);
158+
return self::width(self::removeDecoration($formatter, $string));
133159
}
134160

135161
public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string)

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ private static function initPlaceholderFormatters(): array
513513
$completeBars = $bar->getBarOffset();
514514
$display = str_repeat($bar->getBarCharacter(), $completeBars);
515515
if ($completeBars < $bar->getBarWidth()) {
516-
$emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter());
516+
$emptyBars = $bar->getBarWidth() - $completeBars - Helper::length(Helper::removeDecoration($output->getFormatter(), $bar->getProgressCharacter()));
517517
$display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars);
518518
}
519519

src/Symfony/Component/Console/Helper/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ private function renderCell(array $row, int $column, string $cellFormat): string
511511
return sprintf($style->getBorderFormat(), str_repeat($style->getBorderChars()[2], $width));
512512
}
513513

514-
$width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
514+
$width += Helper::length($cell) - Helper::length(Helper::removeDecoration($this->output->getFormatter(), $cell));
515515
$content = sprintf($style->getCellRowContentFormat(), $cell);
516516

517517
$padType = $style->getPadType();

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,21 @@ public function testSetFormat()
899899
);
900900
}
901901

902+
public function testUnicode()
903+
{
904+
$bar = new ProgressBar($output = $this->getOutputStream(), 10, 0);
905+
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.');
906+
$bar->setFormat('test');
907+
$bar->setProgressCharacter('💧');
908+
$bar->start();
909+
rewind($output->getStream());
910+
$this->assertStringContainsString(
911+
' 0/10 [💧] 0%',
912+
stream_get_contents($output->getStream())
913+
);
914+
$bar->finish();
915+
}
916+
902917
/**
903918
* @dataProvider provideFormat
904919
*/

0 commit comments

Comments
 (0)
0