From 7baaf918459dade0d2ada2e2d3dd9f58b81f5aae Mon Sep 17 00:00:00 2001 From: c-bata Date: Tue, 18 Jul 2017 05:19:13 +0900 Subject: [PATCH] Add FilterHasPrefix and FilterContains --- prompt/filter.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 prompt/filter.go diff --git a/prompt/filter.go b/prompt/filter.go new file mode 100644 index 0000000..10e91a2 --- /dev/null +++ b/prompt/filter.go @@ -0,0 +1,68 @@ +package prompt + +import "strings" + +func FilterHasPrefix(completions []string, sub string, ignoreCase bool) []string { + if sub == "" { + return completions + } + if ignoreCase { + sub = strings.ToUpper(sub) + } + + ret := make([]string, 0, len(completions)) + for i := range completions { + n := completions[i] + if ignoreCase { + n = strings.ToUpper(n) + } + if strings.HasPrefix(n, sub) { + ret = append(ret, completions[i]) + } + } + return ret +} + +func FilterHasSuffix(completions []string, sub string, ignoreCase bool) []string { + if sub == "" { + return completions + } + if ignoreCase { + sub = strings.ToUpper(sub) + } + + ret := make([]string, 0, len(completions)) + for i := range completions { + n := completions[i] + if ignoreCase { + n = strings.ToUpper(n) + } + if strings.HasSuffix(n, sub) { + ret = append(ret, completions[i]) + } + } + return ret +} + +func FilterContains(completions []string, sub string, ignoreCase bool) []string { + if sub == "" { + return completions + } + if ignoreCase { + sub = strings.ToUpper(sub) + } + + ret := make([]string, 0, len(completions)) + for i := range completions { + n := completions[i] + if ignoreCase { + n = strings.ToUpper(n) + } + if strings.Contains(n, sub) { + ret = append(ret, n) + } + } + return ret +} + +