From 807e7c8f5cd829667f6bb9cd12927fc2e53f4ff9 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 2 Aug 2023 14:02:42 +1000 Subject: [PATCH] Improve support for accent characters --- src/Concerns/TypedValue.php | 12 ++++++------ src/PasswordPrompt.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Concerns/TypedValue.php b/src/Concerns/TypedValue.php index 9d08e76f..b67d3d89 100644 --- a/src/Concerns/TypedValue.php +++ b/src/Concerns/TypedValue.php @@ -36,15 +36,15 @@ protected function trackTypedValue(string $default = '', bool $submit = true): v $this->typedValue = $default; if ($this->typedValue) { - $this->cursorPosition = strlen($this->typedValue); + $this->cursorPosition = mb_strlen($this->typedValue); } $this->on('key', function ($key) use ($submit) { if ($key[0] === "\e") { match ($key) { Key::LEFT => $this->cursorPosition = max(0, $this->cursorPosition - 1), - Key::RIGHT => $this->cursorPosition = min(strlen($this->typedValue), $this->cursorPosition + 1), - Key::DELETE => $this->typedValue = substr($this->typedValue, 0, $this->cursorPosition).substr($this->typedValue, $this->cursorPosition + 1), + Key::RIGHT => $this->cursorPosition = min(mb_strlen($this->typedValue), $this->cursorPosition + 1), + Key::DELETE => $this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition).mb_substr($this->typedValue, $this->cursorPosition + 1), default => null, }; @@ -52,7 +52,7 @@ protected function trackTypedValue(string $default = '', bool $submit = true): v } // Keys may be buffered. - foreach (str_split($key) as $key) { + foreach (mb_str_split($key) as $key) { if ($key === Key::ENTER && $submit) { $this->submit(); @@ -62,10 +62,10 @@ protected function trackTypedValue(string $default = '', bool $submit = true): v return; } - $this->typedValue = substr($this->typedValue, 0, $this->cursorPosition - 1).substr($this->typedValue, $this->cursorPosition); + $this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition - 1).mb_substr($this->typedValue, $this->cursorPosition); $this->cursorPosition--; } elseif (! in_array($key, $this->ignore)) { - $this->typedValue = substr($this->typedValue, 0, $this->cursorPosition).$key.substr($this->typedValue, $this->cursorPosition); + $this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition).$key.mb_substr($this->typedValue, $this->cursorPosition); $this->cursorPosition++; } } diff --git a/src/PasswordPrompt.php b/src/PasswordPrompt.php index ecbada87..2806b43d 100644 --- a/src/PasswordPrompt.php +++ b/src/PasswordPrompt.php @@ -25,7 +25,7 @@ public function __construct( */ public function masked(): string { - return str_repeat('•', strlen($this->value())); + return str_repeat('•', mb_strlen($this->value())); } /**