author | Mikael Berthe <mikael@lilotux.net> |
Sun, 11 Jul 2021 10:35:56 +0200 | |
changeset 256 | 6d9efbef00a9 |
parent 251 | 1c52a0eeb952 |
child 260 | 445e01aede7e |
permissions | -rw-r--r-- |
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
1 |
package cobra |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
2 |
|
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
3 |
import ( |
256 | 4 |
"bytes" |
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
5 |
"fmt" |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
6 |
"io" |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
7 |
"os" |
251 | 8 |
) |
9 |
||
256 | 10 |
// GenZshCompletionFile generates zsh completion file including descriptions. |
11 |
func (c *Command) GenZshCompletionFile(filename string) error { |
|
12 |
return c.genZshCompletionFile(filename, true) |
|
13 |
} |
|
251 | 14 |
|
256 | 15 |
// GenZshCompletion generates zsh completion file including descriptions |
16 |
// and writes it to the passed writer. |
|
17 |
func (c *Command) GenZshCompletion(w io.Writer) error { |
|
18 |
return c.genZshCompletion(w, true) |
|
251 | 19 |
} |
20 |
||
256 | 21 |
// GenZshCompletionFileNoDesc generates zsh completion file without descriptions. |
22 |
func (c *Command) GenZshCompletionFileNoDesc(filename string) error { |
|
23 |
return c.genZshCompletionFile(filename, false) |
|
24 |
} |
|
251 | 25 |
|
256 | 26 |
// GenZshCompletionNoDesc generates zsh completion file without descriptions |
27 |
// and writes it to the passed writer. |
|
28 |
func (c *Command) GenZshCompletionNoDesc(w io.Writer) error { |
|
29 |
return c.genZshCompletion(w, false) |
|
251 | 30 |
} |
31 |
||
256 | 32 |
// MarkZshCompPositionalArgumentFile only worked for zsh and its behavior was |
33 |
// not consistent with Bash completion. It has therefore been disabled. |
|
34 |
// Instead, when no other completion is specified, file completion is done by |
|
35 |
// default for every argument. One can disable file completion on a per-argument |
|
36 |
// basis by using ValidArgsFunction and ShellCompDirectiveNoFileComp. |
|
37 |
// To achieve file extension filtering, one can use ValidArgsFunction and |
|
38 |
// ShellCompDirectiveFilterFileExt. |
|
39 |
// |
|
40 |
// Deprecated |
|
41 |
func (c *Command) MarkZshCompPositionalArgumentFile(argPosition int, patterns ...string) error { |
|
42 |
return nil |
|
43 |
} |
|
44 |
||
45 |
// MarkZshCompPositionalArgumentWords only worked for zsh. It has therefore |
|
46 |
// been disabled. |
|
47 |
// To achieve the same behavior across all shells, one can use |
|
48 |
// ValidArgs (for the first argument only) or ValidArgsFunction for |
|
49 |
// any argument (can include the first one also). |
|
50 |
// |
|
51 |
// Deprecated |
|
52 |
func (c *Command) MarkZshCompPositionalArgumentWords(argPosition int, words ...string) error { |
|
53 |
return nil |
|
54 |
} |
|
55 |
||
56 |
func (c *Command) genZshCompletionFile(filename string, includeDesc bool) error { |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 |
outFile, err := os.Create(filename) |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
58 |
if err != nil { |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 |
return err |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 |
} |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 |
defer outFile.Close() |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 |
|
256 | 63 |
return c.genZshCompletion(outFile, includeDesc) |
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
} |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
65 |
|
256 | 66 |
func (c *Command) genZshCompletion(w io.Writer, includeDesc bool) error { |
67 |
buf := new(bytes.Buffer) |
|
68 |
genZshComp(buf, c.Name(), includeDesc) |
|
69 |
_, err := buf.WriteTo(w) |
|
70 |
return err |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 |
} |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 |
|
256 | 73 |
func genZshComp(buf io.StringWriter, name string, includeDesc bool) { |
74 |
compCmd := ShellCompRequestCmd |
|
75 |
if !includeDesc { |
|
76 |
compCmd = ShellCompNoDescRequestCmd |
|
251 | 77 |
} |
256 | 78 |
WriteStringAndCheck(buf, fmt.Sprintf(`#compdef _%[1]s %[1]s |
79 |
||
80 |
# zsh completion for %-36[1]s -*- shell-script -*- |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
81 |
|
256 | 82 |
__%[1]s_debug() |
83 |
{ |
|
84 |
local file="$BASH_COMP_DEBUG_FILE" |
|
85 |
if [[ -n ${file} ]]; then |
|
86 |
echo "$*" >> "${file}" |
|
87 |
fi |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 |
} |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 |
|
256 | 90 |
_%[1]s() |
91 |
{ |
|
92 |
local shellCompDirectiveError=%[3]d |
|
93 |
local shellCompDirectiveNoSpace=%[4]d |
|
94 |
local shellCompDirectiveNoFileComp=%[5]d |
|
95 |
local shellCompDirectiveFilterFileExt=%[6]d |
|
96 |
local shellCompDirectiveFilterDirs=%[7]d |
|
97 |
||
98 |
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace |
|
99 |
local -a completions |
|
100 |
||
101 |
__%[1]s_debug "\n========= starting completion logic ==========" |
|
102 |
__%[1]s_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}" |
|
103 |
||
104 |
# The user could have moved the cursor backwards on the command-line. |
|
105 |
# We need to trigger completion from the $CURRENT location, so we need |
|
106 |
# to truncate the command-line ($words) up to the $CURRENT location. |
|
107 |
# (We cannot use $CURSOR as its value does not work when a command is an alias.) |
|
108 |
words=("${=words[1,CURRENT]}") |
|
109 |
__%[1]s_debug "Truncated words[*]: ${words[*]}," |
|
110 |
||
111 |
lastParam=${words[-1]} |
|
112 |
lastChar=${lastParam[-1]} |
|
113 |
__%[1]s_debug "lastParam: ${lastParam}, lastChar: ${lastChar}" |
|
114 |
||
115 |
# For zsh, when completing a flag with an = (e.g., %[1]s -n=<TAB>) |
|
116 |
# completions must be prefixed with the flag |
|
117 |
setopt local_options BASH_REMATCH |
|
118 |
if [[ "${lastParam}" =~ '-.*=' ]]; then |
|
119 |
# We are dealing with a flag with an = |
|
120 |
flagPrefix="-P ${BASH_REMATCH}" |
|
121 |
fi |
|
251 | 122 |
|
256 | 123 |
# Prepare the command to obtain completions |
124 |
requestComp="${words[1]} %[2]s ${words[2,-1]}" |
|
125 |
if [ "${lastChar}" = "" ]; then |
|
126 |
# If the last parameter is complete (there is a space following it) |
|
127 |
# We add an extra empty parameter so we can indicate this to the go completion code. |
|
128 |
__%[1]s_debug "Adding extra empty parameter" |
|
129 |
requestComp="${requestComp} \"\"" |
|
130 |
fi |
|
131 |
||
132 |
__%[1]s_debug "About to call: eval ${requestComp}" |
|
133 |
||
134 |
# Use eval to handle any environment variables and such |
|
135 |
out=$(eval ${requestComp} 2>/dev/null) |
|
136 |
__%[1]s_debug "completion output: ${out}" |
|
137 |
||
138 |
# Extract the directive integer following a : from the last line |
|
139 |
local lastLine |
|
140 |
while IFS='\n' read -r line; do |
|
141 |
lastLine=${line} |
|
142 |
done < <(printf "%%s\n" "${out[@]}") |
|
143 |
__%[1]s_debug "last line: ${lastLine}" |
|
144 |
||
145 |
if [ "${lastLine[1]}" = : ]; then |
|
146 |
directive=${lastLine[2,-1]} |
|
147 |
# Remove the directive including the : and the newline |
|
148 |
local suffix |
|
149 |
(( suffix=${#lastLine}+2)) |
|
150 |
out=${out[1,-$suffix]} |
|
151 |
else |
|
152 |
# There is no directive specified. Leave $out as is. |
|
153 |
__%[1]s_debug "No directive found. Setting do default" |
|
154 |
directive=0 |
|
155 |
fi |
|
156 |
||
157 |
__%[1]s_debug "directive: ${directive}" |
|
158 |
__%[1]s_debug "completions: ${out}" |
|
159 |
__%[1]s_debug "flagPrefix: ${flagPrefix}" |
|
160 |
||
161 |
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then |
|
162 |
__%[1]s_debug "Completion received error. Ignoring completions." |
|
163 |
return |
|
164 |
fi |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
165 |
|
256 | 166 |
while IFS='\n' read -r comp; do |
167 |
if [ -n "$comp" ]; then |
|
168 |
# If requested, completions are returned with a description. |
|
169 |
# The description is preceded by a TAB character. |
|
170 |
# For zsh's _describe, we need to use a : instead of a TAB. |
|
171 |
# We first need to escape any : as part of the completion itself. |
|
172 |
comp=${comp//:/\\:} |
|
173 |
||
174 |
local tab=$(printf '\t') |
|
175 |
comp=${comp//$tab/:} |
|
176 |
||
177 |
__%[1]s_debug "Adding completion: ${comp}" |
|
178 |
completions+=${comp} |
|
179 |
lastComp=$comp |
|
180 |
fi |
|
181 |
done < <(printf "%%s\n" "${out[@]}") |
|
182 |
||
183 |
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then |
|
184 |
__%[1]s_debug "Activating nospace." |
|
185 |
noSpace="-S ''" |
|
186 |
fi |
|
187 |
||
188 |
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then |
|
189 |
# File extension filtering |
|
190 |
local filteringCmd |
|
191 |
filteringCmd='_files' |
|
192 |
for filter in ${completions[@]}; do |
|
193 |
if [ ${filter[1]} != '*' ]; then |
|
194 |
# zsh requires a glob pattern to do file filtering |
|
195 |
filter="\*.$filter" |
|
196 |
fi |
|
197 |
filteringCmd+=" -g $filter" |
|
198 |
done |
|
199 |
filteringCmd+=" ${flagPrefix}" |
|
251 | 200 |
|
256 | 201 |
__%[1]s_debug "File filtering command: $filteringCmd" |
202 |
_arguments '*:filename:'"$filteringCmd" |
|
203 |
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then |
|
204 |
# File completion for directories only |
|
205 |
local subDir |
|
206 |
subdir="${completions[1]}" |
|
207 |
if [ -n "$subdir" ]; then |
|
208 |
__%[1]s_debug "Listing directories in $subdir" |
|
209 |
pushd "${subdir}" >/dev/null 2>&1 |
|
210 |
else |
|
211 |
__%[1]s_debug "Listing directories in ." |
|
212 |
fi |
|
251 | 213 |
|
256 | 214 |
local result |
215 |
_arguments '*:dirname:_files -/'" ${flagPrefix}" |
|
216 |
result=$? |
|
217 |
if [ -n "$subdir" ]; then |
|
218 |
popd >/dev/null 2>&1 |
|
219 |
fi |
|
220 |
return $result |
|
221 |
else |
|
222 |
__%[1]s_debug "Calling _describe" |
|
223 |
if eval _describe "completions" completions $flagPrefix $noSpace; then |
|
224 |
__%[1]s_debug "_describe found some completions" |
|
225 |
||
226 |
# Return the success of having called _describe |
|
227 |
return 0 |
|
228 |
else |
|
229 |
__%[1]s_debug "_describe did not find completions." |
|
230 |
__%[1]s_debug "Checking if we should do file completion." |
|
231 |
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then |
|
232 |
__%[1]s_debug "deactivating file completion" |
|
233 |
||
234 |
# We must return an error code here to let zsh know that there were no |
|
235 |
# completions found by _describe; this is what will trigger other |
|
236 |
# matching algorithms to attempt to find completions. |
|
237 |
# For example zsh can match letters in the middle of words. |
|
238 |
return 1 |
|
239 |
else |
|
240 |
# Perform file completion |
|
241 |
__%[1]s_debug "Activating file completion" |
|
242 |
||
243 |
# We must return the result of this command, so it must be the |
|
244 |
# last command, or else we must store its result to return it. |
|
245 |
_arguments '*:filename:_files'" ${flagPrefix}" |
|
246 |
fi |
|
247 |
fi |
|
248 |
fi |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
249 |
} |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
250 |
|
256 | 251 |
# don't run the completion function when being source-ed or eval-ed |
252 |
if [ "$funcstack[1]" = "_%[1]s" ]; then |
|
253 |
_%[1]s |
|
254 |
fi |
|
255 |
`, name, compCmd, |
|
256 |
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, |
|
257 |
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs)) |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
258 |
} |