feat(textarea): add SetOffset and CursorPosition methods

This commit is contained in:
Ayman Bagabas
2024-11-26 14:17:28 -05:00
committed by Christian Rocha
parent 4c214e0848
commit afbb7bc049
+15
View File
@@ -296,6 +296,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
@@ -536,6 +539,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() {
@@ -606,6 +614,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) {
@@ -1160,6 +1173,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