Skip to content

Commit

Permalink
feat(textarea): add SetOffset and CursorPosition methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 28, 2024
1 parent 84bab1e commit 6dfd0e0
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))

Check failure on line 1166 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / test (~1.18, ubuntu-latest)

undefined: tea.SetCursorPosition

Check failure on line 1166 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / lint-soft

undefined: tea.SetCursorPosition (typecheck)

Check failure on line 1166 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / lint

undefined: tea.SetCursorPosition (typecheck)

Check failure on line 1166 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / coverage (^1.18, ubuntu-latest)

undefined: tea.SetCursorPosition

Check failure on line 1166 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / test (^1, ubuntu-latest)

undefined: tea.SetCursorPosition

Check failure on line 1166 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / test (^1, macos-latest)

undefined: tea.SetCursorPosition

m.Cursor, cmd = m.Cursor.Update(msg)
if (newRow != oldRow || newCol != oldCol) && m.Cursor.Mode() == cursor.CursorBlink {
m.Cursor.Blink = false
Expand Down

0 comments on commit 6dfd0e0

Please sign in to comment.