go-prompt/filter.go

79 lines
1.8 KiB
Go
Raw Normal View History

2017-07-17 20:19:13 +00:00
package prompt
import (
"strings"
)
2017-07-17 20:19:13 +00:00
// Filter is the type to filter the prompt.Suggestion array.
// Deprecated, move to github.com/c-bata/go-prompt/filter package.
2017-08-09 16:03:43 +00:00
type Filter func([]Suggest, string, bool) []Suggest
2017-07-18 10:04:11 +00:00
// FilterHasPrefix checks whether the string completions.Text begins with sub.
// Deprecated, move to github.com/c-bata/go-prompt/filter package.
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
}
// FilterHasSuffix checks whether the completion.Text ends with sub.
// Deprecated, move to github.com/c-bata/go-prompt/filter package.
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
}
// FilterContains checks whether the completion.Text contains sub.
// Deprecated, move to github.com/c-bata/go-prompt/filter package.
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) {
ret = append(ret, completions[i])
2017-07-17 20:19:13 +00:00
}
}
return ret
}