ziggs/internal/interactive/completer.go

37 lines
928 B
Go
Raw Normal View History

2022-03-07 14:57:42 +00:00
package interactive
import cli "git.tcp.direct/Mirrors/go-prompt"
func completer(in cli.Document) []cli.Suggest {
c := in.CurrentLine()
if c == "" {
return []cli.Suggest{}
}
var set []cli.Suggest
for command, arguments := range suggestions {
head := in.CursorPositionCol()
if head > len(command.Text) {
2022-03-07 14:57:42 +00:00
head = len(command.Text)
}
tmpl := &cli.Document{Text: command.Text}
2022-03-07 14:57:42 +00:00
one := tmpl.GetWordAfterCursor()
2022-07-29 10:44:08 +00:00
if one != "" && one != "use" {
set = append(set, cli.Suggest{Text: tmpl.Text})
}
if one == "use" && len(arguments) > 1 {
2022-03-07 14:57:42 +00:00
set = append(set, cli.Suggest{Text: tmpl.Text})
}
for _, a := range arguments {
if head > len(tmpl.Text+a.Text)+1 {
2022-03-07 14:57:42 +00:00
continue
}
tmpl = &cli.Document{Text: tmpl.Text + " " + a.Text}
2022-03-07 14:57:42 +00:00
two := tmpl.GetWordAfterCursorWithSpace()
if two != "" {
set = append(set, cli.Suggest{Text: tmpl.Text})
}
}
}
return cli.FilterHasPrefix(set, c, true)
}