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

Fix for up/down arrows + cursor position when textarea content contains multi-byte strings #137

Merged
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
8 changes: 4 additions & 4 deletions src/TextareaPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function handleUpKey(): void
$lines = collect($this->lines());

// Line length + 1 for the newline character
$lineLengths = $lines->map(fn ($line, $index) => mb_strwidth($line) + ($index === $lines->count() - 1 ? 0 : 1));
$lineLengths = $lines->map(fn ($line, $index) => mb_strlen($line) + ($index === $lines->count() - 1 ? 0 : 1));

$currentLineIndex = $this->currentLineIndex();

Expand Down Expand Up @@ -145,13 +145,13 @@ protected function handleDownKey(): void
$lines = collect($this->lines());

// Line length + 1 for the newline character
$lineLengths = $lines->map(fn ($line, $index) => mb_strwidth($line) + ($index === $lines->count() - 1 ? 0 : 1));
$lineLengths = $lines->map(fn ($line, $index) => mb_strlen($line) + ($index === $lines->count() - 1 ? 0 : 1));

$currentLineIndex = $this->currentLineIndex();

if ($currentLineIndex === $lines->count() - 1) {
// They're already at the last line, jump them to the last position
$this->cursorPosition = mb_strwidth($lines->implode(PHP_EOL));
$this->cursorPosition = mb_strlen($lines->implode(PHP_EOL));

return;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ protected function currentLineIndex(): int
$totalLineLength = 0;

return (int) collect($this->lines())->search(function ($line) use (&$totalLineLength) {
$totalLineLength += mb_strwidth($line) + 1;
$totalLineLength += mb_strlen($line) + 1;

return $totalLineLength > $this->cursorPosition;
}) ?: 0;
Expand Down
47 changes: 47 additions & 0 deletions tests/Feature/TextareaPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,50 @@

expect($result)->toBe("abc\ndeg\nf");
});

it('correctly handles multi-byte strings for the down arrow', function () {
Prompt::fake([
'a', 'b', Key::ENTER,
'c', 'd', 'e', 'f', Key::ENTER,
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'n', 'o', 'p', 'q', 'r', 's', Key::ENTER,
't', 'u', 'v', 'w', 'x', 'y', 'z',
Key::UP,
Key::UP,
Key::UP,
Key::UP,
Key::RIGHT,
Key::DOWN,
'y', 'o',
Key::CTRL_D,
]);

$result = textarea(label: 'What is your name?');

expect($result)->toBe(
"ab\ncyodef\nghijklmnnopqrs\ntuvwxyz"
);
});

it('correctly handles multi-byte strings for the up arrow', function () {
Prompt::fake([
'a', 'b', Key::ENTER,
'c', 'd', 'e', 'f', Key::ENTER,
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'n', 'o', 'p', 'q', 'r', 's', Key::ENTER,
't', 'u', 'v', 'w', 'x', 'y', 'z',
Key::UP,
Key::UP,
Key::UP,
Key::UP,
Key::RIGHT,
Key::DOWN,
Key::UP,
'y', 'o',
Key::CTRL_D,
]);

$result = textarea(label: 'What is your name?');

expect($result)->toBe(
"ayob\ncdef\nghijklmnnopqrs\ntuvwxyz"
);
});
Loading