Make filter to CompletionFilter
This commit is contained in:
parent
6feb776763
commit
8bcf5c31fc
35
filter.go
35
filter.go
|
@ -2,9 +2,9 @@ package prompt
|
|||
|
||||
import "strings"
|
||||
|
||||
type Filter func([]string, string, bool) []string
|
||||
type CompletionFilter func([]Completion, string, bool) []Completion
|
||||
|
||||
func FilterHasPrefix(completions []string, sub string, ignoreCase bool) []string {
|
||||
func FilterHasPrefix(completions []Completion, sub string, ignoreCase bool) []Completion {
|
||||
if sub == "" {
|
||||
return completions
|
||||
}
|
||||
|
@ -12,19 +12,20 @@ func FilterHasPrefix(completions []string, sub string, ignoreCase bool) []string
|
|||
sub = strings.ToUpper(sub)
|
||||
}
|
||||
|
||||
ret := make([]string, 0, len(completions))
|
||||
for i, n := range completions {
|
||||
ret := make([]Completion, 0, len(completions))
|
||||
for i := range completions {
|
||||
c := completions[i].Text
|
||||
if ignoreCase {
|
||||
n = strings.ToUpper(n)
|
||||
c = strings.ToUpper(c)
|
||||
}
|
||||
if strings.HasPrefix(n, sub) {
|
||||
if strings.HasPrefix(c, sub) {
|
||||
ret = append(ret, completions[i])
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func FilterHasSuffix(completions []string, sub string, ignoreCase bool) []string {
|
||||
func FilterHasSuffix(completions []Completion, sub string, ignoreCase bool) []Completion {
|
||||
if sub == "" {
|
||||
return completions
|
||||
}
|
||||
|
@ -32,19 +33,20 @@ func FilterHasSuffix(completions []string, sub string, ignoreCase bool) []string
|
|||
sub = strings.ToUpper(sub)
|
||||
}
|
||||
|
||||
ret := make([]string, 0, len(completions))
|
||||
for i, n := range completions {
|
||||
ret := make([]Completion, 0, len(completions))
|
||||
for i := range completions {
|
||||
c := completions[i].Text
|
||||
if ignoreCase {
|
||||
n = strings.ToUpper(n)
|
||||
c = strings.ToUpper(c)
|
||||
}
|
||||
if strings.HasSuffix(n, sub) {
|
||||
if strings.HasSuffix(c, sub) {
|
||||
ret = append(ret, completions[i])
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func FilterContains(completions []string, sub string, ignoreCase bool) []string {
|
||||
func FilterContains(completions []Completion, sub string, ignoreCase bool) []Completion {
|
||||
if sub == "" {
|
||||
return completions
|
||||
}
|
||||
|
@ -52,12 +54,13 @@ func FilterContains(completions []string, sub string, ignoreCase bool) []string
|
|||
sub = strings.ToUpper(sub)
|
||||
}
|
||||
|
||||
ret := make([]string, 0, len(completions))
|
||||
for i, n := range completions {
|
||||
ret := make([]Completion, 0, len(completions))
|
||||
for i := range completions {
|
||||
c := completions[i].Text
|
||||
if ignoreCase {
|
||||
n = strings.ToUpper(n)
|
||||
c = strings.ToUpper(c)
|
||||
}
|
||||
if strings.Contains(n, sub) {
|
||||
if strings.Contains(c, sub) {
|
||||
ret = append(ret, completions[i])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,59 +8,98 @@ import (
|
|||
func TestFilter(t *testing.T) {
|
||||
var scenarioTable = [] struct {
|
||||
scenario string
|
||||
filter Filter
|
||||
list []string
|
||||
filter CompletionFilter
|
||||
list []Completion
|
||||
substr string
|
||||
ignoreCase bool
|
||||
expected []string
|
||||
expected []Completion
|
||||
} {
|
||||
{
|
||||
scenario: "Contains don't ignore case",
|
||||
filter: FilterContains,
|
||||
list: []string{"abcde", "fghij", "ABCDE"},
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cd",
|
||||
ignoreCase: false,
|
||||
expected: []string{"abcde"},
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "Contains ignore case",
|
||||
filter: FilterContains,
|
||||
list: []string{"abcde", "fghij", "ABCDE"},
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cd",
|
||||
ignoreCase: true,
|
||||
expected: []string{"abcde", "ABCDE"},
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasPrefix don't ignore case",
|
||||
filter: FilterHasPrefix,
|
||||
list: []string{"abcde", "fghij", "ABCDE"},
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "abc",
|
||||
ignoreCase: false,
|
||||
expected: []string{"abcde"},
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasPrefix ignore case",
|
||||
filter: FilterHasPrefix,
|
||||
list: []string{"abcde", "fabcj", "ABCDE"},
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fabcj"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "abc",
|
||||
ignoreCase: true,
|
||||
expected: []string{"abcde", "ABCDE"},
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasSuffix don't ignore case",
|
||||
filter: FilterHasSuffix,
|
||||
list: []string{"abcde", "fcdej", "ABCDE"},
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fcdej"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cde",
|
||||
ignoreCase: false,
|
||||
expected: []string{"abcde"},
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasSuffix ignore case",
|
||||
filter: FilterHasSuffix,
|
||||
list: []string{"abcde", "fcdej", "ABCDE"},
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fcdej"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cde",
|
||||
ignoreCase: true,
|
||||
expected: []string{"abcde", "ABCDE"},
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue