laravel / prompts

Beautiful and user-friendly forms for your command-line PHP applications.
https://laravel.com/docs/prompts
MIT License
532 stars 94 forks source link

Incorrect cursor position when text area contains multi-byte strings #133

Closed macocci7 closed 6 months ago

macocci7 commented 7 months ago

Laravel Prompts Version

0.1.18

Laravel Version

11.3.0

PHP Version

8.2.13

Operating System & Version

Ubuntu 22.04 via WSL2

Terminal Application

VS Code's integrated terminal

Description

Issue

If there is a multibyte string in the previous line, the cursor moves downwards to the wrong position. The same goes for moving up.

Findings

$this->cursorPosition in Laravel\Prompts\TextareaPrompt includes multibyte widths counted as 2 by mb_strwidth(), and I think it's necessary for determining cursor position in multiline text. It seems the bug is caused by $this->cursorPosition passed in:

        return $this->addCursor($this->wrappedValue(), $this->cursorPosition + $this->cursorOffset(), -1);

I'm posting here because I couldn't find a good solution.

Steps To Reproduce

textarea(
    label: 'Tell me a story.',
    default: "ab\ncdef\nghijklmnnopqrs\ntuvwxyz\n0123456789\n+-*/",
);

run this code and:

  1. The last line will not be displayed.
  2. Press [Home], press [End], then the last line will not be displayed.
  3. Press [Home], press [↓], then the cursor will move to right of 'd'.
  4. Press [↓] after step 3 above, then the cursor will move to right of 'l'.
  5. Press [↓] after step 4 above, then the cursor will move to right of '0'.
  6. Press [↓] after step 5 above, then the cursor will move to right of '8'.
jessarcher commented 7 months ago

@joetannenbaum are you able to take a look at this one please?

joetannenbaum commented 7 months ago

Will do 👍