Repository navigation
Description
Description
textinput.Model.Cursor() returns an X coordinate based on m.Position() (the absolute cursor position in the full value), but when the input text is wider than the configured width, the textinput scrolls horizontally. View() correctly uses m.pos - m.offset for the visible cursor column, but Cursor() does not account for the scroll offset.
This means the hardware cursor (used when SetVirtualCursor(false)) is placed at the wrong column when the input is scrolled.
Version
charm.land/bubbles/v2 v2.1.0
Affected code
In textinput.go, the Cursor() method:
func (m Model) Cursor() *tea.Cursor {
if m.useVirtualCursor || !m.Focused() {
return nil
}
w := lipgloss.Width
promptWidth := w(m.promptView())
xOffset := m.Position() + promptWidth // <-- uses absolute position
if m.width > 0 {
xOffset = min(xOffset, m.width+promptWidth)
}
// ...
}Compare with View() which correctly adjusts for scroll:
value := m.value[m.offset:m.offsetRight]
pos := max(0, m.pos-m.offset) // <-- uses scroll-adjusted positionReproduction test
package textinput_test
import (
"strings"
"testing"
"charm.land/bubbles/v2/textinput"
)
func TestCursorXAccountsForScrollOffset(t *testing.T) {
m := textinput.New()
m.Focus()
m.SetVirtualCursor(false)
m.CharLimit = 200
m.SetWidth(20)
text := strings.Repeat("a", 30)
m.SetValue(text)
m.CursorEnd()
m, _ = m.Update(nil)
cur := m.Cursor()
if cur == nil {
t.Fatal("Cursor() returned nil")
}
width := m.Width()
t.Logf("width=%d pos=%d cursor.X=%d", width, m.Position(), cur.X)
if cur.X > width {
t.Errorf("Cursor().X = %d, exceeds viewport width %d; "+
"Cursor() uses Position() (absolute) instead of the "+
"scroll-adjusted visible column", cur.X, width)
}
}Output:
width=20 pos=30 cursor.X=22
Cursor().X = 22, exceeds viewport width 20
Expected behavior
Cursor().X should reflect the visible cursor column within the viewport (i.e., m.pos - m.offset + promptWidth), consistent with how View() renders the cursor.
Impact
Any consumer using the hardware cursor (SetVirtualCursor(false)) with text longer than the input width will see the terminal cursor placed at the wrong column. We hit this building an SSH-accessible CLI where the hardware cursor is needed to avoid re-render-induced text selection breakage.
Suggested fix
Replace m.Position() with m.Position() - m.offset in Cursor() (the offset field is unexported, so this must be fixed in the package):
xOffset := (m.pos - m.offset) + promptWidth
Activity
hiSandog commentedon Jul 11, 2026
Using
m.pos - m.offsetaligns the hardware cursor withView(), but the regression should assert display-cell width rather than rune count. Wide CJK characters, combining marks, a styled/wide prompt, cursor-at-start after scrolling, and selection edits can all change the visible column. A table test comparingCursor().Xwith the rendered cursor position across those cases would prevent fixing the ASCII overflow example only.sifhg commentedon Jul 24, 2026
I just came across this issue too and it's making it difficult working with the cursor when using textinput, so I'm glad to see it's already reported.
Instead of changing the behaviour of
m.Position()as this might break other projects that depend on bubbles, I'd suggest either defining a method that returns the relative position (maybem.RelativePosition()) or one that returns the offset so we can easily compute the cursor position (m.Offset).mastersingh24 commentedon Aug 15, 2026
Still present in v2.1.1 —
textinput/textinput.gois byte-identical to v2.1.0, so the version field above can be read as "v2.1.0 and v2.1.1".Worth linking: #906 reports a second defect in the same expression —
m.Position()is a rune index whilepromptWidthis a display width, so wide runes misplace the caret independently of any scrolling. Neither issue references the other, but they are the same two terms:A single expression fixes both, and mirrors what
View()already does twenty lines earlier:ansi.StringWidthresolves #906 (width, not rune count); slicing fromm.offsetresolves this issue. The existingmin(xOffset, m.width+promptWidth)clamp then becomes redundant rather than load-bearing — right now it is what pins the caret to the right edge once the value scrolls.On @sifhg's point about not changing
Position(): agreed, and worth separating the two cases, because they are not equally workaroundable downstream.ansi.StringWidth(m.Value()) <= widththe value cannot have scrolled, som.offsetis provably 0 and a caller can compute the correct X from the exportedValue()andPosition()alone.m.offsethas no exported accessor — I checked the full exported method set — and reproducing it downstream would mean reimplementinghandleOverflow's scroll arithmetic against state that is only observable indirectly.So even if the fix inside
Cursor()is deferred, exporting anOffset() int(or aRelativePosition() intreturningm.pos - m.offset) would unblock callers, which the current API cannot.thomas-mangin commentedon Aug 15, 2026
As #1005 highlighted, it is important to count cells and not runes.
fix(cli): restore the bubbles cursor patch and guard it properly
build: bump eight go modules, reapply the two vendor patches