-
Notifications
You must be signed in to change notification settings - Fork 35
/
set.go
115 lines (103 loc) · 2.93 KB
/
set.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package completion
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/mitchellh/go-ps"
)
func completionSet(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {
result := []Element{}
base := strings.ToUpper(params[len(params)-1])
for _, env1 := range os.Environ() {
if strings.HasPrefix(strings.ToUpper(env1), base) {
result = append(result, Element1(env1))
}
}
return result, nil
}
func completionDir(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {
return listUpDirs(ctx, ua, params[len(params)-1])
}
func completionCd(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {
list, err := completionDir(ctx, ua, params)
source := params[len(params)-1]
if len(source) < 1 || source[0] == '.' || strings.ContainsAny(source, "/\\:") {
return list, err
}
cdpath := os.Getenv("CDPATH")
if cdpath == "" {
return list, err
}
duplicatedCheckTable := make(map[string]struct{})
for _, element := range list {
name := element.String()
if os.IsPathSeparator(name[len(name)-1]) {
name = name[:len(name)-1]
}
duplicatedCheckTable[strings.ToUpper(name)] = struct{}{}
}
base := strings.ToUpper(source)
for _, cdpath1 := range filepath.SplitList(cdpath) {
if files, err := os.ReadDir(cdpath1); err == nil {
for _, file1 := range files {
if file1.IsDir() {
name := strings.ToUpper(file1.Name())
if strings.HasPrefix(name, base) {
if _, ok := duplicatedCheckTable[name]; !ok {
list = append(list, Element1(file1.Name()))
}
}
}
}
}
}
return list, nil
}
func completionEnv(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
eq := -1
for i := 1; i < len(param); i++ {
if strings.Contains(param[i], "=") {
eq = i
}
}
current := len(param) - 1
if current == eq || current == 1 {
return completionSet(ctx, ua, param)
} else if current == eq+1 {
return listUpCommands(ctx, param[current])
} else {
return nil, nil
}
}
func completionWhich(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
if len(param) == 2 {
return listUpCommands(ctx, param[len(param)-1])
}
return nil, nil
}
func completionProcessName(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
processes, err := ps.Processes()
if err != nil {
return nil, err
}
uniq := map[string]struct{}{}
base := strings.ToUpper(param[len(param)-1])
for _, ps1 := range processes {
name := ps1.Executable()
if strings.HasPrefix(strings.ToUpper(name), base) {
uniq[name] = struct{}{}
}
}
result := make([]Element, 0, len(uniq))
for name := range uniq {
result = append(result, Element1(name))
}
return result, nil
}
func completionTaskKill(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
if len(param) >= 3 && strings.EqualFold(param[len(param)-2], "/IM") {
return completionProcessName(ctx, ua, param)
}
return nil, nil
}