author | Mikael Berthe <mikael@lilotux.net> |
Tue, 23 Aug 2022 22:39:43 +0200 | |
changeset 260 | 445e01aede7e |
parent 256 | 6d9efbef00a9 |
child 265 | 05c40b36d3b2 |
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 |
} |
260 | 78 |
WriteStringAndCheck(buf, fmt.Sprintf(`#compdef %[1]s |
256 | 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 |
|
260 | 166 |
local activeHelpMarker="%[8]s" |
167 |
local endIndex=${#activeHelpMarker} |
|
168 |
local startIndex=$((${#activeHelpMarker}+1)) |
|
169 |
local hasActiveHelp=0 |
|
256 | 170 |
while IFS='\n' read -r comp; do |
260 | 171 |
# Check if this is an activeHelp statement (i.e., prefixed with $activeHelpMarker) |
172 |
if [ "${comp[1,$endIndex]}" = "$activeHelpMarker" ];then |
|
173 |
__%[1]s_debug "ActiveHelp found: $comp" |
|
174 |
comp="${comp[$startIndex,-1]}" |
|
175 |
if [ -n "$comp" ]; then |
|
176 |
compadd -x "${comp}" |
|
177 |
__%[1]s_debug "ActiveHelp will need delimiter" |
|
178 |
hasActiveHelp=1 |
|
179 |
fi |
|
180 |
||
181 |
continue |
|
182 |
fi |
|
183 |
||
256 | 184 |
if [ -n "$comp" ]; then |
185 |
# If requested, completions are returned with a description. |
|
186 |
# The description is preceded by a TAB character. |
|
187 |
# For zsh's _describe, we need to use a : instead of a TAB. |
|
188 |
# We first need to escape any : as part of the completion itself. |
|
189 |
comp=${comp//:/\\:} |
|
190 |
||
260 | 191 |
local tab="$(printf '\t')" |
256 | 192 |
comp=${comp//$tab/:} |
193 |
||
194 |
__%[1]s_debug "Adding completion: ${comp}" |
|
195 |
completions+=${comp} |
|
196 |
lastComp=$comp |
|
197 |
fi |
|
198 |
done < <(printf "%%s\n" "${out[@]}") |
|
199 |
||
260 | 200 |
# Add a delimiter after the activeHelp statements, but only if: |
201 |
# - there are completions following the activeHelp statements, or |
|
202 |
# - file completion will be performed (so there will be choices after the activeHelp) |
|
203 |
if [ $hasActiveHelp -eq 1 ]; then |
|
204 |
if [ ${#completions} -ne 0 ] || [ $((directive & shellCompDirectiveNoFileComp)) -eq 0 ]; then |
|
205 |
__%[1]s_debug "Adding activeHelp delimiter" |
|
206 |
compadd -x "--" |
|
207 |
hasActiveHelp=0 |
|
208 |
fi |
|
209 |
fi |
|
210 |
||
256 | 211 |
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then |
212 |
__%[1]s_debug "Activating nospace." |
|
213 |
noSpace="-S ''" |
|
214 |
fi |
|
215 |
||
216 |
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then |
|
217 |
# File extension filtering |
|
218 |
local filteringCmd |
|
219 |
filteringCmd='_files' |
|
220 |
for filter in ${completions[@]}; do |
|
221 |
if [ ${filter[1]} != '*' ]; then |
|
222 |
# zsh requires a glob pattern to do file filtering |
|
223 |
filter="\*.$filter" |
|
224 |
fi |
|
225 |
filteringCmd+=" -g $filter" |
|
226 |
done |
|
227 |
filteringCmd+=" ${flagPrefix}" |
|
251 | 228 |
|
256 | 229 |
__%[1]s_debug "File filtering command: $filteringCmd" |
230 |
_arguments '*:filename:'"$filteringCmd" |
|
231 |
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then |
|
232 |
# File completion for directories only |
|
260 | 233 |
local subdir |
256 | 234 |
subdir="${completions[1]}" |
235 |
if [ -n "$subdir" ]; then |
|
236 |
__%[1]s_debug "Listing directories in $subdir" |
|
237 |
pushd "${subdir}" >/dev/null 2>&1 |
|
238 |
else |
|
239 |
__%[1]s_debug "Listing directories in ." |
|
240 |
fi |
|
251 | 241 |
|
256 | 242 |
local result |
243 |
_arguments '*:dirname:_files -/'" ${flagPrefix}" |
|
244 |
result=$? |
|
245 |
if [ -n "$subdir" ]; then |
|
246 |
popd >/dev/null 2>&1 |
|
247 |
fi |
|
248 |
return $result |
|
249 |
else |
|
250 |
__%[1]s_debug "Calling _describe" |
|
251 |
if eval _describe "completions" completions $flagPrefix $noSpace; then |
|
252 |
__%[1]s_debug "_describe found some completions" |
|
253 |
||
254 |
# Return the success of having called _describe |
|
255 |
return 0 |
|
256 |
else |
|
257 |
__%[1]s_debug "_describe did not find completions." |
|
258 |
__%[1]s_debug "Checking if we should do file completion." |
|
259 |
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then |
|
260 |
__%[1]s_debug "deactivating file completion" |
|
261 |
||
262 |
# We must return an error code here to let zsh know that there were no |
|
263 |
# completions found by _describe; this is what will trigger other |
|
264 |
# matching algorithms to attempt to find completions. |
|
265 |
# For example zsh can match letters in the middle of words. |
|
266 |
return 1 |
|
267 |
else |
|
268 |
# Perform file completion |
|
269 |
__%[1]s_debug "Activating file completion" |
|
270 |
||
271 |
# We must return the result of this command, so it must be the |
|
272 |
# last command, or else we must store its result to return it. |
|
273 |
_arguments '*:filename:_files'" ${flagPrefix}" |
|
274 |
fi |
|
275 |
fi |
|
276 |
fi |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
277 |
} |
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
278 |
|
256 | 279 |
# don't run the completion function when being source-ed or eval-ed |
280 |
if [ "$funcstack[1]" = "_%[1]s" ]; then |
|
260 | 281 |
_%[1]s |
256 | 282 |
fi |
283 |
`, name, compCmd, |
|
284 |
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, |
|
260 | 285 |
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, |
286 |
activeHelpMarker)) |
|
242
2a9ec03fe5a1
Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
287 |
} |