Skip to content

fix(textinput): use display width for cursor position with CJK characters#920

Open
amosbird wants to merge 1 commit intocharmbracelet:mainfrom
amosbird:fix/textinput-cjk-cursor
Open

fix(textinput): use display width for cursor position with CJK characters#920
amosbird wants to merge 1 commit intocharmbracelet:mainfrom
amosbird:fix/textinput-cjk-cursor

Conversation

@amosbird
Copy link
Copy Markdown

Summary

Cursor() used m.Position() (rune index) to compute the X offset. This is incorrect for wide characters (e.g. CJK) that occupy 2 terminal columns per rune, causing the real cursor to be placed to the left of where it should be.

Fix: replace m.Position() with uniseg.StringWidth(string(m.value[m.offset:m.pos])) to compute the actual display width of the visible text before the cursor.

Test plan

  • Added TestCursorPositionWithCJKCharacters — pure CJK input
  • Added TestCursorPositionWithMixedASCIIAndCJK — mixed ASCII + CJK
  • Added TestCursorPositionCJKWithOffset — narrow width forcing scroll, cursor stays within bounds

…ters

Cursor() used m.Position() (rune index) to compute the X offset,
which is incorrect for wide characters like CJK that occupy 2 terminal
columns per rune. Replace it with uniseg.StringWidth to compute the
actual display width of the visible text before the cursor.
@amosbird amosbird requested a review from meowgorithm as a code owner March 29, 2026 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant