github.com/AbhinandanKurakure/podman/v3@v3.4.10/completions/bash/podman-remote (about)

     1  # bash completion V2 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  # 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  {
    14      COMPREPLY=()
    15      _get_comp_words_by_ref "$@" cur prev words cword
    16  }
    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() {
    21      local requestComp lastParam lastChar args
    22  
    23      # Prepare the command to request completions for the program.
    24      # Calling ${words[0]} instead of directly podman-remote allows to handle aliases
    25      args=("${words[@]:1}")
    26      requestComp="${words[0]} __complete ${args[*]}"
    27  
    28      lastParam=${words[$((${#words[@]}-1))]}
    29      lastChar=${lastParam:$((${#lastParam}-1)):1}
    30      __podman-remote_debug "lastParam ${lastParam}, lastChar ${lastChar}"
    31  
    32      if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then
    33          # 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"
    36          requestComp="${requestComp} ''"
    37      fi
    38  
    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 remove
    41      # the flag part from $cur
    42      if [[ "${cur}" == -*=* ]]; then
    43          cur="${cur#*=}"
    44      fi
    45  
    46      __podman-remote_debug "Calling ${requestComp}"
    47      # Use eval to handle any environment variables and such
    48      out=$(eval "${requestComp}" 2>/dev/null)
    49  
    50      # Extract the directive integer at the very end of the output following a colon (:)
    51      directive=${out##*:}
    52      # Remove the directive
    53      out=${out%:*}
    54      if [ "${directive}" = "${out}" ]; then
    55          # There is not directive specified
    56          directive=0
    57      fi
    58      __podman-remote_debug "The completion directive is: ${directive}"
    59      __podman-remote_debug "The completions are: ${out[*]}"
    60  }
    61  
    62  __podman-remote_process_completion_results() {
    63      local shellCompDirectiveError=1
    64      local shellCompDirectiveNoSpace=2
    65      local shellCompDirectiveNoFileComp=4
    66      local shellCompDirectiveFilterFileExt=8
    67      local shellCompDirectiveFilterDirs=16
    68  
    69      if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
    70          # Error code.  No completion.
    71          __podman-remote_debug "Received error from custom completion go code"
    72          return
    73      else
    74          if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
    75              if [[ $(type -t compopt) = "builtin" ]]; then
    76                  __podman-remote_debug "Activating no space"
    77                  compopt -o nospace
    78              else
    79                  __podman-remote_debug "No space directive not supported in this version of bash"
    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              else
    87                  __podman-remote_debug "No file completion directive not supported in this version of bash"
    88              fi
    89          fi
    90      fi
    91  
    92      if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
    93          # File extension filtering
    94          local fullFilter filter filteringCmd
    95  
    96          # Do not use quotes around the $out variable or else newline
    97          # characters will be kept.
    98          for filter in ${out[*]}; do
    99              fullFilter+="$filter|"
   100          done
   101  
   102          filteringCmd="_filedir $fullFilter"
   103          __podman-remote_debug "File filtering command: $filteringCmd"
   104          $filteringCmd
   105      elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
   106          # File completion for directories only
   107  
   108          # Use printf to strip any trailing newline
   109          local subdir
   110          subdir=$(printf "%s" "${out[0]}")
   111          if [ -n "$subdir" ]; then
   112              __podman-remote_debug "Listing directories in $subdir"
   113              pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
   114          else
   115              __podman-remote_debug "Listing directories in ."
   116              _filedir -d
   117          fi
   118      else
   119          __podman-remote_handle_standard_completion_case
   120      fi
   121  
   122      __podman-remote_handle_special_char "$cur" :
   123      __podman-remote_handle_special_char "$cur" =
   124  }
   125  
   126  __podman-remote_handle_standard_completion_case() {
   127      local tab comp
   128      tab=$(printf '\t')
   129  
   130      local longest=0
   131      # Look for the longest completion so that we can format things nicely
   132      while IFS='' read -r comp; do
   133          # Strip any description before checking the length
   134          comp=${comp%%$tab*}
   135          # Only consider the completions that match
   136          comp=$(compgen -W "$comp" -- "$cur")
   137          if ((${#comp}>longest)); then
   138              longest=${#comp}
   139          fi
   140      done < <(printf "%s\n" "${out[@]}")
   141  
   142      local completions=()
   143      while IFS='' read -r comp; do
   144          if [ -z "$comp" ]; then
   145              continue
   146          fi
   147  
   148          __podman-remote_debug "Original comp: $comp"
   149          comp="$(__podman-remote_format_comp_descriptions "$comp" "$longest")"
   150          __podman-remote_debug "Final comp: $comp"
   151          completions+=("$comp")
   152      done < <(printf "%s\n" "${out[@]}")
   153  
   154      while IFS='' read -r comp; do
   155          COMPREPLY+=("$comp")
   156      done < <(compgen -W "${completions[*]}" -- "$cur")
   157  
   158      # If there is a single completion left, remove the description text
   159      if [ ${#COMPREPLY[*]} -eq 1 ]; then
   160          __podman-remote_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
   161          comp="${COMPREPLY[0]%% *}"
   162          __podman-remote_debug "Removed description from single completion, which is now: ${comp}"
   163          COMPREPLY=()
   164          COMPREPLY+=("$comp")
   165      fi
   166  }
   167  
   168  __podman-remote_handle_special_char()
   169  {
   170      local comp="$1"
   171      local char=$2
   172      if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
   173          local word=${comp%"${comp##*${char}}"}
   174          local idx=${#COMPREPLY[*]}
   175          while [[ $((--idx)) -ge 0 ]]; do
   176              COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"}
   177          done
   178      fi
   179  }
   180  
   181  __podman-remote_format_comp_descriptions()
   182  {
   183      local tab
   184      tab=$(printf '\t')
   185      local comp="$1"
   186      local longest=$2
   187  
   188      # Properly format the description string which follows a tab character if there is one
   189      if [[ "$comp" == *$tab* ]]; then
   190          desc=${comp#*$tab}
   191          comp=${comp%%$tab*}
   192  
   193          # $COLUMNS stores the current shell width.
   194          # Remove an extra 4 because we add 2 spaces and 2 parentheses.
   195          maxdesclength=$(( COLUMNS - longest - 4 ))
   196  
   197          # Make sure we can fit a description of at least 8 characters
   198          # if we are to align the descriptions.
   199          if [[ $maxdesclength -gt 8 ]]; then
   200              # Add the proper number of spaces to align the descriptions
   201              for ((i = ${#comp} ; i < longest ; i++)); do
   202                  comp+=" "
   203              done
   204          else
   205              # Don't pad the descriptions so we can fit more text after the completion
   206              maxdesclength=$(( COLUMNS - ${#comp} - 4 ))
   207          fi
   208  
   209          # If there is enough space for any description text,
   210          # truncate the descriptions that are too long for the shell width
   211          if [ $maxdesclength -gt 0 ]; then
   212              if [ ${#desc} -gt $maxdesclength ]; then
   213                  desc=${desc:0:$(( maxdesclength - 1 ))}
   214                  desc+="…"
   215              fi
   216              comp+="  ($desc)"
   217          fi
   218      fi
   219  
   220      # Must use printf to escape all special characters
   221      printf "%q" "${comp}"
   222  }
   223  
   224  __start_podman-remote()
   225  {
   226      local cur prev words cword split
   227  
   228      COMPREPLY=()
   229  
   230      # Call _init_completion from the bash-completion package
   231      # to prepare the arguments properly
   232      if declare -F _init_completion >/dev/null 2>&1; then
   233          _init_completion -n "=:" || return
   234      else
   235          __podman-remote_init_completion -n "=:" || return
   236      fi
   237  
   238      __podman-remote_debug
   239      __podman-remote_debug "========= starting completion logic =========="
   240      __podman-remote_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"
   241  
   242      # The user could have moved the cursor backwards on the command-line.
   243      # We need to trigger completion from the $cword location, so we need
   244      # to truncate the command-line ($words) up to the $cword location.
   245      words=("${words[@]:0:$cword+1}")
   246      __podman-remote_debug "Truncated words[*]: ${words[*]},"
   247  
   248      local out directive
   249      __podman-remote_get_completion_results
   250      __podman-remote_process_completion_results
   251  }
   252  
   253  if [[ $(type -t compopt) = "builtin" ]]; then
   254      complete -o default -F __start_podman-remote podman-remote
   255  else
   256      complete -o default -o nospace -F __start_podman-remote podman-remote
   257  fi
   258  
   259  # ex: ts=4 sw=4 et filetype=sh
   260  
   261  # This file is generated with "podman-remote completion"; see: podman-completion(1)