github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/completions/bash/podman-remote (about) 1 # bash completion for podman-remote -*- shell-script -*- 2 3 __podman-remote_debug() 4 { 5 if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then 6 echo "$*" >> "${BASH_COMP_DEBUG_FILE}" 7 fi 8 } 9 10 __podman-remote_perform_completion() 11 { 12 __podman-remote_debug 13 __podman-remote_debug "========= starting completion logic ==========" 14 __podman-remote_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword" 15 16 # The user could have moved the cursor backwards on the command-line. 17 # We need to trigger completion from the $cword location, so we need 18 # to truncate the command-line ($words) up to the $cword location. 19 words=("${words[@]:0:$cword+1}") 20 __podman-remote_debug "Truncated words[*]: ${words[*]}," 21 22 local shellCompDirectiveError=1 23 local shellCompDirectiveNoSpace=2 24 local shellCompDirectiveNoFileComp=4 25 local shellCompDirectiveFilterFileExt=8 26 local shellCompDirectiveFilterDirs=16 27 local shellCompDirectiveLegacyCustomComp=32 28 local shellCompDirectiveLegacyCustomArgsComp=64 29 30 local out requestComp lastParam lastChar comp directive args flagPrefix 31 32 # Prepare the command to request completions for the program. 33 # Calling ${words[0]} instead of directly podman-remote allows to handle aliases 34 args=("${words[@]:1}") 35 requestComp="${words[0]} __completeNoDesc ${args[*]}" 36 37 lastParam=${words[$((${#words[@]}-1))]} 38 lastChar=${lastParam:$((${#lastParam}-1)):1} 39 __podman-remote_debug "lastParam ${lastParam}, lastChar ${lastChar}" 40 41 if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then 42 # If the last parameter is complete (there is a space following it) 43 # We add an extra empty parameter so we can indicate this to the go method. 44 __podman-remote_debug "Adding extra empty parameter" 45 requestComp="${requestComp} \"\"" 46 fi 47 48 # When completing a flag with an = (e.g., podman-remote -n=<TAB>) 49 # bash focuses on the part after the =, so we need to remove 50 # the flag part from $cur 51 if [[ "${cur}" == -*=* ]]; then 52 flagPrefix="${cur%%=*}=" 53 cur="${cur#*=}" 54 fi 55 56 __podman-remote_debug "Calling ${requestComp}" 57 # Use eval to handle any environment variables and such 58 out=$(eval "${requestComp}" 2>/dev/null) 59 60 # Extract the directive integer at the very end of the output following a colon (:) 61 directive=${out##*:} 62 # Remove the directive 63 out=${out%:*} 64 if [ "${directive}" = "${out}" ]; then 65 # There is not directive specified 66 directive=0 67 fi 68 __podman-remote_debug "The completion directive is: ${directive}" 69 __podman-remote_debug "The completions are: ${out[*]}" 70 71 if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then 72 # Error code. No completion. 73 __podman-remote_debug "Received error from custom completion go code" 74 return 75 else 76 if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then 77 if [[ $(type -t compopt) = "builtin" ]]; then 78 __podman-remote_debug "Activating no space" 79 compopt -o nospace 80 fi 81 fi 82 if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then 83 if [[ $(type -t compopt) = "builtin" ]]; then 84 __podman-remote_debug "Activating no file completion" 85 compopt +o default 86 fi 87 fi 88 fi 89 90 if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then 91 # File extension filtering 92 local fullFilter filter filteringCmd 93 94 # Do not use quotes around the $out variable or else newline 95 # characters will be kept. 96 for filter in ${out[*]}; do 97 fullFilter+="$filter|" 98 done 99 100 filteringCmd="_filedir $fullFilter" 101 __podman-remote_debug "File filtering command: $filteringCmd" 102 $filteringCmd 103 elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then 104 # File completion for directories only 105 106 # Use printf to strip any trailing newline 107 local subdir 108 subdir=$(printf "%s" "${out[0]}") 109 if [ -n "$subdir" ]; then 110 __podman-remote_debug "Listing directories in $subdir" 111 pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return 112 else 113 __podman-remote_debug "Listing directories in ." 114 _filedir -d 115 fi 116 elif [ $((directive & shellCompDirectiveLegacyCustomComp)) -ne 0 ]; then 117 local cmd 118 __podman-remote_debug "Legacy custom completion. Directive: $directive, cmds: ${out[*]}" 119 120 # The following variables should get their value through the commands 121 # we have received as completions and are parsing below. 122 local last_command 123 local nouns 124 125 # Execute every command received 126 while IFS='' read -r cmd; do 127 __podman-remote_debug "About to execute: $cmd" 128 eval "$cmd" 129 done < <(printf "%s\n" "${out[@]}") 130 131 __podman-remote_debug "last_command: $last_command" 132 __podman-remote_debug "nouns[0]: ${nouns[0]}, nouns[1]: ${nouns[1]}" 133 134 if [ $((directive & shellCompDirectiveLegacyCustomArgsComp)) -ne 0 ]; then 135 # We should call the global legacy custom completion function, if it is defined 136 if declare -F __podman-remote_custom_func >/dev/null; then 137 # Use command name qualified legacy custom func 138 __podman-remote_debug "About to call: __podman-remote_custom_func" 139 __podman-remote_custom_func 140 elif declare -F __custom_func >/dev/null; then 141 # Otherwise fall back to unqualified legacy custom func for compatibility 142 __podman-remote_debug "About to call: __custom_func" 143 __custom_func 144 fi 145 fi 146 else 147 local tab 148 tab=$(printf '\t') 149 local longest=0 150 # Look for the longest completion so that we can format things nicely 151 while IFS='' read -r comp; do 152 comp=${comp%%$tab*} 153 if ((${#comp}>longest)); then 154 longest=${#comp} 155 fi 156 done < <(printf "%s\n" "${out[@]}") 157 158 local completions=() 159 while IFS='' read -r comp; do 160 if [ -z "$comp" ]; then 161 continue 162 fi 163 164 __podman-remote_debug "Original comp: $comp" 165 comp="$(__podman-remote_format_comp_descriptions "$comp" "$longest")" 166 __podman-remote_debug "Final comp: $comp" 167 completions+=("$comp") 168 done < <(printf "%s\n" "${out[@]}") 169 170 while IFS='' read -r comp; do 171 # Although this script should only be used for bash 172 # there may be programs that still convert the bash 173 # script into a zsh one. To continue supporting those 174 # programs, we do this single adaptation for zsh 175 if [ -n "${ZSH_VERSION}" ]; then 176 # zsh completion needs --flag= prefix 177 COMPREPLY+=("$flagPrefix$comp") 178 else 179 COMPREPLY+=("$comp") 180 fi 181 done < <(compgen -W "${completions[*]}" -- "$cur") 182 183 # If there is a single completion left, remove the description text 184 if [ ${#COMPREPLY[*]} -eq 1 ]; then 185 __podman-remote_debug "COMPREPLY[0]: ${COMPREPLY[0]}" 186 comp="${COMPREPLY[0]%% *}" 187 __podman-remote_debug "Removed description from single completion, which is now: ${comp}" 188 COMPREPLY=() 189 COMPREPLY+=("$comp") 190 fi 191 fi 192 193 __podman-remote_handle_special_char "$cur" : 194 __podman-remote_handle_special_char "$cur" = 195 } 196 197 __podman-remote_handle_special_char() 198 { 199 local comp="$1" 200 local char=$2 201 if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then 202 local word=${comp%"${comp##*${char}}"} 203 local idx=${#COMPREPLY[*]} 204 while [[ $((--idx)) -ge 0 ]]; do 205 COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"} 206 done 207 fi 208 } 209 210 __podman-remote_format_comp_descriptions() 211 { 212 local tab 213 tab=$(printf '\t') 214 local comp="$1" 215 local longest=$2 216 217 # Properly format the description string which follows a tab character if there is one 218 if [[ "$comp" == *$tab* ]]; then 219 desc=${comp#*$tab} 220 comp=${comp%%$tab*} 221 222 # $COLUMNS stores the current shell width. 223 # Remove an extra 4 because we add 2 spaces and 2 parentheses. 224 maxdesclength=$(( COLUMNS - longest - 4 )) 225 226 # Make sure we can fit a description of at least 8 characters 227 # if we are to align the descriptions. 228 if [[ $maxdesclength -gt 8 ]]; then 229 # Add the proper number of spaces to align the descriptions 230 for ((i = ${#comp} ; i < longest ; i++)); do 231 comp+=" " 232 done 233 else 234 # Don't pad the descriptions so we can fit more text after the completion 235 maxdesclength=$(( COLUMNS - ${#comp} - 4 )) 236 fi 237 238 # If there is enough space for any description text, 239 # truncate the descriptions that are too long for the shell width 240 if [ $maxdesclength -gt 0 ]; then 241 if [ ${#desc} -gt $maxdesclength ]; then 242 desc=${desc:0:$(( maxdesclength - 1 ))} 243 desc+="…" 244 fi 245 comp+=" ($desc)" 246 fi 247 fi 248 249 # Must use printf to escape all special characters 250 printf "%q" "${comp}" 251 } 252 253 __start_podman-remote() 254 { 255 local cur prev words cword 256 257 COMPREPLY=() 258 _get_comp_words_by_ref -n "=:" cur prev words cword 259 260 __podman-remote_perform_completion 261 } 262 263 if [[ $(type -t compopt) = "builtin" ]]; then 264 complete -o default -F __start_podman-remote podman-remote 265 else 266 complete -o default -o nospace -F __start_podman-remote podman-remote 267 fi 268 269 # ex: ts=4 sw=4 et filetype=sh 270 271 # This file is generated with "podman-remote completion"; see: podman-completion(1)