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 {
|
2017-07-17 20:19:13 +00:00
|
|
|
if sub == "" {
|
|
|
|
return completions
|
|
|
|
}
|
|
|
|
if ignoreCase {
|
|
|
|
sub = strings.ToUpper(sub)
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:30:50 +00:00
|
|
|
ret := make([]Suggest, 0, len(completions))
|
2017-07-18 17:12:22 +00:00
|
|
|
for i := range completions {
|
|
|
|
c := completions[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
|
|
|
}
|
2017-07-18 17:12:22 +00:00
|
|
|
if strings.HasPrefix(c, sub) {
|
2017-07-17 20:19:13 +00:00
|
|
|
ret = append(ret, completions[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-07-17 20:19:13 +00:00
|
|
|
if sub == "" {
|
|
|
|
return completions
|
|
|
|
}
|
|
|
|
if ignoreCase {
|
|
|
|
sub = strings.ToUpper(sub)
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:30:50 +00:00
|
|
|
ret := make([]Suggest, 0, len(completions))
|
2017-07-18 17:12:22 +00:00
|
|
|
for i := range completions {
|
|
|
|
c := completions[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
|
|
|
}
|
2017-07-18 17:12:22 +00:00
|
|
|
if strings.HasSuffix(c, sub) {
|
2017-07-17 20:19:13 +00:00
|
|
|
ret = append(ret, completions[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// FilterContains checks whether the completion.Text contains sub.
|
2017-08-04 11:30:50 +00:00
|
|
|
func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
2017-07-17 20:19:13 +00:00
|
|
|
if sub == "" {
|
|
|
|
return completions
|
|
|
|
}
|
|
|
|
if ignoreCase {
|
|
|
|
sub = strings.ToUpper(sub)
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:30:50 +00:00
|
|
|
ret := make([]Suggest, 0, len(completions))
|
2017-07-18 17:12:22 +00:00
|
|
|
for i := range completions {
|
|
|
|
c := completions[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
|
|
|
}
|
2017-07-18 17:12:22 +00:00
|
|
|
if strings.Contains(c, sub) {
|
2017-07-18 02:36:25 +00:00
|
|
|
ret = append(ret, completions[i])
|
2017-07-17 20:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|