-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletion.go
78 lines (65 loc) · 1.75 KB
/
completion.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
package cmdmux
import (
"fmt"
"io"
)
const (
header = "# bash completion file for %s\n" +
"# Copy this file to somewhere (e.g. ~/.%s-completion)\n" +
"# and then '$ source ~/.%s-completion'\n\n"
body = ` local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
`
end = ` *)
local prev2="${COMP_WORDS[COMP_CWORD-2]}"
;;
esac
COMPREPLY=( $(compgen -W "$opts" -- $cur) )
return 0
}
`
entryHead = ` COMPREPLY=( $(compgen -W "`
entryEnd = `" -- $cur) )
return 0
;;
`
)
func generateEntry(node *cmdNode, depth int, data interface{}) {
if len(node.subNodes) == 0 {
return
}
w := data.(io.Writer)
fmt.Fprintf(w, " %s )\n", node.name)
fmt.Fprintf(w, entryHead)
for _, sub := range node.subNodes {
fmt.Fprintf(w, "%s ", sub.name)
}
fmt.Fprintf(w, entryEnd)
}
func generateEntries(w io.Writer, node *cmdNode) {
walkByDepth(node.subNodes, 0, generateEntry, w)
}
// GenerateCompletion generates a *bash* completion file with the program name.
func (c *CmdMux) GenerateCompletion(program string, w io.Writer) error {
fmt.Fprintf(w, header, program, program, program)
fmt.Fprintf(w, "_%s()\n{\n", program)
fmt.Fprintf(w, body)
// 1. list all depth 1 nodes into $opts
fmt.Fprintf(w, ` opts="`)
for _, v := range c.root.subNodes {
fmt.Fprintf(w, "%s ", v.name)
}
fmt.Fprintf(w, "\"\n\n")
fmt.Fprintln(w, ` case "$prev" in`)
// 2. create entry for every node which has sub-node.
generateEntries(w, c.root)
fmt.Fprintf(w, end)
fmt.Fprintf(w, "complete -F _%s %s\n", program, program)
return nil
}
// GenerateCompletion generates a *bash* completion file with the program name.
func GenerateCompletion(program string, w io.Writer) error {
return std.GenerateCompletion(program, w)
}