-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.go
70 lines (62 loc) · 2.87 KB
/
core.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
package spec
import (
"runtime"
"strings"
"github.com/carapace-sh/carapace"
)
func init() {
// modifiers added as dummy for completeness
addCoreMacro("chdir", MacroI(func(s string) carapace.Action { return carapace.ActionValues() }))
addCoreMacro("list", MacroI(func(s string) carapace.Action { return carapace.ActionValues() }))
addCoreMacro("multiparts", MacroI(func(s string) carapace.Action { return carapace.ActionValues() }))
addCoreMacro("nospace", MacroI(func(s string) carapace.Action { return carapace.ActionValues() }))
addCoreMacro("uniquelist", MacroI(func(s string) carapace.Action { return carapace.ActionValues() }))
addCoreMacro("directories", MacroN(carapace.ActionDirectories))
addCoreMacro("files", MacroV(carapace.ActionFiles))
addCoreMacro("executables", MacroV(carapace.ActionExecutables))
addCoreMacro("message", MacroI(func(s string) carapace.Action { return carapace.ActionMessage(s) }))
// TODO is there still use for this? addCoreMacro("noflag", MacroN(func() carapace.Action { return carapace.ActionValues() }).NoFlag())
addCoreMacro("spec", MacroI(ActionSpec))
addCoreMacro("", MacroI(func(s string) carapace.Action {
if runtime.GOOS == "windows" {
return shell("pwsh", s)
}
return shell("sh", s)
}))
addCoreMacro("bash", MacroI(func(s string) carapace.Action { return shell("bash", s) }))
addCoreMacro("elvish", MacroI(func(s string) carapace.Action { return shell("elvish", s) }))
addCoreMacro("fish", MacroI(func(s string) carapace.Action { return shell("fish", s) }))
addCoreMacro("ion", MacroI(func(s string) carapace.Action { return shell("ion", s) }))
addCoreMacro("nu", MacroI(func(s string) carapace.Action { return shell("nu", s) }))
addCoreMacro("osh", MacroI(func(s string) carapace.Action { return shell("osh", s) }))
addCoreMacro("pwsh", MacroI(func(s string) carapace.Action { return shell("pwsh", s) }))
addCoreMacro("sh", MacroI(func(s string) carapace.Action { return shell("sh", s) }))
addCoreMacro("xonsh", MacroI(func(s string) carapace.Action { return shell("xonsh", s) }))
addCoreMacro("zsh", MacroI(func(s string) carapace.Action { return shell("zsh", s) }))
}
func shell(shell, command string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if runtime.GOOS == "windows" &&
shell != "elvish" &&
shell != "nu" &&
shell != "pwsh" &&
shell != "xonsh" {
return carapace.ActionMessage("unsupported shell [%v]: %v", runtime.GOOS, shell)
}
args := []string{"-c", command}
if shell != "pwsh" {
args = append(args, "--")
}
args = append(args, c.Args...)
return carapace.ActionExecCommand(shell, args...)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
batch := carapace.Batch()
for _, line := range lines {
if line != "" {
batch = append(batch, parseValue(line))
}
}
return batch.ToA()
}).Invoke(c).ToA()
})
}