Skip to content

Commit 5feb067

Browse files
authored
Merge pull request #226 from carapace-sh/argcomplete-legacy
argcomplete: re-added legacy version for gcloud
2 parents a950ca3 + 94e71bc commit 5feb067

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package bridge
2+
3+
import (
4+
"os/exec"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/carapace-sh/carapace"
9+
)
10+
11+
// Deprecated: Old version which uses fd 8/9, which aren't available on powershell/windows.
12+
func ActionArgcompleteLegacy(command ...string) carapace.Action {
13+
return actionCommand(command...)(func(command ...string) carapace.Action {
14+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
15+
if _, err := exec.LookPath(command[0]); err != nil {
16+
return carapace.ActionMessage(err.Error())
17+
}
18+
19+
args := append(command[1:], c.Args...)
20+
current := c.Value
21+
22+
prefix := ""
23+
if strings.HasPrefix(current, "--") {
24+
if strings.Contains(current, "=") { // optarg flag which is handled as normal arg by the completer
25+
splitted := strings.SplitN(current, "=", 2)
26+
prefix = splitted[0] + "="
27+
args = append(args, splitted[0]) // add flag as arg
28+
current = "" // seem partial optarg value isn't completed
29+
30+
} else {
31+
current = "--" // seems partial flag names aren't completed so get all
32+
}
33+
} else {
34+
current = "" // seems partial positional arguments aren't completed as well
35+
}
36+
37+
compLine := command[0] + " " + strings.Join(append(args, current), " ") // TODO escape/quote special characters
38+
c.Setenv("_ARGCOMPLETE", "1")
39+
c.Setenv("_ARGCOMPLETE_DFS", "\t")
40+
c.Setenv("_ARGCOMPLETE_IFS", "\n")
41+
c.Setenv("_ARGCOMPLETE_SHELL", "fish")
42+
c.Setenv("_ARGCOMPLETE_SUPPRESS_SPACE", "1") // TODO needed? relevant for nospace detection?
43+
// c.Setenv("_ARGCOMPLETE_COMP_WORDBREAKS", " ") // TODO set to space-only for multiparts?
44+
c.Setenv("_ARGCOMPLETE", "1")
45+
c.Setenv("COMP_LINE", compLine)
46+
c.Setenv("COMP_POINT", strconv.Itoa(len(compLine)))
47+
nospace := false
48+
a := carapace.ActionExecCommand("sh", "-c", command[0]+" 8>&1 9>&2 1>/dev/null 2>/dev/null")(func(output []byte) carapace.Action {
49+
lines := strings.Split(string(output), "\n")
50+
vals := make([]string, 0)
51+
isFlag := strings.HasPrefix(c.Value, "-")
52+
for _, line := range lines[:len(lines)-1] {
53+
if !isFlag && strings.HasPrefix(line, "-") {
54+
continue
55+
}
56+
if strings.HasSuffix(line, "=") ||
57+
strings.HasSuffix(line, "/") ||
58+
strings.HasSuffix(line, ",") {
59+
nospace = true
60+
}
61+
if splitted := strings.SplitN(line, "\t", 2); splitted[0] != "" {
62+
vals = append(vals, splitted...)
63+
if len(splitted) < 2 {
64+
vals = append(vals, "")
65+
}
66+
}
67+
}
68+
69+
if len(vals) == 0 {
70+
// fallback to file completions when no values returned
71+
if index := strings.Index(c.Value, "="); index > -1 {
72+
return carapace.ActionFiles().Invoke(carapace.Context{Value: c.Value[index+1:]}).ToA()
73+
}
74+
return carapace.ActionFiles()
75+
}
76+
return carapace.ActionValuesDescribed(vals...)
77+
}).Invoke(c).Prefix(prefix).ToA() // re-add optarg prefix
78+
if nospace {
79+
return a.NoSpace()
80+
}
81+
return a
82+
})
83+
})
84+
}

0 commit comments

Comments
 (0)