diff options
| author | Eyal Posener <[email protected]> | 2017-05-12 22:43:33 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-05-12 22:46:01 +0300 |
| commit | 66402080675c250edabe50839555942d1b1aa189 (patch) | |
| tree | 42c857d1815f215ca610bc47331425ea20ed3cc3 /predict_files.go | |
| parent | 136e52e074be4a85fb9e9bccf93d060c46e67561 (diff) | |
Recursive directory lookup
When only one directory matches the result, search recursively
whithin this directory for files.
Diffstat (limited to 'predict_files.go')
| -rw-r--r-- | predict_files.go | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/predict_files.go b/predict_files.go index 8ad5368..fe70c97 100644 --- a/predict_files.go +++ b/predict_files.go @@ -25,9 +25,35 @@ func PredictFiles(pattern string) Predictor { } func files(pattern string, allowFiles bool) PredictFunc { + + // search for files according to arguments, + // if only one directory has matched the result, search recursively into + // this directory to give more results. return func(a Args) (prediction []string) { - prediction = predictFiles(a.Last, pattern, allowFiles) - return + last := a.Last + for { + + prediction = predictFiles(last, pattern, allowFiles) + + // if the number of prediction is not 1, we either have many results or + // have no results, so we return it. + if len(prediction) != 1 { + return + } + + // if the result is only one item, we might want to recursively check + // for more accurate results. + if prediction[0] == last { // avoid loop forever + return + } + + // only try deeper, if the one item is a directory + if stat, err := os.Stat(prediction[0]); err != nil || !stat.IsDir() { + return + } + + last = prediction[0] + } } } |
