Skip to content
Open
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
4 changes: 2 additions & 2 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,8 @@ func (m Model) Cursor() *tea.Cursor {
w := lipgloss.Width

promptWidth := w(m.promptView())
xOffset := m.Position() +
promptWidth
displayWidth := uniseg.StringWidth(string(m.value[m.offset:m.pos]))
xOffset := displayWidth + promptWidth
if m.width > 0 {
xOffset = min(xOffset, m.width+promptWidth)
}
Expand Down
70 changes: 70 additions & 0 deletions textinput/textinput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,76 @@ func ExampleValidateFunc() {
}
}

func TestCursorPositionWithCJKCharacters(t *testing.T) {
t.Parallel()

ti := New()
ti.SetVirtualCursor(false)
ti.Focus()
ti.Prompt = "> "

// Type CJK characters that are 2 cells wide each.
ti = sendString(ti, "你好")

cur := ti.Cursor()
if cur == nil {
t.Fatal("expected non-nil cursor")
}

promptWidth := 2 // "> " is 2 columns
// "你好" = 2 CJK characters, each 2 cells wide = 4 columns total.
expectedX := promptWidth + 4
if cur.X != expectedX {
t.Fatalf("expected cursor X=%d but got X=%d", expectedX, cur.X)
}
}

func TestCursorPositionWithMixedASCIIAndCJK(t *testing.T) {
t.Parallel()

ti := New()
ti.SetVirtualCursor(false)
ti.Focus()
ti.Prompt = ""

// Type mixed ASCII and CJK characters.
ti = sendString(ti, "ab你c")

cur := ti.Cursor()
if cur == nil {
t.Fatal("expected non-nil cursor")
}

// "ab" = 2 columns, "你" = 2 columns, "c" = 1 column => 5 total.
expectedX := 5
if cur.X != expectedX {
t.Fatalf("expected cursor X=%d but got X=%d", expectedX, cur.X)
}
}

func TestCursorPositionCJKWithOffset(t *testing.T) {
t.Parallel()

ti := New()
ti.SetVirtualCursor(false)
ti.Focus()
ti.Prompt = ""
ti.SetWidth(6) // narrow width to force scrolling

// Type enough CJK characters to overflow the width.
ti = sendString(ti, "你好世界")

cur := ti.Cursor()
if cur == nil {
t.Fatal("expected non-nil cursor")
}

// Cursor X should not exceed width.
if cur.X > ti.Width() {
t.Fatalf("cursor X=%d exceeds width=%d", cur.X, ti.Width())
}
}

func keyPress(key rune) tea.Msg {
return tea.KeyPressMsg{Code: key, Text: string(key)}
}
Expand Down