Gim/internal/action/delete.go
2026-02-18 17:25:38 -07:00

77 lines
1.4 KiB
Go

package action
import tea "github.com/charmbracelet/bubbletea"
// DeleteChar implements Action (x)
type DeleteChar struct {
Count int
}
func (a DeleteChar) Execute(m Model) tea.Cmd {
pos := m.CursorX()
line := m.Line(m.CursorY())
for i := 0; i < a.Count && pos < len(line); i++ {
line = line[:pos] + line[pos+1:]
m.SetLine(m.CursorY(), line)
}
return nil
}
func (a DeleteChar) WithCount(n int) Action {
return DeleteChar{Count: n}
}
type DeleteToEndOfLine struct {
Count int
}
func (a DeleteToEndOfLine) Execute(m Model) tea.Cmd {
// Delete to end of line
pos := m.CursorX()
line := m.Line(m.CursorY())
m.SetLine(m.CursorY(), line[:pos])
m.SetCursorX(pos - 1)
// If count is greater, than we will delete the next N - 1 lines below
initY := m.CursorY()
if a.Count > 1 {
// Copied from `internal/operator/delete.go`
opCount := min(a.Count-1, m.LineCount()-m.CursorY())
// Down one
m.SetCursorY(initY + 1)
for range opCount {
y := m.CursorY() // Changed from the copied code
// Stop if were on the starting line
if y == initY {
break
}
m.DeleteLine(y)
if m.LineCount() == 0 {
m.InsertLine(0, "")
}
if y >= m.LineCount() {
y = m.LineCount() - 1
}
m.SetCursorY(y)
m.ClampCursorX()
}
}
m.SetCursorY(initY)
m.ClampCursorX()
return nil
}
func (a DeleteToEndOfLine) WithCount(n int) Action {
return DeleteToEndOfLine{Count: n}
}