podman
340 строк · 11.9 Кб
1# bash completion V2 for podman-remote -*- shell-script -*-
2
3__podman-remote_debug()4{
5if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then6echo "$*" >> "${BASH_COMP_DEBUG_FILE}"7fi8}
9
10# Macs have bash3 for which the bash-completion package doesn't include
11# _init_completion. This is a minimal version of that function.
12__podman-remote_init_completion()13{
14COMPREPLY=()15_get_comp_words_by_ref "$@" cur prev words cword16}
17
18# This function calls the podman-remote program to obtain the completion
19# results and the directive. It fills the 'out' and 'directive' vars.
20__podman-remote_get_completion_results() {21local requestComp lastParam lastChar args22
23# Prepare the command to request completions for the program.24# Calling ${words[0]} instead of directly podman-remote allows handling aliases25args=("${words[@]:1}")26requestComp="${words[0]} __complete ${args[*]}"27
28lastParam=${words[$((${#words[@]}-1))]}29lastChar=${lastParam:$((${#lastParam}-1)):1}30__podman-remote_debug "lastParam ${lastParam}, lastChar ${lastChar}"31
32if [[ -z ${cur} && ${lastChar} != = ]]; then33# If the last parameter is complete (there is a space following it)34# We add an extra empty parameter so we can indicate this to the go method.35__podman-remote_debug "Adding extra empty parameter"36requestComp="${requestComp} ''"37fi38
39# When completing a flag with an = (e.g., podman-remote -n=<TAB>)40# bash focuses on the part after the =, so we need to remove41# the flag part from $cur42if [[ ${cur} == -*=* ]]; then43cur="${cur#*=}"44fi45
46__podman-remote_debug "Calling ${requestComp}"47# Use eval to handle any environment variables and such48out=$(eval "${requestComp}" 2>/dev/null)49
50# Extract the directive integer at the very end of the output following a colon (:)51directive=${out##*:}52# Remove the directive53out=${out%:*}54if [[ ${directive} == "${out}" ]]; then55# There is not directive specified56directive=057fi58__podman-remote_debug "The completion directive is: ${directive}"59__podman-remote_debug "The completions are: ${out}"60}
61
62__podman-remote_process_completion_results() {63local shellCompDirectiveError=164local shellCompDirectiveNoSpace=265local shellCompDirectiveNoFileComp=466local shellCompDirectiveFilterFileExt=867local shellCompDirectiveFilterDirs=1668local shellCompDirectiveKeepOrder=3269
70if (((directive & shellCompDirectiveError) != 0)); then71# Error code. No completion.72__podman-remote_debug "Received error from custom completion go code"73return74else75if (((directive & shellCompDirectiveNoSpace) != 0)); then76if [[ $(type -t compopt) == builtin ]]; then77__podman-remote_debug "Activating no space"78compopt -o nospace79else80__podman-remote_debug "No space directive not supported in this version of bash"81fi82fi83if (((directive & shellCompDirectiveKeepOrder) != 0)); then84if [[ $(type -t compopt) == builtin ]]; then85# no sort isn't supported for bash less than < 4.486if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then87__podman-remote_debug "No sort directive not supported in this version of bash"88else89__podman-remote_debug "Activating keep order"90compopt -o nosort91fi92else93__podman-remote_debug "No sort directive not supported in this version of bash"94fi95fi96if (((directive & shellCompDirectiveNoFileComp) != 0)); then97if [[ $(type -t compopt) == builtin ]]; then98__podman-remote_debug "Activating no file completion"99compopt +o default
100else101__podman-remote_debug "No file completion directive not supported in this version of bash"102fi103fi104fi105
106# Separate activeHelp from normal completions107local completions=()108local activeHelp=()109__podman-remote_extract_activeHelp
110
111if (((directive & shellCompDirectiveFilterFileExt) != 0)); then112# File extension filtering113local fullFilter filter filteringCmd114
115# Do not use quotes around the $completions variable or else newline116# characters will be kept.117for filter in ${completions[*]}; do118fullFilter+="$filter|"119done120
121filteringCmd="_filedir $fullFilter"122__podman-remote_debug "File filtering command: $filteringCmd"123$filteringCmd124elif (((directive & shellCompDirectiveFilterDirs) != 0)); then125# File completion for directories only126
127local subdir128subdir=${completions[0]}129if [[ -n $subdir ]]; then130__podman-remote_debug "Listing directories in $subdir"131pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return132else133__podman-remote_debug "Listing directories in ."134_filedir -d135fi136else137__podman-remote_handle_completion_types
138fi139
140__podman-remote_handle_special_char "$cur" :141__podman-remote_handle_special_char "$cur" =142
143# Print the activeHelp statements before we finish144if ((${#activeHelp[*]} != 0)); then145printf "\n";146printf "%s\n" "${activeHelp[@]}"147printf "\n"148
149# The prompt format is only available from bash 4.4.150# We test if it is available before using it.151if (x=${PS1@P}) 2> /dev/null; then152printf "%s" "${PS1@P}${COMP_LINE[@]}"153else154# Can't print the prompt. Just print the155# text the user had typed, it is workable enough.156printf "%s" "${COMP_LINE[@]}"157fi158fi159}
160
161# Separate activeHelp lines from real completions.
162# Fills the $activeHelp and $completions arrays.
163__podman-remote_extract_activeHelp() {164local activeHelpMarker="_activeHelp_ "165local endIndex=${#activeHelpMarker}166
167while IFS='' read -r comp; do168if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then169comp=${comp:endIndex}170__podman-remote_debug "ActiveHelp found: $comp"171if [[ -n $comp ]]; then172activeHelp+=("$comp")173fi174else175# Not an activeHelp line but a normal completion176completions+=("$comp")177fi178done <<<"${out}"179}
180
181__podman-remote_handle_completion_types() {182__podman-remote_debug "__podman-remote_handle_completion_types: COMP_TYPE is $COMP_TYPE"183
184case $COMP_TYPE in18537|42)186# Type: menu-complete/menu-complete-backward and insert-completions187# If the user requested inserting one completion at a time, or all188# completions at once on the command-line we must remove the descriptions.189# https://github.com/spf13/cobra/issues/1508190local tab=$'\t' comp191while IFS='' read -r comp; do192[[ -z $comp ]] && continue193# Strip any description194comp=${comp%%$tab*}195# Only consider the completions that match196if [[ $comp == "$cur"* ]]; then197COMPREPLY+=("$comp")198fi199done < <(printf "%s\n" "${completions[@]}")200;;201
202*)203# Type: complete (normal completion)204__podman-remote_handle_standard_completion_case
205;;206esac207}
208
209__podman-remote_handle_standard_completion_case() {210local tab=$'\t' comp211
212# Short circuit to optimize if we don't have descriptions213if [[ "${completions[*]}" != *$tab* ]]; then214IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur")215return 0216fi217
218local longest=0219local compline220# Look for the longest completion so that we can format things nicely221while IFS='' read -r compline; do222[[ -z $compline ]] && continue223# Strip any description before checking the length224comp=${compline%%$tab*}225# Only consider the completions that match226[[ $comp == "$cur"* ]] || continue227COMPREPLY+=("$compline")228if ((${#comp}>longest)); then229longest=${#comp}230fi231done < <(printf "%s\n" "${completions[@]}")232
233# If there is a single completion left, remove the description text234if ((${#COMPREPLY[*]} == 1)); then235__podman-remote_debug "COMPREPLY[0]: ${COMPREPLY[0]}"236comp="${COMPREPLY[0]%%$tab*}"237__podman-remote_debug "Removed description from single completion, which is now: ${comp}"238COMPREPLY[0]=$comp239else # Format the descriptions240__podman-remote_format_comp_descriptions $longest241fi242}
243
244__podman-remote_handle_special_char()245{
246local comp="$1"247local char=$2248if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then249local word=${comp%"${comp##*${char}}"}250local idx=${#COMPREPLY[*]}251while ((--idx >= 0)); do252COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}253done
254fi
255}
256
257__podman-remote_format_comp_descriptions()
258{
259local tab=$'\t'260local comp desc maxdesclength
261local longest=$1262
263local i ci
264for ci in ${!COMPREPLY[*]}; do265comp=${COMPREPLY[ci]}266# Properly format the description string which follows a tab character if there is one
267if [[ "$comp" == *$tab* ]]; then268__podman-remote_debug "Original comp: $comp"269desc=${comp#*$tab}270comp=${comp%%$tab*}271
272# $COLUMNS stores the current shell width.273# Remove an extra 4 because we add 2 spaces and 2 parentheses.
274maxdesclength=$(( COLUMNS - longest - 4 ))275
276# Make sure we can fit a description of at least 8 characters
277# if we are to align the descriptions.
278if ((maxdesclength > 8)); then279# Add the proper number of spaces to align the descriptions
280for ((i = ${#comp} ; i < longest ; i++)); do281comp+=" "282done
283else
284# Don't pad the descriptions so we can fit more text after the completion
285maxdesclength=$(( COLUMNS - ${#comp} - 4 ))286fi
287
288# If there is enough space for any description text,
289# truncate the descriptions that are too long for the shell width
290if ((maxdesclength > 0)); then291if ((${#desc} > maxdesclength)); then292desc=${desc:0:$(( maxdesclength - 1 ))}293desc+="…"294fi
295comp+=" ($desc)"296fi
297COMPREPLY[ci]=$comp298__podman-remote_debug "Final comp: $comp"299fi
300done
301}
302
303__start_podman-remote()
304{
305local cur prev words cword split
306
307COMPREPLY=()
308
309# Call _init_completion from the bash-completion package
310# to prepare the arguments properly
311if declare -F _init_completion >/dev/null 2>&1; then
312_init_completion -n =: || return
313else
314__podman-remote_init_completion -n =: || return
315fi
316
317__podman-remote_debug
318__podman-remote_debug "========= starting completion logic =========="319__podman-remote_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"320
321# The user could have moved the cursor backwards on the command-line.322# We need to trigger completion from the $cword location, so we need323# to truncate the command-line ($words) up to the $cword location.324words=("${words[@]:0:$cword+1}")325__podman-remote_debug "Truncated words[*]: ${words[*]},"326
327local out directive328__podman-remote_get_completion_results
329__podman-remote_process_completion_results
330}
331
332if [[ $(type -t compopt) = "builtin" ]]; then333complete -o default -F __start_podman-remote podman-remote334else
335complete -o default -o nospace -F __start_podman-remote podman-remote336fi
337
338# ex: ts=4 sw=4 et filetype=sh
339
340# This file is generated with "podman-remote completion"; see: podman-completion(1)
341