summaryrefslogtreecommitdiff
path: root/match
diff options
context:
space:
mode:
Diffstat (limited to 'match')
-rw-r--r--match/file.go22
-rw-r--r--match/match.go13
-rw-r--r--match/match_test.go26
-rw-r--r--match/prefix.go10
4 files changed, 29 insertions, 42 deletions
diff --git a/match/file.go b/match/file.go
index 0b554ce..eee5bec 100644
--- a/match/file.go
+++ b/match/file.go
@@ -1,26 +1,16 @@
package match
-import (
- "strings"
-)
+import "strings"
-// File is a file name Matcher, if the last word can prefix the
-// File path, there is a possible match
-type File string
-
-func (a File) String() string {
- return string(a)
-}
-
-// Match returns true if prefix's abs path prefixes a's abs path
-func (a File) Match(prefix string) bool {
+// File returns true if prefix can match the file
+func File(file, prefix string) bool {
// special case for current directory completion
- if a == "./" && (prefix == "." || prefix == "") {
+ if file == "./" && (prefix == "." || prefix == "") {
return true
}
- cmp := strings.TrimPrefix(string(a), "./")
+ file = strings.TrimPrefix(file, "./")
prefix = strings.TrimPrefix(prefix, "./")
- return strings.HasPrefix(cmp, prefix)
+ return strings.HasPrefix(file, prefix)
}
diff --git a/match/match.go b/match/match.go
index ae95549..812fcac 100644
--- a/match/match.go
+++ b/match/match.go
@@ -1,11 +1,6 @@
package match
-import "fmt"
-
-// Matcher matches itself to a string
-// it is used for comparing a given argument to the last typed
-// word, and see if it is a possible auto complete option.
-type Matcher interface {
- fmt.Stringer
- Match(prefix string) bool
-}
+// Match matches two strings
+// it is used for comparing a term to the last typed
+// word, the prefix, and see if it is a possible auto complete option.
+type Match func(term, prefix string) bool
diff --git a/match/match_test.go b/match/match_test.go
index d7a851a..b5a0d87 100644
--- a/match/match_test.go
+++ b/match/match_test.go
@@ -1,6 +1,7 @@
package match
import (
+ "fmt"
"os"
"testing"
)
@@ -21,11 +22,13 @@ func TestMatch(t *testing.T) {
}
tests := []struct {
- m Matcher
+ m Match
+ long string
tests []matcherTest
}{
{
- m: Prefix("abcd"),
+ m: Prefix,
+ long: "abcd",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "ab", want: true},
@@ -33,14 +36,16 @@ func TestMatch(t *testing.T) {
},
},
{
- m: Prefix(""),
+ m: Prefix,
+ long: "",
tests: []matcherTest{
{prefix: "ac", want: false},
{prefix: "", want: true},
},
},
{
- m: File("file.txt"),
+ m: File,
+ long: "file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: true},
@@ -59,7 +64,8 @@ func TestMatch(t *testing.T) {
},
},
{
- m: File("./file.txt"),
+ m: File,
+ long: "./file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: true},
@@ -78,7 +84,8 @@ func TestMatch(t *testing.T) {
},
},
{
- m: File("/file.txt"),
+ m: File,
+ long: "/file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: false},
@@ -97,7 +104,8 @@ func TestMatch(t *testing.T) {
},
},
{
- m: File("./"),
+ m: File,
+ long: "./",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: ".", want: true},
@@ -109,9 +117,9 @@ func TestMatch(t *testing.T) {
for _, tt := range tests {
for _, ttt := range tt.tests {
- name := "matcher='" + tt.m.String() + "'&prefix='" + ttt.prefix + "'"
+ name := fmt.Sprintf("matcher=%T&long='%s'&prefix='%s'", tt.m, tt.long, ttt.prefix)
t.Run(name, func(t *testing.T) {
- got := tt.m.Match(ttt.prefix)
+ got := tt.m(tt.long, ttt.prefix)
if got != ttt.want {
t.Errorf("Failed %s: got = %t, want: %t", name, got, ttt.want)
}
diff --git a/match/prefix.go b/match/prefix.go
index d54902d..9a01ba6 100644
--- a/match/prefix.go
+++ b/match/prefix.go
@@ -3,13 +3,7 @@ package match
import "strings"
// Prefix is a simple Matcher, if the word is it's prefix, there is a match
-type Prefix string
-
-func (a Prefix) String() string {
- return string(a)
-}
-
// Match returns true if a has the prefix as prefix
-func (a Prefix) Match(prefix string) bool {
- return strings.HasPrefix(string(a), prefix)
+func Prefix(long, prefix string) bool {
+ return strings.HasPrefix(long, prefix)
}