84 lines
1.3 KiB
Go
84 lines
1.3 KiB
Go
package action
|
|
|
|
import (
|
|
"git.gophernest.net/azpect/TextEditor/internal/core"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type FindChar struct {
|
|
Char string
|
|
Forward bool
|
|
Inclusive bool
|
|
Count int
|
|
}
|
|
|
|
func (m FindChar) WithChar(char string) Motion {
|
|
m.Char = char
|
|
return m
|
|
}
|
|
|
|
func (m FindChar) Type() core.MotionType {
|
|
if m.Inclusive {
|
|
return core.CharwiseInclusive
|
|
}
|
|
return core.CharwiseExclusive
|
|
|
|
}
|
|
|
|
// WithCount sets the count (required by Repeatable interface)
|
|
func (f FindChar) WithCount(n int) Action {
|
|
f.Count = n
|
|
return f
|
|
}
|
|
|
|
func (a FindChar) Execute(m Model) tea.Cmd {
|
|
// Get the line
|
|
// Get the current position, moved based on inputs
|
|
win := m.ActiveWindow()
|
|
buf := win.Buffer
|
|
|
|
line := buf.Line(win.Cursor.Line)
|
|
col := win.Cursor.Col
|
|
|
|
if len(line) <= 0 {
|
|
return nil
|
|
}
|
|
|
|
if a.Forward {
|
|
for x := col; x < len(line); x++ {
|
|
if string(line[x]) == a.Char {
|
|
if a.Count == 1 {
|
|
if a.Inclusive {
|
|
win.SetCursorCol(x)
|
|
} else {
|
|
win.SetCursorCol(x - 1)
|
|
}
|
|
break
|
|
} else {
|
|
a.Count--
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if !a.Forward {
|
|
for x := col; x >= 0; x-- {
|
|
if string(line[x]) == a.Char {
|
|
if a.Count == 1 {
|
|
if a.Inclusive {
|
|
win.SetCursorCol(x)
|
|
} else {
|
|
win.SetCursorCol(x + 1)
|
|
}
|
|
break
|
|
} else {
|
|
a.Count--
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|