2017-07-17 20:19:13 +00:00
|
|
|
package prompt
|
|
|
|
|
2018-06-24 05:48:26 +00:00
|
|
|
import "strings"
|
2017-07-17 20:19:13 +00:00
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// Filter is the type to filter the prompt.Suggestion array.
|
2017-08-09 16:03:43 +00:00
|
|
|
type Filter func([]Suggest, string, bool) []Suggest
|
2017-07-18 10:04:11 +00:00
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// FilterHasPrefix checks whether the string completions.Text begins with sub.
|
2017-08-04 11:30:50 +00:00
|
|
|
func FilterHasPrefix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
2018-10-20 06:50:53 +00:00
|
|
|
return filterSuggestions(completions, sub, ignoreCase, strings.HasPrefix)
|
2017-07-17 20:19:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// FilterHasSuffix checks whether the completion.Text ends with sub.
|
2017-08-04 11:30:50 +00:00
|
|
|
func FilterHasSuffix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
2018-10-20 06:50:53 +00:00
|
|
|
return filterSuggestions(completions, sub, ignoreCase, strings.HasSuffix)
|
2018-09-03 04:55:51 +00:00
|
|
|
}
|
2017-07-17 20:19:13 +00:00
|
|
|
|
2018-09-03 04:55:51 +00:00
|
|
|
// FilterContains checks whether the completion.Text contains sub.
|
|
|
|
func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
2018-10-20 06:50:53 +00:00
|
|
|
return filterSuggestions(completions, sub, ignoreCase, strings.Contains)
|
2018-09-03 04:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FilterFuzzy checks whether the completion.Text fuzzy matches sub.
|
|
|
|
// Fuzzy searching for "dog" is equivalent to "*d*o*g*". This search term
|
|
|
|
// would match, for example, "Good food is gone"
|
|
|
|
// ^ ^ ^
|
|
|
|
func FilterFuzzy(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
2018-10-20 06:50:53 +00:00
|
|
|
return filterSuggestions(completions, sub, ignoreCase, fuzzyMatch)
|
2018-09-03 04:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fuzzyMatch(s, sub string) bool {
|
|
|
|
sChars := []rune(s)
|
|
|
|
sIdx := 0
|
|
|
|
|
2020-02-24 13:06:18 +00:00
|
|
|
// https://staticcheck.io/docs/checks#S1029
|
|
|
|
for _, c := range sub {
|
2018-09-03 04:55:51 +00:00
|
|
|
found := false
|
|
|
|
for ; sIdx < len(sChars); sIdx++ {
|
|
|
|
if sChars[sIdx] == c {
|
|
|
|
found = true
|
|
|
|
sIdx++
|
|
|
|
break
|
|
|
|
}
|
2017-07-17 20:19:13 +00:00
|
|
|
}
|
2018-09-03 04:55:51 +00:00
|
|
|
if !found {
|
|
|
|
return false
|
2017-07-17 20:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-03 04:55:51 +00:00
|
|
|
return true
|
2017-07-17 20:19:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-20 06:50:53 +00:00
|
|
|
func filterSuggestions(suggestions []Suggest, sub string, ignoreCase bool, function func(string, string) bool) []Suggest {
|
2017-07-17 20:19:13 +00:00
|
|
|
if sub == "" {
|
2018-10-20 06:50:53 +00:00
|
|
|
return suggestions
|
2017-07-17 20:19:13 +00:00
|
|
|
}
|
|
|
|
if ignoreCase {
|
|
|
|
sub = strings.ToUpper(sub)
|
|
|
|
}
|
|
|
|
|
2018-10-20 06:50:53 +00:00
|
|
|
ret := make([]Suggest, 0, len(suggestions))
|
|
|
|
for i := range suggestions {
|
|
|
|
c := suggestions[i].Text
|
2017-07-17 20:19:13 +00:00
|
|
|
if ignoreCase {
|
2017-07-18 17:12:22 +00:00
|
|
|
c = strings.ToUpper(c)
|
2017-07-17 20:19:13 +00:00
|
|
|
}
|
2018-10-20 06:50:53 +00:00
|
|
|
if function(c, sub) {
|
|
|
|
ret = append(ret, suggestions[i])
|
2017-07-17 20:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|