9 "github.com/posener/complete/match"
12 // PredictDirs will search for directories in the given started to be typed
13 // path, if no path was started to be typed, it will complete to directories
14 // in the current working directory.
15 func PredictDirs(pattern string) Predictor {
16 return files(pattern, false)
19 // PredictFiles will search for files matching the given pattern in the started to
20 // be typed path, if no path was started to be typed, it will complete to files that
21 // match the pattern in the current working directory.
22 // To match any file, use "*" as pattern. To match go files use "*.go", and so on.
23 func PredictFiles(pattern string) Predictor {
24 return files(pattern, true)
27 func files(pattern string, allowFiles bool) PredictFunc {
29 // search for files according to arguments,
30 // if only one directory has matched the result, search recursively into
31 // this directory to give more results.
32 return func(a Args) (prediction []string) {
33 prediction = predictFiles(a, pattern, allowFiles)
35 // if the number of prediction is not 1, we either have many results or
36 // have no results, so we return it.
37 if len(prediction) != 1 {
41 // only try deeper, if the one item is a directory
42 if stat, err := os.Stat(prediction[0]); err != nil || !stat.IsDir() {
46 a.Last = prediction[0]
47 return predictFiles(a, pattern, allowFiles)
51 func predictFiles(a Args, pattern string, allowFiles bool) []string {
52 if strings.HasSuffix(a.Last, "/..") {
57 files := listFiles(dir, pattern, allowFiles)
60 files = append(files, dir)
62 return PredictFilesSet(files).Predict(a)
65 // PredictFilesSet predict according to file rules to a given set of file names
66 func PredictFilesSet(files []string) PredictFunc {
67 return func(a Args) (prediction []string) {
68 // add all matching files to prediction
69 for _, f := range files {
70 f = fixPathForm(a.Last, f)
72 // test matching of file to the argument
73 if match.File(f, a.Last) {
74 prediction = append(prediction, f)
81 func listFiles(dir, pattern string, allowFiles bool) []string {
82 // set of all file names
83 m := map[string]bool{}
86 if files, err := filepath.Glob(filepath.Join(dir, pattern)); err == nil {
87 for _, f := range files {
88 if stat, err := os.Stat(f); err != nil || stat.IsDir() || allowFiles {
95 if dirs, err := ioutil.ReadDir(dir); err == nil {
96 for _, d := range dirs {
98 m[filepath.Join(dir, d.Name())] = true
103 list := make([]string, 0, len(m))
105 list = append(list, k)