summaryrefslogtreecommitdiff
path: root/match/file.go
blob: c972ce0b560793816ca5a8f2dff1aa5d8b0f8d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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:], "/"))
}