github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/completions/zsh/_podman-remote (about)

     1  #compdef _podman-remote podman-remote
     2  
     3  # zsh completion for podman-remote                        -*- shell-script -*-
     4  
     5  __podman-remote_debug()
     6  {
     7      local file="$BASH_COMP_DEBUG_FILE"
     8      if [[ -n ${file} ]]; then
     9          echo "$*" >> "${file}"
    10      fi
    11  }
    12  
    13  _podman-remote()
    14  {
    15      local shellCompDirectiveError=1
    16      local shellCompDirectiveNoSpace=2
    17      local shellCompDirectiveNoFileComp=4
    18      local shellCompDirectiveFilterFileExt=8
    19      local shellCompDirectiveFilterDirs=16
    20      local shellCompDirectiveLegacyCustomComp=32
    21      local shellCompDirectiveLegacyCustomArgsComp=64
    22  
    23      local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace
    24      local -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.)
    33      words=("${=words[1,CURRENT]}")
    34      __podman-remote_debug "Truncated words[*]: ${words[*]},"
    35  
    36      lastParam=${words[-1]}
    37      lastChar=${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
    42      setopt local_options BASH_REMATCH
    43      if [[ "${lastParam}" =~ '-.*=' ]]; then
    44          # We are dealing with a flag with an =
    45          flagPrefix="-P ${BASH_REMATCH}"
    46      fi
    47  
    48      # Prepare the command to obtain completions
    49      requestComp="${words[1]} __complete ${words[2,-1]}"
    50      if [ "${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"
    54          requestComp="${requestComp} \"\""
    55      fi
    56  
    57      __podman-remote_debug "About to call: eval ${requestComp}"
    58  
    59      # Use eval to handle any environment variables and such
    60      out=$(eval ${requestComp} 2>/dev/null)
    61      __podman-remote_debug "completion output: ${out}"
    62  
    63      # Extract the directive integer following a : from the last line
    64      local lastLine
    65      while IFS='\n' read -r line; do
    66          lastLine=${line}
    67      done < <(printf "%s\n" "${out[@]}")
    68      __podman-remote_debug "last line: ${lastLine}"
    69  
    70      if [ "${lastLine[1]}" = : ]; then
    71          directive=${lastLine[2,-1]}
    72          # Remove the directive including the : and the newline
    73          local suffix
    74          (( suffix=${#lastLine}+2))
    75          out=${out[1,-$suffix]}
    76      else
    77          # There is no directive specified.  Leave $out as is.
    78          __podman-remote_debug "No directive found.  Setting do default"
    79          directive=0
    80      fi
    81  
    82      __podman-remote_debug "directive: ${directive}"
    83      __podman-remote_debug "completions: ${out}"
    84      __podman-remote_debug "flagPrefix: ${flagPrefix}"
    85  
    86      if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
    87          __podman-remote_debug "Completion received error. Ignoring completions."
    88          return
    89      fi
    90  
    91      while IFS='\n' read -r comp; do
    92          if [ -n "$comp" ]; then
    93              # If requested, completions are returned with a description.
    94              # The description is preceded by a TAB character.
    95              # For zsh's _describe, we need to use a : instead of a TAB.
    96              # We first need to escape any : as part of the completion itself.
    97              comp=${comp//:/\\:}
    98  
    99              local tab=$(printf '\t')
   100              comp=${comp//$tab/:}
   101  
   102              __podman-remote_debug "Adding completion: ${comp}"
   103              completions+=${comp}
   104              lastComp=$comp
   105          fi
   106      done < <(printf "%s\n" "${out[@]}")
   107  
   108      if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
   109          __podman-remote_debug "Activating nospace."
   110          noSpace="-S ''"
   111      fi
   112  
   113      if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
   114          # File extension filtering
   115          local filteringCmd
   116          filteringCmd='_files'
   117          for filter in ${completions[@]}; do
   118              if [ ${filter[1]} != '*' ]; then
   119                  # zsh requires a glob pattern to do file filtering
   120                  filter="\*.$filter"
   121              fi
   122              filteringCmd+=" -g $filter"
   123          done
   124          filteringCmd+=" ${flagPrefix}"
   125  
   126          __podman-remote_debug "File filtering command: $filteringCmd"
   127          _arguments '*:filename:'"$filteringCmd"
   128      elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
   129          # File completion for directories only
   130          local subDir
   131          subdir="${completions[1]}"
   132          if [ -n "$subdir" ]; then
   133              __podman-remote_debug "Listing directories in $subdir"
   134              pushd "${subdir}" >/dev/null 2>&1
   135          else
   136              __podman-remote_debug "Listing directories in ."
   137          fi
   138  
   139          local result
   140          _arguments '*:dirname:_files -/'" ${flagPrefix}"
   141          result=$?
   142          if [ -n "$subdir" ]; then
   143              popd >/dev/null 2>&1
   144          fi
   145          return $result
   146      else
   147          __podman-remote_debug "Calling _describe"
   148          if eval _describe "completions" completions $flagPrefix $noSpace; then
   149              __podman-remote_debug "_describe found some completions"
   150  
   151              # Return the success of having called _describe
   152              return 0
   153          else
   154              __podman-remote_debug "_describe did not find completions."
   155              __podman-remote_debug "Checking if we should do file completion."
   156              if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
   157                  __podman-remote_debug "deactivating file completion"
   158  
   159                  # We must return an error code here to let zsh know that there were no
   160                  # completions found by _describe; this is what will trigger other
   161                  # matching algorithms to attempt to find completions.
   162                  # For example zsh can match letters in the middle of words.
   163                  return 1
   164              else
   165                  # Perform file completion
   166                  __podman-remote_debug "Activating file completion"
   167  
   168                  # We must return the result of this command, so it must be the
   169                  # last command, or else we must store its result to return it.
   170                  _arguments '*:filename:_files'" ${flagPrefix}"
   171              fi
   172          fi
   173      fi
   174  }
   175  
   176  # don't run the completion function when being source-ed or eval-ed
   177  if [ "$funcstack[1]" = "_podman-remote" ]; then
   178  	_podman-remote
   179  fi
   180  
   181  # This file is generated with "podman-remote completion"; see: podman-completion(1)