-
Notifications
You must be signed in to change notification settings - Fork 5
/
prefix_file_predictor.go
92 lines (74 loc) · 2.06 KB
/
prefix_file_predictor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package predictor
import (
"os"
"path/filepath"
"strings"
"github.com/posener/complete"
cst "github.com/DelineaXPM/dsv-cli/constants"
)
type PrefixFilePredictor struct {
filePattern string
}
func NewPrefixFilePredictor(pattern string) *PrefixFilePredictor {
return &PrefixFilePredictor{filePattern: pattern}
}
func (p PrefixFilePredictor) Predict(a complete.Args) (prediction []string) {
last := a.Last
if !strings.HasPrefix(last, cst.CmdFilePrefix) {
return []string{}
}
allowFiles := true
prediction = predictFiles(a, p.filePattern, allowFiles, cst.CmdFilePrefix)
// 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
}
// only try deeper, if the one item is a directory
if stat, err := os.Stat(prediction[0]); err != nil || !stat.IsDir() {
return
}
a.Last = prediction[0]
return predictFiles(a, p.filePattern, allowFiles, cst.CmdFilePrefix)
}
func predictFiles(a complete.Args, pattern string, allowFiles bool, prefix string) []string {
if strings.HasSuffix(a.Last, "/..") {
return nil
}
argsWithRealPath := complete.Args{
All: a.All,
Completed: a.Completed,
Last: a.Last[len(prefix):],
LastCompleted: a.LastCompleted,
}
dir := argsWithRealPath.Directory()
files := listFiles(dir, pattern, allowFiles, prefix)
// add dir if match
files = append(files, dir)
return complete.PredictFilesSet(files).Predict(a)
}
func listFiles(dir, pattern string, allowFiles bool, prefix string) []string {
// set of all file names
m := map[string]bool{}
// list files
if files, err := filepath.Glob(filepath.Join(dir, pattern)); err == nil {
for _, f := range files {
if stat, err := os.Stat(f); err != nil || stat.IsDir() || allowFiles {
m[f] = true
}
}
}
// list directories
if dirs, err := os.ReadDir(dir); err == nil {
for _, d := range dirs {
if d.IsDir() {
m[filepath.Join(dir, d.Name())] = true
}
}
}
list := make([]string, 0, len(m))
for k := range m {
list = append(list, prefix+k)
}
return list
}