diff --git a/src/Builder/CliMenuBuilder.php b/src/Builder/CliMenuBuilder.php index 25b4305..6150a77 100644 --- a/src/Builder/CliMenuBuilder.php +++ b/src/Builder/CliMenuBuilder.php @@ -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]); } diff --git a/src/CliMenu.php b/src/CliMenu.php index 7d8eeba..f3f9de7 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -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()), diff --git a/src/Dialogue/CancellableConfirm.php b/src/Dialogue/CancellableConfirm.php index 3bf6b2d..3f93abd 100644 --- a/src/Dialogue/CancellableConfirm.php +++ b/src/Dialogue/CancellableConfirm.php @@ -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', @@ -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)); diff --git a/src/Dialogue/Confirm.php b/src/Dialogue/Confirm.php index bdf9161..8912b80 100644 --- a/src/Dialogue/Confirm.php +++ b/src/Dialogue/Confirm.php @@ -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(); @@ -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", @@ -46,7 +46,7 @@ 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() )); @@ -54,7 +54,7 @@ public function display(string $confirmText = 'OK') : 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() )); diff --git a/src/Dialogue/Dialogue.php b/src/Dialogue/Dialogue.php index a55a029..9bd544f 100644 --- a/src/Dialogue/Dialogue.php +++ b/src/Dialogue/Dialogue.php @@ -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; } @@ -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() ) diff --git a/src/Input/InputIO.php b/src/Input/InputIO.php index 51e293c..443822d 100644 --- a/src/Input/InputIO.php +++ b/src/Input/InputIO.php @@ -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 ) @@ -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); diff --git a/src/Input/Password.php b/src/Input/Password.php index 0748c54..a259bb0 100644 --- a/src/Input/Password.php +++ b/src/Input/Password.php @@ -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 diff --git a/src/MenuItem/AsciiArtItem.php b/src/MenuItem/AsciiArtItem.php index 3a391cb..b3d7f47 100644 --- a/src/MenuItem/AsciiArtItem.php +++ b/src/MenuItem/AsciiArtItem.php @@ -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))); } /** diff --git a/src/MenuItem/SelectableItemRenderer.php b/src/MenuItem/SelectableItemRenderer.php index aca5808..ea301f8 100644 --- a/src/MenuItem/SelectableItemRenderer.php +++ b/src/MenuItem/SelectableItemRenderer.php @@ -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))) ) ); } @@ -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(); } } diff --git a/src/MenuItem/SplitItem.php b/src/MenuItem/SplitItem.php index 7920ef8..e8abf67 100644 --- a/src/MenuItem/SplitItem.php +++ b/src/MenuItem/SplitItem.php @@ -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( @@ -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, @@ -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) ); @@ -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(); diff --git a/src/Util/StringUtil.php b/src/Util/StringUtil.php index 8cbba5a..560171f 100644 --- a/src/Util/StringUtil.php +++ b/src/Util/StringUtil.php @@ -18,7 +18,7 @@ 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; } @@ -26,7 +26,7 @@ public static function wordwrap(string $string, int $width, string $break = "\n" $line = ''; $actual = ''; foreach ($words as $word) { - if (mb_strlen($actual . $word) <= $width) { + if (mb_strwidth($actual . $word) <= $width) { $actual .= $word . ' '; } else { if ($actual !== '') { @@ -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); } }