Skip to content
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

support full-width unicode characters #274

Merged
merged 1 commit into from
Aug 2, 2023
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
2 changes: 1 addition & 1 deletion src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private function extractShortcut(string $title) : ?string
return null;
}

if (mb_strlen($match[1]) > 1) {
if (mb_strwidth($match[1]) > 1) {
throw InvalidShortcutException::fromShortcut($match[1]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false)
$invertedColoursSetCode,
str_repeat(' ', $this->style->getPaddingLeftRight()),
$row,
str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),
str_repeat(' ', $this->style->getRightHandPadding(mb_strwidth(s::stripAnsiEscapeSequence($row)))),
$invertedColoursUnsetCode,
$borderColour,
str_repeat(' ', $this->style->getBorderRightWidth()),
Expand Down
8 changes: 4 additions & 4 deletions src/Dialogue/CancellableConfirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ private function drawDialog(string $confirmText = 'OK', string $cancelText = 'Ca

$this->terminal->moveCursorToRow($this->y);

$promptWidth = mb_strlen($this->text) + 4;
$promptWidth = mb_strwidth($this->text) + 4;

$buttonLength = mb_strlen($confirmText) + 6;
$buttonLength += mb_strlen($cancelText) + 7;
$buttonLength = mb_strwidth($confirmText) + 6;
$buttonLength += mb_strwidth($cancelText) + 7;

$confirmButton = sprintf(
'%s%s < %s > %s%s',
Expand Down Expand Up @@ -78,7 +78,7 @@ private function drawDialog(string $confirmText = 'OK', string $cancelText = 'Ca
$this->text,
str_repeat(' ', intval(round($pad, 0, 1) + $this->style->getPaddingLeftRight()))
);
$promptWidth = mb_strlen($this->text) + 4;
$promptWidth = mb_strwidth($this->text) + 4;
}

$leftFill = (int) (($promptWidth / 2) - ($buttonLength / 2));
Expand Down
8 changes: 4 additions & 4 deletions src/Dialogue/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function display(string $confirmText = 'OK') : void

$this->terminal->moveCursorToRow($this->y);

$promptWidth = mb_strlen($this->text) + 4;
$promptWidth = mb_strwidth($this->text) + 4;

$this->emptyRow();

Expand All @@ -37,7 +37,7 @@ public function display(string $confirmText = 'OK') : void
$this->emptyRow();

$confirmText = sprintf(' < %s > ', $confirmText);
$leftFill = (int) (($promptWidth / 2) - (mb_strlen($confirmText) / 2));
$leftFill = (int) (($promptWidth / 2) - (mb_strwidth($confirmText) / 2));

$this->write(sprintf(
"%s%s%s%s%s%s%s\n",
Expand All @@ -46,15 +46,15 @@ public function display(string $confirmText = 'OK') : void
$this->style->getInvertedColoursSetCode(),
$confirmText,
$this->style->getInvertedColoursUnsetCode(),
str_repeat(' ', (int) ceil($promptWidth - $leftFill - mb_strlen($confirmText))),
str_repeat(' ', (int) ceil($promptWidth - $leftFill - mb_strwidth($confirmText))),
$this->style->getColoursResetCode()
));

$this->write(sprintf(
"%s%s%s%s%s\n",
$this->style->getColoursSetCode(),
str_repeat(' ', $this->style->getPaddingLeftRight()),
str_repeat(' ', mb_strlen($this->text)),
str_repeat(' ', mb_strwidth($this->text)),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$this->style->getColoursResetCode()
));
Expand Down
4 changes: 2 additions & 2 deletions src/Dialogue/Dialogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function calculateCoordinates() : void

//x
$parentStyle = $this->parentMenu->getStyle();
$dialogueHalfLength = (int) ((mb_strlen($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2);
$dialogueHalfLength = (int) ((mb_strwidth($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2);
$widthHalfLength = (int) ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
$this->x = $widthHalfLength - $dialogueHalfLength;
}
Expand All @@ -98,7 +98,7 @@ protected function emptyRow() : void
"%s%s%s%s%s\n",
$this->style->getColoursSetCode(),
str_repeat(' ', $this->style->getPaddingLeftRight()),
str_repeat(' ', mb_strlen($this->text)),
str_repeat(' ', mb_strwidth($this->text)),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$this->style->getColoursResetCode()
)
Expand Down
4 changes: 2 additions & 2 deletions src/Input/InputIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private function getInputWidth(array $lines) : int
return max(
array_map(
function (string $line) {
return mb_strlen($line);
return mb_strwidth($line);
},
$lines
)
Expand Down Expand Up @@ -171,7 +171,7 @@ private function drawCenteredLine(Input $input, string $userInput, string $text)
]
);

$textLength = mb_strlen(StringUtil::stripAnsiEscapeSequence($text));
$textLength = mb_strwidth(StringUtil::stripAnsiEscapeSequence($text));
$leftFill = (int) (($width / 2) - ($textLength / 2));
$rightFill = (int) ceil($width - $leftFill - $textLength);

Expand Down
4 changes: 2 additions & 2 deletions src/Input/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public function validate(string $input) : bool
return $validator($input);
}

return mb_strlen($input) >= $this->passwordLength;
return mb_strwidth($input) >= $this->passwordLength;
}

public function filter(string $value) : string
{
return str_repeat('*', mb_strlen($value));
return str_repeat('*', mb_strwidth($value));
}

public function getStyle() : MenuStyle
Expand Down
2 changes: 1 addition & 1 deletion src/MenuItem/AsciiArtItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function setText(string $text) : void
*/
private function calculateArtLength() : void
{
$this->artLength = (int) max(array_map('mb_strlen', explode("\n", $this->text)));
$this->artLength = (int) max(array_map('mb_strwidth', explode("\n", $this->text)));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/MenuItem/SelectableItemRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function wrapAndIndentText(string $marker, string $text, int $availableWi
s::wordwrap(
"{$marker}{$text}",
$availableWidth,
sprintf("\n%s", $this->emptyString(mb_strlen($marker)))
sprintf("\n%s", $this->emptyString(mb_strwidth($marker)))
)
);
}
Expand All @@ -59,7 +59,7 @@ public function emptyString(int $numCharacters) : string
public function getAvailableTextWidth(MenuStyle $menuStyle, ItemStyle $itemStyle) : int
{
return $itemStyle->getDisplaysExtra()
? $menuStyle->getContentWidth() - (mb_strlen($itemStyle->getItemExtra()) + 2)
? $menuStyle->getContentWidth() - (mb_strwidth($itemStyle->getItemExtra()) + 2)
: $menuStyle->getContentWidth();
}
}
10 changes: 5 additions & 5 deletions src/MenuItem/SplitItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
$itemExtraVal = $item->getStyle()->getItemExtra();
$itemExtra = $item->showsItemExtra()
? sprintf(' %s', $itemExtraVal)
: sprintf(' %s', str_repeat(' ', mb_strlen($itemExtraVal)));
: sprintf(' %s', str_repeat(' ', mb_strwidth($itemExtraVal)));
}

return $this->buildCell(
Expand All @@ -163,7 +163,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
StringUtil::wordwrap(
sprintf('%s%s', $marker, $item->getText()),
$length,
sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
sprintf("\n%s", str_repeat(' ', mb_strwidth($marker)))
)
),
$length,
Expand Down Expand Up @@ -226,8 +226,8 @@ private function buildCell(
'%s%s%s%s%s%s',
$invertedColoursSetCode,
$row,
str_repeat(' ', $length - mb_strlen($row)),
$index === 0 ? $itemExtra : str_repeat(' ', mb_strlen($itemExtra)),
str_repeat(' ', $length - mb_strwidth($row)),
$index === 0 ? $itemExtra : str_repeat(' ', mb_strwidth($itemExtra)),
$invertedColoursUnsetCode,
str_repeat(' ', $this->gutter)
);
Expand Down Expand Up @@ -339,7 +339,7 @@ private function calculateItemExtra() : int
{
return max(array_map(
function (MenuItemInterface $item) {
return mb_strlen($item->getStyle()->getItemExtra());
return mb_strwidth($item->getStyle()->getItemExtra());
},
array_filter($this->items, function (MenuItemInterface $item) {
return $item->getStyle()->getDisplaysExtra();
Expand Down
6 changes: 3 additions & 3 deletions src/Util/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public static function wordwrap(string $string, int $width, string $break = "\n"
$break,
array_map(function (string $line) use ($width, $break) {
$line = rtrim($line);
if (mb_strlen($line) <= $width) {
if (mb_strwidth($line) <= $width) {
return $line;
}

$words = explode(' ', $line);
$line = '';
$actual = '';
foreach ($words as $word) {
if (mb_strlen($actual . $word) <= $width) {
if (mb_strwidth($actual . $word) <= $width) {
$actual .= $word . ' ';
} else {
if ($actual !== '') {
Expand All @@ -47,6 +47,6 @@ public static function stripAnsiEscapeSequence(string $str) : string

public static function length(string $str, bool $ignoreAnsiEscapeSequence = true) : int
{
return mb_strlen($ignoreAnsiEscapeSequence ? self::stripAnsiEscapeSequence($str) : $str);
return mb_strwidth($ignoreAnsiEscapeSequence ? self::stripAnsiEscapeSequence($str) : $str);
}
}
Loading