-
Notifications
You must be signed in to change notification settings - Fork 6
/
bash.go
196 lines (148 loc) · 5 KB
/
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Package bash provides methods for generating bash completion
package bash
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"strings"
"github.com/essentialkaos/ek/v12/usage"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// _BASH_TEMPLATE is template used for completion generation
const _BASH_TEMPLATE = `# Completion for {{COMPNAME}}
# This completion is automatically generated
_{{COMPNAME_SAFE}}() {
local cur prev cmds opts show_files
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmds="{{COMMANDS}}"
opts="{{GLOBAL_OPTIONS}}"
show_files="{{SHOW_FILES}}"
file_glob="{{FILE_GLOB}}"
{{COMMANDS_HANDLERS}}
if [[ $cur == -* ]] ; then
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
return 0
fi
_{{COMPNAME_SAFE}}_filter "$cmds" "$opts" "$show_files" "$file_glob"
}
_{{COMPNAME_SAFE}}_filter() {
local cmds="$1"
local opts="$2"
local show_files="$3"
local file_glob="$4"
local cmd1 cmd2
for cmd1 in $cmds ; do
for cmd2 in ${COMP_WORDS[*]} ; do
if [[ "$cmd1" == "$cmd2" ]] ; then
if [[ -z "$show_files" ]] ; then
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
else
_filedir "$file_glob"
fi
return 0
fi
done
done
if [[ -z "$show_files" ]] ; then
COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
return 0
fi
_filedir "$file_glob"
}
complete -F _{{COMPNAME_SAFE}} {{COMPNAME}} {{COMP_OPTS}}
`
// ////////////////////////////////////////////////////////////////////////////////// //
// Generate generates Bash completion code
func Generate(info *usage.Info, name string, fileExt ...string) string {
result := _BASH_TEMPLATE
result = strings.Replace(result, "{{COMMANDS}}", genCommandsList(info), -1)
result = strings.Replace(result, "{{GLOBAL_OPTIONS}}", genGlobalOptionsList(info), -1)
result = strings.Replace(result, "{{COMMANDS_HANDLERS}}", genCommandsHandlers(info), -1)
result = strings.Replace(result, "{{COMPNAME}}", name, -1)
if len(info.Args) != 0 {
result = strings.Replace(result, "{{SHOW_FILES}}", "true", -1)
result = strings.Replace(result, "{{COMP_OPTS}}", "-o filenames", -1)
if len(fileExt) != 0 {
result = strings.Replace(result, "{{FILE_GLOB}}", fileExt[0], -1)
}
} else {
result = strings.Replace(result, "{{SHOW_FILES}}", "", -1)
result = strings.Replace(result, "{{COMP_OPTS}}", "", -1)
result = strings.Replace(result, "{{FILE_GLOB}}", "", -1)
}
nameSafe := strings.Replace(name, "-", "_", -1)
result = strings.Replace(result, "{{COMPNAME_SAFE}}", nameSafe, -1)
return result
}
// ////////////////////////////////////////////////////////////////////////////////// //
// genGlobalOptionsList generates list with global options
func genGlobalOptionsList(info *usage.Info) string {
var result []string
nonGlobalOptions := make(map[string]bool)
for _, cmd := range info.Commands {
for _, opt := range cmd.BoundOptions {
nonGlobalOptions[opt] = true
}
}
for _, opt := range info.Options {
if nonGlobalOptions[opt.Long] {
continue
}
result = append(result, "--"+opt.Long)
}
return strings.Join(result, " ")
}
// genCommandsHandlers generates command handler
func genCommandsHandlers(info *usage.Info) string {
if !isCommandHandlersRequired(info) {
return ""
}
result := " case $prev in\n"
for _, cmd := range info.Commands {
if len(cmd.BoundOptions) != 0 {
result += genCommandHandler(cmd, info)
}
}
result += " esac"
return result
}
// genCommandHandler generates handler for given command
func genCommandHandler(cmd *usage.Command, info *usage.Info) string {
result := fmt.Sprintf(" %s)\n", cmd.Name)
var options []string
for _, optName := range cmd.BoundOptions {
opt := info.GetOption(optName)
if opt == nil {
continue
}
options = append(options, "--"+opt.Long)
}
result += fmt.Sprintf(" opts=\"%s\"\n", strings.Join(options, " "))
result += " COMPREPLY=($(compgen -W \"$opts\" -- \"$cur\"))\n"
result += " return 0\n"
result += " ;;\n\n"
return result
}
// getCommandsList returns slice with available commands
func genCommandsList(info *usage.Info) string {
var result []string
for _, command := range info.Commands {
result = append(result, command.Name)
}
return strings.Join(result, " ")
}
// isCommandHandlersRequired returns true if commands have bound options
func isCommandHandlersRequired(info *usage.Info) bool {
for _, cmd := range info.Commands {
if len(cmd.BoundOptions) != 0 {
return true
}
}
return false
}