Skip to content

[Console] Add Helper::width() and Helper::length() #40698

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

Merged
merged 2 commits into from
Apr 8, 2021
Merged
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
40 changes: 33 additions & 7 deletions src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public function getHelperSet()
* @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 = '';

Expand All @@ -59,6 +70,27 @@ public static function strlen(?string $string)
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.
*
Expand Down Expand Up @@ -123,13 +155,7 @@ public static function formatMemory(int $memory)

public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string)
{
$string = self::removeDecoration($formatter, $string);

if (preg_match('//u', $string)) {
return (new UnicodeString($string))->width(true);
Copy link
Member Author

Choose a reason for hiding this comment

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

The true here is for "remove ansi decoration", but that is already done in self::removeDecoration.

}

return self::strlen($string);
return self::width(self::removeDecoration($formatter, $string));
}

public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string)
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ private static function initPlaceholderFormatters(): array
$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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ private function renderCell(array $row, int $column, string $cellFormat): string
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();
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,21 @@ public function testSetFormat()
);
}

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
*/
Expand Down