diff --git a/textarea/textarea.go b/textarea/textarea.go index 2732f0e8..d80f3cd3 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -291,6 +291,9 @@ type Model struct { // Cursor row. row int + // The bubble offset in the parent model. + offsetX, offsetY int + // Last character offset, used to maintain state when the cursor is moved // vertically such that we can maintain the same navigating position. lastCharOffset int @@ -522,6 +525,11 @@ func (m Model) Line() int { return m.row } +// SetOffset sets the bubble offset in the parent model. +func (m *Model) SetOffset(x, y int) { + m.offsetX, m.offsetY = x, y +} + // CursorDown moves the cursor down by one line. // Returns whether or not the cursor blink should be reset. func (m *Model) CursorDown() { @@ -592,6 +600,11 @@ func (m *Model) CursorUp() { } } +// CursorPosition returns the current cursor position. +func (m Model) CursorPosition() (int, int) { + return m.col, m.row +} + // SetCursor moves the cursor to the given position. If the position is // out of bounds the cursor will be moved to the start or end accordingly. func (m *Model) SetCursor(col int) { @@ -1150,6 +1163,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { cmds = append(cmds, cmd) newRow, newCol := m.cursorLineNumber(), m.col + cmds = append(cmds, tea.SetCursorPosition(m.offsetX+newCol, m.offsetY+newRow)) + m.Cursor, cmd = m.Cursor.Update(msg) if (newRow != oldRow || newCol != oldCol) && m.Cursor.Mode() == cursor.CursorBlink { m.Cursor.Blink = false