73 func genZshComp(buf io.StringWriter, name string, includeDesc bool) { |
73 func genZshComp(buf io.StringWriter, name string, includeDesc bool) { |
74 compCmd := ShellCompRequestCmd |
74 compCmd := ShellCompRequestCmd |
75 if !includeDesc { |
75 if !includeDesc { |
76 compCmd = ShellCompNoDescRequestCmd |
76 compCmd = ShellCompNoDescRequestCmd |
77 } |
77 } |
78 WriteStringAndCheck(buf, fmt.Sprintf(`#compdef _%[1]s %[1]s |
78 WriteStringAndCheck(buf, fmt.Sprintf(`#compdef %[1]s |
79 |
79 |
80 # zsh completion for %-36[1]s -*- shell-script -*- |
80 # zsh completion for %-36[1]s -*- shell-script -*- |
81 |
81 |
82 __%[1]s_debug() |
82 __%[1]s_debug() |
83 { |
83 { |
161 if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then |
161 if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then |
162 __%[1]s_debug "Completion received error. Ignoring completions." |
162 __%[1]s_debug "Completion received error. Ignoring completions." |
163 return |
163 return |
164 fi |
164 fi |
165 |
165 |
|
166 local activeHelpMarker="%[8]s" |
|
167 local endIndex=${#activeHelpMarker} |
|
168 local startIndex=$((${#activeHelpMarker}+1)) |
|
169 local hasActiveHelp=0 |
166 while IFS='\n' read -r comp; do |
170 while IFS='\n' read -r comp; do |
|
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 |
167 if [ -n "$comp" ]; then |
184 if [ -n "$comp" ]; then |
168 # If requested, completions are returned with a description. |
185 # If requested, completions are returned with a description. |
169 # The description is preceded by a TAB character. |
186 # The description is preceded by a TAB character. |
170 # For zsh's _describe, we need to use a : instead of a TAB. |
187 # 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. |
188 # We first need to escape any : as part of the completion itself. |
172 comp=${comp//:/\\:} |
189 comp=${comp//:/\\:} |
173 |
190 |
174 local tab=$(printf '\t') |
191 local tab="$(printf '\t')" |
175 comp=${comp//$tab/:} |
192 comp=${comp//$tab/:} |
176 |
193 |
177 __%[1]s_debug "Adding completion: ${comp}" |
194 __%[1]s_debug "Adding completion: ${comp}" |
178 completions+=${comp} |
195 completions+=${comp} |
179 lastComp=$comp |
196 lastComp=$comp |
180 fi |
197 fi |
181 done < <(printf "%%s\n" "${out[@]}") |
198 done < <(printf "%%s\n" "${out[@]}") |
|
199 |
|
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 |
182 |
210 |
183 if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then |
211 if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then |
184 __%[1]s_debug "Activating nospace." |
212 __%[1]s_debug "Activating nospace." |
185 noSpace="-S ''" |
213 noSpace="-S ''" |
186 fi |
214 fi |
200 |
228 |
201 __%[1]s_debug "File filtering command: $filteringCmd" |
229 __%[1]s_debug "File filtering command: $filteringCmd" |
202 _arguments '*:filename:'"$filteringCmd" |
230 _arguments '*:filename:'"$filteringCmd" |
203 elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then |
231 elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then |
204 # File completion for directories only |
232 # File completion for directories only |
205 local subDir |
233 local subdir |
206 subdir="${completions[1]}" |
234 subdir="${completions[1]}" |
207 if [ -n "$subdir" ]; then |
235 if [ -n "$subdir" ]; then |
208 __%[1]s_debug "Listing directories in $subdir" |
236 __%[1]s_debug "Listing directories in $subdir" |
209 pushd "${subdir}" >/dev/null 2>&1 |
237 pushd "${subdir}" >/dev/null 2>&1 |
210 else |
238 else |
248 fi |
276 fi |
249 } |
277 } |
250 |
278 |
251 # don't run the completion function when being source-ed or eval-ed |
279 # don't run the completion function when being source-ed or eval-ed |
252 if [ "$funcstack[1]" = "_%[1]s" ]; then |
280 if [ "$funcstack[1]" = "_%[1]s" ]; then |
253 _%[1]s |
281 _%[1]s |
254 fi |
282 fi |
255 `, name, compCmd, |
283 `, name, compCmd, |
256 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, |
284 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, |
257 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs)) |
285 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, |
258 } |
286 activeHelpMarker)) |
|
287 } |