podman
214 строк · 7.9 Кб
1#compdef podman-remote
2compdef _podman-remote podman-remote
3
4# zsh completion for podman-remote -*- shell-script -*-
5
6__podman-remote_debug()
7{
8local file="$BASH_COMP_DEBUG_FILE"
9if [[ -n ${file} ]]; then
10echo "$*" >> "${file}"
11fi
12}
13
14_podman-remote()
15{
16local shellCompDirectiveError=1
17local shellCompDirectiveNoSpace=2
18local shellCompDirectiveNoFileComp=4
19local shellCompDirectiveFilterFileExt=8
20local shellCompDirectiveFilterDirs=16
21local shellCompDirectiveKeepOrder=32
22
23local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
24local -a completions
25
26__podman-remote_debug "\n========= starting completion logic =========="
27__podman-remote_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
28
29# The user could have moved the cursor backwards on the command-line.
30# We need to trigger completion from the $CURRENT location, so we need
31# to truncate the command-line ($words) up to the $CURRENT location.
32# (We cannot use $CURSOR as its value does not work when a command is an alias.)
33words=("${=words[1,CURRENT]}")
34__podman-remote_debug "Truncated words[*]: ${words[*]},"
35
36lastParam=${words[-1]}
37lastChar=${lastParam[-1]}
38__podman-remote_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"
39
40# For zsh, when completing a flag with an = (e.g., podman-remote -n=<TAB>)
41# completions must be prefixed with the flag
42setopt local_options BASH_REMATCH
43if [[ "${lastParam}" =~ '-.*=' ]]; then
44# We are dealing with a flag with an =
45flagPrefix="-P ${BASH_REMATCH}"
46fi
47
48# Prepare the command to obtain completions
49requestComp="${words[1]} __complete ${words[2,-1]}"
50if [ "${lastChar}" = "" ]; then
51# If the last parameter is complete (there is a space following it)
52# We add an extra empty parameter so we can indicate this to the go completion code.
53__podman-remote_debug "Adding extra empty parameter"
54requestComp="${requestComp} \"\""
55fi
56
57__podman-remote_debug "About to call: eval ${requestComp}"
58
59# Use eval to handle any environment variables and such
60out=$(eval ${requestComp} 2>/dev/null)
61__podman-remote_debug "completion output: ${out}"
62
63# Extract the directive integer following a : from the last line
64local lastLine
65while IFS='\n' read -r line; do
66lastLine=${line}
67done < <(printf "%s\n" "${out[@]}")
68__podman-remote_debug "last line: ${lastLine}"
69
70if [ "${lastLine[1]}" = : ]; then
71directive=${lastLine[2,-1]}
72# Remove the directive including the : and the newline
73local suffix
74(( suffix=${#lastLine}+2))
75out=${out[1,-$suffix]}
76else
77# There is no directive specified. Leave $out as is.
78__podman-remote_debug "No directive found. Setting do default"
79directive=0
80fi
81
82__podman-remote_debug "directive: ${directive}"
83__podman-remote_debug "completions: ${out}"
84__podman-remote_debug "flagPrefix: ${flagPrefix}"
85
86if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
87__podman-remote_debug "Completion received error. Ignoring completions."
88return
89fi
90
91local activeHelpMarker="_activeHelp_ "
92local endIndex=${#activeHelpMarker}
93local startIndex=$((${#activeHelpMarker}+1))
94local hasActiveHelp=0
95while IFS='\n' read -r comp; do
96# Check if this is an activeHelp statement (i.e., prefixed with $activeHelpMarker)
97if [ "${comp[1,$endIndex]}" = "$activeHelpMarker" ];then
98__podman-remote_debug "ActiveHelp found: $comp"
99comp="${comp[$startIndex,-1]}"
100if [ -n "$comp" ]; then
101compadd -x "${comp}"
102__podman-remote_debug "ActiveHelp will need delimiter"
103hasActiveHelp=1
104fi
105
106continue
107fi
108
109if [ -n "$comp" ]; then
110# If requested, completions are returned with a description.
111# The description is preceded by a TAB character.
112# For zsh's _describe, we need to use a : instead of a TAB.
113# We first need to escape any : as part of the completion itself.
114comp=${comp//:/\\:}
115
116local tab="$(printf '\t')"
117comp=${comp//$tab/:}
118
119__podman-remote_debug "Adding completion: ${comp}"
120completions+=${comp}
121lastComp=$comp
122fi
123done < <(printf "%s\n" "${out[@]}")
124
125# Add a delimiter after the activeHelp statements, but only if:
126# - there are completions following the activeHelp statements, or
127# - file completion will be performed (so there will be choices after the activeHelp)
128if [ $hasActiveHelp -eq 1 ]; then
129if [ ${#completions} -ne 0 ] || [ $((directive & shellCompDirectiveNoFileComp)) -eq 0 ]; then
130__podman-remote_debug "Adding activeHelp delimiter"
131compadd -x "--"
132hasActiveHelp=0
133fi
134fi
135
136if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
137__podman-remote_debug "Activating nospace."
138noSpace="-S ''"
139fi
140
141if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then
142__podman-remote_debug "Activating keep order."
143keepOrder="-V"
144fi
145
146if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
147# File extension filtering
148local filteringCmd
149filteringCmd='_files'
150for filter in ${completions[@]}; do
151if [ ${filter[1]} != '*' ]; then
152# zsh requires a glob pattern to do file filtering
153filter="\*.$filter"
154fi
155filteringCmd+=" -g $filter"
156done
157filteringCmd+=" ${flagPrefix}"
158
159__podman-remote_debug "File filtering command: $filteringCmd"
160_arguments '*:filename:'"$filteringCmd"
161elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
162# File completion for directories only
163local subdir
164subdir="${completions[1]}"
165if [ -n "$subdir" ]; then
166__podman-remote_debug "Listing directories in $subdir"
167pushd "${subdir}" >/dev/null 2>&1
168else
169__podman-remote_debug "Listing directories in ."
170fi
171
172local result
173_arguments '*:dirname:_files -/'" ${flagPrefix}"
174result=$?
175if [ -n "$subdir" ]; then
176popd >/dev/null 2>&1
177fi
178return $result
179else
180__podman-remote_debug "Calling _describe"
181if eval _describe $keepOrder "completions" completions $flagPrefix $noSpace; then
182__podman-remote_debug "_describe found some completions"
183
184# Return the success of having called _describe
185return 0
186else
187__podman-remote_debug "_describe did not find completions."
188__podman-remote_debug "Checking if we should do file completion."
189if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
190__podman-remote_debug "deactivating file completion"
191
192# We must return an error code here to let zsh know that there were no
193# completions found by _describe; this is what will trigger other
194# matching algorithms to attempt to find completions.
195# For example zsh can match letters in the middle of words.
196return 1
197else
198# Perform file completion
199__podman-remote_debug "Activating file completion"
200
201# We must return the result of this command, so it must be the
202# last command, or else we must store its result to return it.
203_arguments '*:filename:_files'" ${flagPrefix}"
204fi
205fi
206fi
207}
208
209# don't run the completion function when being source-ed or eval-ed
210if [ "$funcstack[1]" = "_podman-remote" ]; then
211_podman-remote
212fi
213
214# This file is generated with "podman-remote completion"; see: podman-completion(1)
215