complete/predicate.go

155 lines
4.0 KiB
Go
Raw Normal View History

2017-05-05 08:57:22 -05:00
package complete
2017-05-05 13:57:21 -05:00
import (
"os"
"path/filepath"
2017-05-07 11:53:55 -05:00
"github.com/posener/complete/match"
2017-05-05 13:57:21 -05:00
)
2017-05-05 13:59:10 -05:00
// Predicate determines what terms can follow a command or a flag
2017-05-06 14:06:49 -05:00
// It is used for auto completion, given last - the last word in the already
// in the command line, what words can complete it.
2017-05-07 11:53:55 -05:00
type Predicate func(last string) []match.Matcher
2017-05-05 13:57:21 -05:00
2017-05-06 14:06:49 -05:00
// Or unions two predicate functions, so that the result predicate
// returns the union of their predication
func (p Predicate) Or(other Predicate) Predicate {
2017-05-06 14:48:34 -05:00
if p == nil {
return other
}
if other == nil {
return p
2017-05-06 11:08:47 -05:00
}
2017-05-07 11:53:55 -05:00
return func(last string) []match.Matcher { return append(p.predict(last), other.predict(last)...) }
}
2017-05-07 11:53:55 -05:00
func (p Predicate) predict(last string) []match.Matcher {
if p == nil {
2017-05-05 13:57:21 -05:00
return nil
}
return p(last)
2017-05-05 08:57:22 -05:00
}
2017-05-06 14:06:49 -05:00
// PredictNothing does not expect anything after.
2017-05-06 14:25:44 -05:00
var PredictNothing Predicate
2017-05-05 08:57:22 -05:00
2017-05-06 14:25:44 -05:00
// PredictAnything expects something, but nothing particular, such as a number
2017-05-06 14:06:49 -05:00
// or arbitrary name.
2017-05-07 11:53:55 -05:00
func PredictAnything(last string) []match.Matcher { return nil }
2017-05-05 13:57:21 -05:00
2017-05-06 14:06:49 -05:00
// PredictSet expects specific set of terms, given in the options argument.
func PredictSet(options ...string) Predicate {
2017-05-07 11:53:55 -05:00
return func(last string) []match.Matcher {
ret := make([]match.Matcher, len(options))
for i := range options {
2017-05-07 11:53:55 -05:00
ret[i] = match.Prefix(options[i])
}
return ret
}
2017-05-05 16:25:27 -05:00
}
2017-05-06 14:06:49 -05:00
// PredictDirs will search for directories in the given started to be typed
// path, if no path was started to be typed, it will complete to directories
// in the current working directory.
func PredictDirs(pattern string) Predicate {
return files(pattern, true, false)
}
2017-05-05 16:25:27 -05:00
2017-05-06 14:06:49 -05:00
// PredictFiles will search for files matching the given pattern in the started to
// be typed path, if no path was started to be typed, it will complete to files that
// match the pattern in the current working directory.
// To match any file, use "*" as pattern. To match go files use "*.go", and so on.
func PredictFiles(pattern string) Predicate {
return files(pattern, false, true)
}
// PredictFilesOrDirs predict any file or directory that matches the pattern
func PredictFilesOrDirs(pattern string) Predicate {
return files(pattern, true, true)
}
func files(pattern string, allowDirs, allowFiles bool) Predicate {
2017-05-07 11:53:55 -05:00
return func(last string) []match.Matcher {
dir := dirFromLast(last)
Log("looking for files in %s (last=%s)", dir, last)
files, err := filepath.Glob(filepath.Join(dir, pattern))
2017-05-05 13:57:21 -05:00
if err != nil {
2017-05-05 16:25:27 -05:00
Log("failed glob operation with pattern '%s': %s", pattern, err)
2017-05-05 13:57:21 -05:00
}
if allowDirs {
files = append(files, dir)
}
files = selectByType(files, allowDirs, allowFiles)
2017-05-05 13:57:21 -05:00
if !filepath.IsAbs(pattern) {
filesToRel(files)
}
2017-05-06 14:06:49 -05:00
return filesToMatchers(files)
}
}
func selectByType(names []string, allowDirs bool, allowFiles bool) []string {
filtered := make([]string, 0, len(names))
for _, name := range names {
stat, err := os.Stat(name)
if err != nil {
continue
}
if (stat.IsDir() && !allowDirs) || (!stat.IsDir() && !allowFiles) {
continue
}
filtered = append(filtered, name)
}
return filtered
}
2017-05-06 14:06:49 -05:00
// filesToRel, change list of files to their names in the relative
// to current directory form.
2017-05-05 13:57:21 -05:00
func filesToRel(files []string) {
wd, err := os.Getwd()
if err != nil {
return
}
for i := range files {
abs, err := filepath.Abs(files[i])
if err != nil {
continue
}
rel, err := filepath.Rel(wd, abs)
if err != nil {
continue
}
if rel != "." {
2017-05-07 22:51:11 -05:00
rel = "./" + rel
}
if info, err := os.Stat(rel); err == nil && info.IsDir() {
rel += "/"
}
2017-05-07 22:51:11 -05:00
files[i] = rel
2017-05-05 13:57:21 -05:00
}
return
}
2017-05-07 11:53:55 -05:00
func filesToMatchers(files []string) []match.Matcher {
options := make([]match.Matcher, len(files))
for i, f := range files {
2017-05-07 11:53:55 -05:00
options[i] = match.File(f)
}
return options
}
// dirFromLast gives the directory of the current written
// last argument if it represents a file name being written.
// in case that it is not, we fall back to the current directory.
func dirFromLast(last string) string {
if info, err := os.Stat(last); err == nil && info.IsDir() {
return last
}
dir := filepath.Dir(last)
_, err := os.Stat(dir)
if err != nil {
return "./"
}
return dir
}