Skip to content

Commit

Permalink
Fix for up/down arrows + cursor position when textarea content contai…
Browse files Browse the repository at this point in the history
…ns multi-byte strings (#137)

* fix for up and down arrows for multi byte strings

* Fix code styling

* mb_strlen instead of whatever i was doing before

---------

Co-authored-by: joetannenbaum <[email protected]>
  • Loading branch information
joetannenbaum and joetannenbaum authored Apr 18, 2024
1 parent dd33b5e commit bf9a360
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
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([
'', '', Key::ENTER,
'', '', '', '', Key::ENTER,
'', '', '', '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([
'', '', Key::ENTER,
'', '', '', '', Key::ENTER,
'', '', '', '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"
);
});

0 comments on commit bf9a360

Please sign in to comment.