Repository navigation
Description
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()returns3(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
- Create a
textinput.ModelwithSetVirtualCursor(false) - Type CJK characters (e.g. Japanese hiragana "あいう")
- 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])) + promptWidthAdditional context
textarea.Cursor()handles this correctly usinglineInfo.CharOffset(computed withuniseg.StringWidth)- The virtual cursor mode (
SetVirtualCursor(true)) is not affected since it renders the cursor inline inView() - 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
fix: correct cursor positioning for CJK wide characters
fix: correct cursor positioning for CJK wide characters (#79)
mastersingh24 commentedon Aug 15, 2026
Still reproduces in v2.1.1 —
textinput/textinput.gois byte-identical to v2.1.0.Cross-linking #1001, which reports a second defect in the same expression:
Cursor()also ignoresm.offset, so once the value is wider than the box and scrolls, the caret is computed against the unscrolled string and themin(xOffset, m.width+promptWidth)clamp pins it to the right edge. Neither issue references the other.Both terms are wrong in the same line:
and one expression fixes both, matching what
View()already does at textinput.go:694-695:The
textareacomparison in the original report holds up as the model to follow —LineInfo.CharOffsetis documented as differing fromColumnOffsetprecisely "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()) <= widththe value cannot have scrolled, som.offsetis necessarily 0 and the correct X is derivable from the exportedValue()andPosition(). Above that width you fall back into #1001's territory, wherem.offsethas no exported accessor and there is no downstream fix.feat(tui): type-to-filter in the pickers, and fix the dialog text-inp…
shounakjoshi88-a11y commentedon Nov 10, 2026
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.offsethalf 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.