-
Notifications
You must be signed in to change notification settings - Fork 1
/
compgen.go
139 lines (116 loc) · 2.61 KB
/
compgen.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/yuin/gopher-lua"
)
type compgenerator interface {
compgen(L *lua.LState, compWords []string, compCWords int) string
}
type funcCompgen struct {
f *lua.LFunction
}
func (sc *funcCompgen) compgen(L *lua.LState, compWords []string, compCWords int) string {
tbl := L.NewTable()
for _, v := range compWords {
tbl.Append(lua.LString(v))
}
if err := L.CallByParam(lua.P{
Fn: sc.f,
NRet: 1,
Protect: true,
}, tbl, lua.LNumber(compCWords)); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
ret := L.Get(-1)
L.Pop(1)
emit("Got %v", ret.String())
return ret.String()
}
type strCompgen struct {
s string
}
func (sc *strCompgen) compgen(L *lua.LState, compWords []string, compCWords int) string {
return sc.s
}
func shift(slice []string) []string {
if len(slice) > 1 {
return slice[1:]
}
return make([]string, 0)
}
func printFlags() {
var s []string
flag.VisitAll(func(fl *flag.Flag) {
s = append(s, "-"+fl.Name)
})
fmt.Printf("%v", strings.Join(s, " "))
}
func compgen() {
// create and shift of the program name
args := flag.Args()
args = shift(args)
index := 1
prev := ""
// analyse flags, since it has to start with "-" len(args) must be greater then 0
for len(args) > 0 {
if string(args[0][0]) == "-" || prev == "-f" {
prev, args = args[0], shift(args)
if flg.compCWords == index {
printFlags()
return
}
index++
} else {
break
}
}
// setup Lua environment
L, _, _ := setupEnv()
// analyse runner targets
if flg.compCWords == index {
var s []string
for target := range subcommands {
if target == "" {
continue
}
s = append(s, target)
}
fmt.Printf("%v", strings.Join(s, " "))
return
}
// pass it on to the runner target
target := args[0]
if cmd, ok := subcommands[target]; ok && cmd.compgen != nil {
fmt.Printf("%v", cmd.compgen.compgen(L, args, flg.compCWords-index))
}
}
// blade -generate-bash-conf | sudo tee /etc/bash_completion.d/blade
func generateBashConfig() {
conf := `_blade()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ${prev} == -f ]]; then
if [[ $(declare -f _filedir) ]]; then
_filedir
else
COMPREPLY=( $(compgen -f -- ${cur}) )
fi
return 0
fi
flags=$(echo "${COMP_WORDS[@]}")
flag=$(expr "${flags}" : '.*\(-f [^ ]* *\)')
opts=$(blade $flag -compgen -comp-cwords $COMP_CWORD ${COMP_WORDS[@]})
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _blade blade
`
fmt.Print(conf)
}