-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletion_bash.go
73 lines (68 loc) · 1.96 KB
/
completion_bash.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
package completion
import (
"fmt"
"strings"
)
func bash(cmd *command) string {
var bash string
bash += fmt.Sprintf("#!/bin/bash\n")
bash += bashFunctions(cmd)
bash += fmt.Sprintf("\ncomplete -F _%s %s\n", cmd.Name, cmd.Name)
return bash
}
func bashFunctions(cmd *command) string {
var function string
function += fmt.Sprintf("\n%s() {\n", cmd.FullName())
if cmd.HasSubCommands() || cmd.HasFlags() {
function += fmt.Sprintf("\tif [ $COMP_CWORD -eq %d ]; then\n", cmd.Level())
function += bashRootCompReply(cmd)
if cmd.HasSubCommands() {
function += fmt.Sprintf("\telif [ $COMP_CWORD -ge %d ]; then\n", cmd.Level()+1)
function += bashSubCommandsCompReply(cmd)
}
function += fmt.Sprintf("\tfi\n")
}
function += fmt.Sprintf("\treturn 0\n")
function += fmt.Sprintf("}\n")
if cmd.HasSubCommands() {
for _, c := range cmd.SubCommands {
function += bashFunctions(c)
}
}
return function
}
func bashRootCompReply(cmd *command) string {
var compReply string
if cmd.HasSubCommands() || cmd.HasFlags() {
if cmd.HasSubCommands() {
for _, c := range cmd.SubCommands {
compReply += fmt.Sprintf(" %s", c.Name)
}
}
if cmd.HasFlags() {
for _, f := range cmd.Flags {
compReply += fmt.Sprintf(" --%s", f.Name)
if f.HasShorthand() {
compReply += fmt.Sprintf(" -%s", f.Shorthand)
}
}
}
compReply = fmt.Sprintf("\t\tCOMPREPLY=($(compgen -W \"%s\" \"${COMP_WORDS[%d]}\"))\n",
strings.TrimPrefix(compReply, " "), cmd.Level(),
)
}
return compReply
}
func bashSubCommandsCompReply(cmd *command) string {
var subCommands string
if cmd.HasSubCommands() {
subCommands += fmt.Sprintf("\t\tcase \"${COMP_WORDS[%d]}\" in\n", cmd.Level())
for _, c := range cmd.SubCommands {
subCommands += fmt.Sprintf("\t\t\t\"%s\"%s)\n", c.Name, c.Alias.Format("|\"", "\""))
subCommands += fmt.Sprintf("\t\t\t\t%s\n", c.FullName())
subCommands += fmt.Sprintf("\t\t\t\t;;\n")
}
subCommands += fmt.Sprintf("\t\tesac\n")
}
return subCommands
}