summaryrefslogtreecommitdiff
path: root/predict_files.go
diff options
context:
space:
mode:
Diffstat (limited to 'predict_files.go')
-rw-r--r--predict_files.go62
1 files changed, 34 insertions, 28 deletions
diff --git a/predict_files.go b/predict_files.go
index cdd1206..8ad5368 100644
--- a/predict_files.go
+++ b/predict_files.go
@@ -26,41 +26,47 @@ func PredictFiles(pattern string) Predictor {
func files(pattern string, allowFiles bool) PredictFunc {
return func(a Args) (prediction []string) {
- if strings.HasSuffix(a.Last, "/..") {
- return
- }
- dir := dirFromLast(a.Last)
- rel := !filepath.IsAbs(pattern)
- Log("looking for files in %s (last=%s)", dir, a.Last)
- files := listFiles(dir, pattern)
+ prediction = predictFiles(a.Last, pattern, allowFiles)
+ return
+ }
+}
- // get wording directory for relative name
- workDir, err := os.Getwd()
- if err != nil {
- workDir = ""
- }
+func predictFiles(last string, pattern string, allowFiles bool) (prediction []string) {
+ if strings.HasSuffix(last, "/..") {
+ return
+ }
- // add dir if match
- files = append(files, dir)
+ dir := dirFromLast(last)
+ rel := !filepath.IsAbs(pattern)
+ files := listFiles(dir, pattern)
- // add all matching files to prediction
- for _, f := range files {
- if stat, err := os.Stat(f); err != nil || (!stat.IsDir() && !allowFiles) {
- continue
- }
+ // get wording directory for relative name
+ workDir, err := os.Getwd()
+ if err != nil {
+ workDir = ""
+ }
- // change file name to relative if necessary
- if rel && workDir != "" {
- f = toRel(workDir, f)
- }
+ // add dir if match
+ files = append(files, dir)
- // test matching of file to the argument
- if match.File(f, a.Last) {
- prediction = append(prediction, f)
- }
+ // add all matching files to prediction
+ for _, f := range files {
+ if stat, err := os.Stat(f); err != nil || (!stat.IsDir() && !allowFiles) {
+ continue
+ }
+
+ // change file name to relative if necessary
+ if rel && workDir != "" {
+ f = toRel(workDir, f)
+ }
+
+ // test matching of file to the argument
+ if match.File(f, last) {
+ prediction = append(prediction, f)
}
- return
}
+ return
+
}
func listFiles(dir, pattern string) []string {