summaryrefslogtreecommitdiff
path: root/match/file.go
diff options
context:
space:
mode:
Diffstat (limited to 'match/file.go')
-rw-r--r--match/file.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/match/file.go b/match/file.go
new file mode 100644
index 0000000..c972ce0
--- /dev/null
+++ b/match/file.go
@@ -0,0 +1,30 @@
+package match
+
+import (
+ "path/filepath"
+ "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 {
+ full, err := filepath.Abs(string(a))
+ if err != nil {
+ return false
+ }
+ prefixFull, err := filepath.Abs(prefix)
+ if err != nil {
+ return false
+ }
+
+ // if the file has the prefix as prefix,
+ // but we don't want to show too many files, so, if it is in a deeper directory - omit it.
+ return strings.HasPrefix(full, prefixFull) && (full == prefixFull || !strings.Contains(full[len(prefixFull)+1:], "/"))
+}