Skip to content

textinput: Cursor() returns incorrect X position for wide (CJK) characters #906

Closed

Description

@708u

Describe the bug

textinput.Model.Cursor() computes the cursor X offset using m.Position(), which returns a rune index. For wide characters (CJK, fullwidth, etc.) that occupy 2 terminal columns, this results in the cursor being placed at the wrong screen position.

In contrast, textarea.Model.Cursor() correctly uses lineInfo.CharOffset, which is computed via uniseg.StringWidth (display width).

// textinput.go — current (buggy)
func (m Model) Cursor() *tea.Cursor {
    // ...
    xOffset := m.Position() + promptWidth  // m.Position() = rune index, not display width
    // ...
}

// textarea.go — correct
func (m Model) Cursor() *tea.Cursor {
    // ...
    lineInfo := m.LineInfo()
    xOffset := lineInfo.CharOffset + ...   // CharOffset = uniseg.StringWidth(...)
    // ...
}

For example, with the value "あいう" (3 CJK runes, 6 display columns):

  • m.Position() returns 3 (rune count)
  • Correct cursor X should be 6 (display columns)
  • The cursor is placed 3 columns too far to the left

This bug was introduced in v2 with the real cursor API. v1 only had virtual cursors (rendered inline in View()), which are not affected.

Setup

  • OS: macOS
  • Shell: zsh
  • Terminal Emulator: WezTerm
  • Locale: en_US.UTF-8

To Reproduce

  1. Create a textinput.Model with SetVirtualCursor(false)
  2. Type CJK characters (e.g. Japanese hiragana "あいう")
  3. Observe that the terminal cursor position does not match the end of the typed text

Source Code

package main

import (
	"fmt"
	"os"

	"charm.land/bubbles/v2/textinput"
	tea "charm.land/bubbletea/v2"
)

type model struct {
	ti textinput.Model
}

func initialModel() model {
	ti := textinput.New()
	ti.Placeholder = "Type CJK text here"
	ti.SetVirtualCursor(false)
	ti.Focus()
	ti.SetWidth(40)
	return model{ti: ti}
}

func (m model) Init() tea.Cmd { return nil }

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.KeyPressMsg:
		if msg.String() == "ctrl+c" {
			return m, tea.Quit
		}
	}
	var cmd tea.Cmd
	m.ti, cmd = m.ti.Update(msg)
	return m, cmd
}

func (m model) View() tea.View {
	var v tea.View
	v.SetContent(fmt.Sprintf("Input: %s\n\nPress Ctrl+C to quit.", m.ti.View()))
	if c := m.ti.Cursor(); c != nil {
		promptW := len([]rune(m.ti.Prompt))
		c.X += len("Input: ") - promptW
		v.Cursor = c
	}
	return v
}

func main() {
	p := tea.NewProgram(initialModel())
	if _, err := p.Run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

Expected behavior

The cursor X position returned by Cursor() should reflect the display width (terminal columns) of the text before the cursor, not the rune count. The fix would be to use display-width-aware calculation, similar to what textarea.Cursor() already does:

// Suggested fix
xOffset := uniseg.StringWidth(string(m.value[m.offset:m.pos])) + promptWidth

Additional context

  • textarea.Cursor() handles this correctly using lineInfo.CharOffset (computed with uniseg.StringWidth)
  • The virtual cursor mode (SetVirtualCursor(true)) is not affected since it renders the cursor inline in View()
  • Only the real cursor mode (SetVirtualCursor(false)) is affected
  • Bubbles version: v2.0.0 (latest as of writing, also present on master at 363089b)

Activity

  1. mastersingh24 commented on Aug 15, 2026

    @mastersingh24

    Still reproduces in v2.1.1 — textinput/textinput.go is byte-identical to v2.1.0.

    Cross-linking #1001, which reports a second defect in the same expression: Cursor() also ignores m.offset, so once the value is wider than the box and scrolls, the caret is computed against the unscrolled string and the min(xOffset, m.width+promptWidth) clamp pins it to the right edge. Neither issue references the other.

    Both terms are wrong in the same line:

    xOffset := m.Position() + promptWidth
    //         ^^^^^^^^^^^^   rune index (this issue), and ignores m.offset (#1001)
    //                        ^^^^^^^^^^^ display width

    and one expression fixes both, matching what View() already does at textinput.go:694-695:

    xOffset := promptWidth + ansi.StringWidth(string(m.value[m.offset:m.pos]))

    The textarea comparison in the original report holds up as the model to follow — LineInfo.CharOffset is documented as differing from ColumnOffset precisely "when there are double-width runes before the cursor", which is this bug stated as a doc comment on the widget that gets it right.

    One practical note for anyone hitting this before it lands: this issue is partially workaroundable downstream, unlike #1001. When ansi.StringWidth(m.Value()) <= width the value cannot have scrolled, so m.offset is necessarily 0 and the correct X is derivable from the exported Value() and Position(). Above that width you fall back into #1001's territory, where m.offset has no exported accessor and there is no downstream fix.

  2. shounakjoshi88-a11y commented on Nov 10, 2026

    @shounakjoshi88-a11y
    Contributor

    Taking this one.

    The fix is a single expression: measure the display width of the value between the scroll offset and the cursor position instead of using the rune index, which also fixes the m.offset half of the same line that @mastersingh24 pointed out.

    PR: #1087 - it adds two regression tests, one for a CJK value and one for a value wider than the input, both of which fail before the change.

    AI tools were used to assist with the investigation and the initial patch; I reviewed and verified every line, and go test ./... passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions