Gim/internal/motion/basic.go

76 lines
1.4 KiB
Go

package motion
import (
tea "github.com/charmbracelet/bubbletea"
"git.gophernest.net/azpect/TextEditor/internal/action"
)
// MoveDown implements Motion (j)
type MoveDown struct {
Count int
}
func (a MoveDown) Execute(m action.Model) tea.Cmd {
for i := 0; i < a.Count && m.CursorY() < m.LineCount()-1; i++ {
m.SetCursorY(m.CursorY() + 1)
}
m.ClampCursorX()
return nil
}
func (a MoveDown) WithCount(n int) action.Action {
return MoveDown{Count: n}
}
// MoveUp implements Motion (k)
type MoveUp struct {
Count int
}
func (a MoveUp) Execute(m action.Model) tea.Cmd {
for i := 0; i < a.Count && m.CursorY() > 0; i++ {
m.SetCursorY(m.CursorY() - 1)
}
m.ClampCursorX()
return nil
}
func (a MoveUp) WithCount(n int) action.Action {
return MoveUp{Count: n}
}
// MoveLeft implements Motion (h)
type MoveLeft struct {
Count int
}
func (a MoveLeft) Execute(m action.Model) tea.Cmd {
for i := 0; i < a.Count && m.CursorX() > 0; i++ {
m.SetCursorX(m.CursorX() - 1)
}
m.ClampCursorX()
return nil
}
func (a MoveLeft) WithCount(n int) action.Action {
return MoveLeft{Count: n}
}
// MoveRight implements Motion (l)
type MoveRight struct {
Count int
}
func (a MoveRight) Execute(m action.Model) tea.Cmd {
lineLen := len(m.Line(m.CursorY()))
for i := 0; i < a.Count && m.CursorX() <= lineLen; i++ {
m.SetCursorX(m.CursorX() + 1)
}
m.ClampCursorX()
return nil
}
func (a MoveRight) WithCount(n int) action.Action {
return MoveRight{Count: n}
}