Skip to content

Commit 0cdb162

Browse files
authored
Merge pull request #274 from mingtsay/master
support full-width unicode characters
2 parents e2a03f7 + 5ed3700 commit 0cdb162

11 files changed

+27
-27
lines changed

src/Builder/CliMenuBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private function extractShortcut(string $title) : ?string
268268
return null;
269269
}
270270

271-
if (mb_strlen($match[1]) > 1) {
271+
if (mb_strwidth($match[1]) > 1) {
272272
throw InvalidShortcutException::fromShortcut($match[1]);
273273
}
274274

src/CliMenu.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false)
569569
$invertedColoursSetCode,
570570
str_repeat(' ', $this->style->getPaddingLeftRight()),
571571
$row,
572-
str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),
572+
str_repeat(' ', $this->style->getRightHandPadding(mb_strwidth(s::stripAnsiEscapeSequence($row)))),
573573
$invertedColoursUnsetCode,
574574
$borderColour,
575575
str_repeat(' ', $this->style->getBorderRightWidth()),

src/Dialogue/CancellableConfirm.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ private function drawDialog(string $confirmText = 'OK', string $cancelText = 'Ca
4545

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

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

50-
$buttonLength = mb_strlen($confirmText) + 6;
51-
$buttonLength += mb_strlen($cancelText) + 7;
50+
$buttonLength = mb_strwidth($confirmText) + 6;
51+
$buttonLength += mb_strwidth($cancelText) + 7;
5252

5353
$confirmButton = sprintf(
5454
'%s%s < %s > %s%s',
@@ -78,7 +78,7 @@ private function drawDialog(string $confirmText = 'OK', string $cancelText = 'Ca
7878
$this->text,
7979
str_repeat(' ', intval(round($pad, 0, 1) + $this->style->getPaddingLeftRight()))
8080
);
81-
$promptWidth = mb_strlen($this->text) + 4;
81+
$promptWidth = mb_strwidth($this->text) + 4;
8282
}
8383

8484
$leftFill = (int) (($promptWidth / 2) - ($buttonLength / 2));

src/Dialogue/Confirm.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function display(string $confirmText = 'OK') : void
2121

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

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

2626
$this->emptyRow();
2727

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

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

4242
$this->write(sprintf(
4343
"%s%s%s%s%s%s%s\n",
@@ -46,15 +46,15 @@ public function display(string $confirmText = 'OK') : void
4646
$this->style->getInvertedColoursSetCode(),
4747
$confirmText,
4848
$this->style->getInvertedColoursUnsetCode(),
49-
str_repeat(' ', (int) ceil($promptWidth - $leftFill - mb_strlen($confirmText))),
49+
str_repeat(' ', (int) ceil($promptWidth - $leftFill - mb_strwidth($confirmText))),
5050
$this->style->getColoursResetCode()
5151
));
5252

5353
$this->write(sprintf(
5454
"%s%s%s%s%s\n",
5555
$this->style->getColoursSetCode(),
5656
str_repeat(' ', $this->style->getPaddingLeftRight()),
57-
str_repeat(' ', mb_strlen($this->text)),
57+
str_repeat(' ', mb_strwidth($this->text)),
5858
str_repeat(' ', $this->style->getPaddingLeftRight()),
5959
$this->style->getColoursResetCode()
6060
));

src/Dialogue/Dialogue.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function calculateCoordinates() : void
8383

8484
//x
8585
$parentStyle = $this->parentMenu->getStyle();
86-
$dialogueHalfLength = (int) ((mb_strlen($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2);
86+
$dialogueHalfLength = (int) ((mb_strwidth($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2);
8787
$widthHalfLength = (int) ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
8888
$this->x = $widthHalfLength - $dialogueHalfLength;
8989
}
@@ -98,7 +98,7 @@ protected function emptyRow() : void
9898
"%s%s%s%s%s\n",
9999
$this->style->getColoursSetCode(),
100100
str_repeat(' ', $this->style->getPaddingLeftRight()),
101-
str_repeat(' ', mb_strlen($this->text)),
101+
str_repeat(' ', mb_strwidth($this->text)),
102102
str_repeat(' ', $this->style->getPaddingLeftRight()),
103103
$this->style->getColoursResetCode()
104104
)

src/Input/InputIO.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function getInputWidth(array $lines) : int
107107
return max(
108108
array_map(
109109
function (string $line) {
110-
return mb_strlen($line);
110+
return mb_strwidth($line);
111111
},
112112
$lines
113113
)
@@ -171,7 +171,7 @@ private function drawCenteredLine(Input $input, string $userInput, string $text)
171171
]
172172
);
173173

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

src/Input/Password.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ public function validate(string $input) : bool
111111
return $validator($input);
112112
}
113113

114-
return mb_strlen($input) >= $this->passwordLength;
114+
return mb_strwidth($input) >= $this->passwordLength;
115115
}
116116

117117
public function filter(string $value) : string
118118
{
119-
return str_repeat('*', mb_strlen($value));
119+
return str_repeat('*', mb_strwidth($value));
120120
}
121121

122122
public function getStyle() : MenuStyle

src/MenuItem/AsciiArtItem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function setText(string $text) : void
127127
*/
128128
private function calculateArtLength() : void
129129
{
130-
$this->artLength = (int) max(array_map('mb_strlen', explode("\n", $this->text)));
130+
$this->artLength = (int) max(array_map('mb_strwidth', explode("\n", $this->text)));
131131
}
132132

133133
/**

src/MenuItem/SelectableItemRenderer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function wrapAndIndentText(string $marker, string $text, int $availableWi
3636
s::wordwrap(
3737
"{$marker}{$text}",
3838
$availableWidth,
39-
sprintf("\n%s", $this->emptyString(mb_strlen($marker)))
39+
sprintf("\n%s", $this->emptyString(mb_strwidth($marker)))
4040
)
4141
);
4242
}
@@ -59,7 +59,7 @@ public function emptyString(int $numCharacters) : string
5959
public function getAvailableTextWidth(MenuStyle $menuStyle, ItemStyle $itemStyle) : int
6060
{
6161
return $itemStyle->getDisplaysExtra()
62-
? $menuStyle->getContentWidth() - (mb_strlen($itemStyle->getItemExtra()) + 2)
62+
? $menuStyle->getContentWidth() - (mb_strwidth($itemStyle->getItemExtra()) + 2)
6363
: $menuStyle->getContentWidth();
6464
}
6565
}

src/MenuItem/SplitItem.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
154154
$itemExtraVal = $item->getStyle()->getItemExtra();
155155
$itemExtra = $item->showsItemExtra()
156156
? sprintf(' %s', $itemExtraVal)
157-
: sprintf(' %s', str_repeat(' ', mb_strlen($itemExtraVal)));
157+
: sprintf(' %s', str_repeat(' ', mb_strwidth($itemExtraVal)));
158158
}
159159

160160
return $this->buildCell(
@@ -163,7 +163,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
163163
StringUtil::wordwrap(
164164
sprintf('%s%s', $marker, $item->getText()),
165165
$length,
166-
sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
166+
sprintf("\n%s", str_repeat(' ', mb_strwidth($marker)))
167167
)
168168
),
169169
$length,
@@ -226,8 +226,8 @@ private function buildCell(
226226
'%s%s%s%s%s%s',
227227
$invertedColoursSetCode,
228228
$row,
229-
str_repeat(' ', $length - mb_strlen($row)),
230-
$index === 0 ? $itemExtra : str_repeat(' ', mb_strlen($itemExtra)),
229+
str_repeat(' ', $length - mb_strwidth($row)),
230+
$index === 0 ? $itemExtra : str_repeat(' ', mb_strwidth($itemExtra)),
231231
$invertedColoursUnsetCode,
232232
str_repeat(' ', $this->gutter)
233233
);
@@ -339,7 +339,7 @@ private function calculateItemExtra() : int
339339
{
340340
return max(array_map(
341341
function (MenuItemInterface $item) {
342-
return mb_strlen($item->getStyle()->getItemExtra());
342+
return mb_strwidth($item->getStyle()->getItemExtra());
343343
},
344344
array_filter($this->items, function (MenuItemInterface $item) {
345345
return $item->getStyle()->getDisplaysExtra();

src/Util/StringUtil.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public static function wordwrap(string $string, int $width, string $break = "\n"
1818
$break,
1919
array_map(function (string $line) use ($width, $break) {
2020
$line = rtrim($line);
21-
if (mb_strlen($line) <= $width) {
21+
if (mb_strwidth($line) <= $width) {
2222
return $line;
2323
}
2424

2525
$words = explode(' ', $line);
2626
$line = '';
2727
$actual = '';
2828
foreach ($words as $word) {
29-
if (mb_strlen($actual . $word) <= $width) {
29+
if (mb_strwidth($actual . $word) <= $width) {
3030
$actual .= $word . ' ';
3131
} else {
3232
if ($actual !== '') {
@@ -47,6 +47,6 @@ public static function stripAnsiEscapeSequence(string $str) : string
4747

4848
public static function length(string $str, bool $ignoreAnsiEscapeSequence = true) : int
4949
{
50-
return mb_strlen($ignoreAnsiEscapeSequence ? self::stripAnsiEscapeSequence($str) : $str);
50+
return mb_strwidth($ignoreAnsiEscapeSequence ? self::stripAnsiEscapeSequence($str) : $str);
5151
}
5252
}

0 commit comments

Comments
 (0)