Gim/internal/action/delete.go
Hayden Hargreaves b1b3edf810 feat: this is huge, and needs some review, but for now its good
The tests are not passing, something to do with view I think.
2026-02-26 23:18:18 -07:00

81 lines
1.5 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 {
win := m.ActiveWindow()
buf := m.ActiveBuffer()
pos := win.Cursor.Col
line := buf.Lines[win.Cursor.Line]
for i := 0; i < a.Count && pos < len(line); i++ {
line = line[:pos] + line[pos+1:]
buf.SetLine(win.Cursor.Line, 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 {
win := m.ActiveWindow()
buf := m.ActiveBuffer()
// Delete to end of line
pos := win.Cursor.Col
line := buf.Lines[win.Cursor.Line]
buf.SetLine(win.Cursor.Line, line[:pos])
win.SetCursorCol(pos - 1)
// If count is greater, than we will delete the next N - 1 lines below
initY := win.Cursor.Line
if a.Count > 1 {
// Copied from `internal/operator/delete.go`
opCount := min(a.Count-1, buf.LineCount()-win.Cursor.Line)
// Down one
win.SetCursorLine(initY + 1)
for range opCount {
y := win.Cursor.Line // Changed from the copied code
// Stop if were on the starting line
if y == initY {
break
}
buf.DeleteLine(y)
if buf.LineCount() == 0 {
buf.InsertLine(0, "")
}
if y >= buf.LineCount() {
y = buf.LineCount() - 1
}
win.SetCursorLine(y)
}
}
win.SetCursorLine(initY)
return nil
}
func (a DeleteToEndOfLine) WithCount(n int) Action {
return DeleteToEndOfLine{Count: n}
}