diff options
| author | Eyal Posener <[email protected]> | 2017-05-06 22:21:03 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-05-06 22:21:03 +0300 |
| commit | c26ef096c7990a5ae97b503545fd76ff6df388d6 (patch) | |
| tree | d0180b667269b985d15520f3b2e1aafc9292af6e /match.go | |
| parent | 2b6aed2b1e974a733c0dc614a9617c33a54c208c (diff) | |
| parent | 404634e843081e7010260bd95006b84d6c40a8fd (diff) | |
Merge pull request #4 from posener/doc
Doc
Diffstat (limited to 'match.go')
| -rw-r--r-- | match.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/match.go b/match.go new file mode 100644 index 0000000..7593d65 --- /dev/null +++ b/match.go @@ -0,0 +1,48 @@ +package complete + +import ( + "path/filepath" + "strings" +) + +// 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 { + String() string + Match(prefix string) bool +} + +// MatchPrefix is a simple Matcher, if the word is it's prefix, there is a match +type MatchPrefix string + +func (a MatchPrefix) String() string { + return string(a) +} + +func (a MatchPrefix) Match(prefix string) bool { + return strings.HasPrefix(string(a), prefix) +} + +// MatchFileName is a file name Matcher, if the last word can prefix the +// MatchFileName path, there is a possible match +type MatchFileName string + +func (a MatchFileName) String() string { + return string(a) +} + +func (a MatchFileName) Match(prefix string) bool { + full, err := filepath.Abs(string(a)) + if err != nil { + Log("failed getting abs path of %s: %s", a, err) + } + prefixFull, err := filepath.Abs(prefix) + if err != nil { + Log("failed getting abs path of %s: %s", prefix, err) + } + + // 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:], "/")) +} |
