Gim/internal/motion/command.go
Hayden Hargreaves 3c98dca777 feat: implement command history, tested
The tests are starting to get messy, lots of duplication. Going to
resolve that. Lots of this is due to AI generation of tests.
2026-03-19 15:23:44 -07:00

85 lines
2.7 KiB
Go

package motion
import (
"git.gophernest.net/azpect/TextEditor/internal/action"
"git.gophernest.net/azpect/TextEditor/internal/core"
tea "github.com/charmbracelet/bubbletea"
)
// MoveCommandLeft implements Motion - moves cursor left in command line.
type MoveCommandLeft struct{}
// MoveCommandLeft.Execute: Moves the command line cursor one position to the left.
func (a MoveCommandLeft) Execute(m action.Model) tea.Cmd {
// The set function handles bounds
m.SetCommandCursor(m.CommandCursor() - 1)
return nil
}
// MoveCommandLeft.Type: Returns CharwiseExclusive for command line motion.
func (a MoveCommandLeft) Type() core.MotionType { return core.CharwiseExclusive }
// MoveCommandRight implements Motion - moves cursor right in command line.
type MoveCommandRight struct{}
// MoveCommandRight.Execute: Moves the command line cursor one position to the right.
func (a MoveCommandRight) Execute(m action.Model) tea.Cmd {
// The set function handles bounds
m.SetCommandCursor(m.CommandCursor() + 1)
return nil
}
// MoveCommandRight.Type: Returns CharwiseExclusive for command line motion.
func (a MoveCommandRight) Type() core.MotionType { return core.CharwiseExclusive }
// ==================================================
// Command History Motions
// ==================================================
// MoveCommandHistoryUp implements Motion - moves cursor right in command line.
type MoveCommandHistoryUp struct{}
// MoveCommandHistoryUp.Execute: Moves the command history cursor up (if possible).
func (a MoveCommandHistoryUp) Execute(m action.Model) tea.Cmd {
cur := m.CommandHistoryCursor() // always +1 (bascially indexed at 1)
history := m.CommandHistory()
if cur < len(history) {
cmd := history[cur]
m.SetCommand(cmd)
m.SetCommandCursor(len(cmd))
// Only go up if we can
m.SetCommandHistoryCursor(cur + 1)
}
return nil
}
// MoveCommandHistoryUp.Type: Returns Linewise for command line motion.
func (a MoveCommandHistoryUp) Type() core.MotionType { return core.Linewise }
// MoveCommandHistoryDown implements Motion - moves cursor right in command line.
type MoveCommandHistoryDown struct{}
// MoveCommandHistoryDown.Execute: Moves the command history cursor down (if possible).
func (a MoveCommandHistoryDown) Execute(m action.Model) tea.Cmd {
cur := m.CommandHistoryCursor() // always +1 (bascially indexed at 1)
history := m.CommandHistory()
if cur > 1 {
cmd := history[cur-2]
m.SetCommand(cmd)
m.SetCommandCursor(len(cmd))
} else {
m.SetCommand("") // BUG: We should probably keep the original in state
m.SetCommandCursor(0)
}
m.SetCommandHistoryCursor(max(0, cur-1))
return nil
}
// MoveCommandHistoryDown.Type: Returns Linewise for command line motion.
func (a MoveCommandHistoryDown) Type() core.MotionType { return core.Linewise }