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

     1  # fish completion for podman                               -*- shell-script -*-
     2  
     3  function __podman_debug
     4      set file "$BASH_COMP_DEBUG_FILE"
     5      if test -n "$file"
     6          echo "$argv" >> $file
     7      end
     8  end
     9  
    10  function __podman_perform_completion
    11      __podman_debug "Starting __podman_perform_completion"
    12  
    13      set args (string split -- " " (commandline -c))
    14      set lastArg "$args[-1]"
    15  
    16      __podman_debug "args: $args"
    17      __podman_debug "last arg: $lastArg"
    18  
    19      set emptyArg ""
    20      if test -z "$lastArg"
    21          __podman_debug "Setting emptyArg"
    22          set emptyArg \"\"
    23      end
    24      __podman_debug "emptyArg: $emptyArg"
    25  
    26      if not type -q "$args[1]"
    27          # This can happen when "complete --do-complete podman" is called when running this script.
    28          __podman_debug "Cannot find $args[1]. No completions."
    29          return
    30      end
    31  
    32      set requestComp "$args[1] __complete $args[2..-1] $emptyArg"
    33      __podman_debug "Calling $requestComp"
    34  
    35      set results (eval $requestComp 2> /dev/null)
    36      set comps $results[1..-2]
    37      set directiveLine $results[-1]
    38  
    39      # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
    40      # completions must be prefixed with the flag
    41      set flagPrefix (string match -r -- '-.*=' "$lastArg")
    42  
    43      __podman_debug "Comps: $comps"
    44      __podman_debug "DirectiveLine: $directiveLine"
    45      __podman_debug "flagPrefix: $flagPrefix"
    46  
    47      for comp in $comps
    48          printf "%s%s\n" "$flagPrefix" "$comp"
    49      end
    50  
    51      printf "%s\n" "$directiveLine"
    52  end
    53  
    54  # This function does two things:
    55  # - Obtain the completions and store them in the global __podman_comp_results
    56  # - Return false if file completion should be performed
    57  function __podman_prepare_completions
    58      __podman_debug ""
    59      __podman_debug "========= starting completion logic =========="
    60  
    61      # Start fresh
    62      set --erase __podman_comp_results
    63  
    64      set results (__podman_perform_completion)
    65      __podman_debug "Completion results: $results"
    66  
    67      if test -z "$results"
    68          __podman_debug "No completion, probably due to a failure"
    69          # Might as well do file completion, in case it helps
    70          return 1
    71      end
    72  
    73      set directive (string sub --start 2 $results[-1])
    74      set --global __podman_comp_results $results[1..-2]
    75  
    76      __podman_debug "Completions are: $__podman_comp_results"
    77      __podman_debug "Directive is: $directive"
    78  
    79      set shellCompDirectiveError 1
    80      set shellCompDirectiveNoSpace 2
    81      set shellCompDirectiveNoFileComp 4
    82      set shellCompDirectiveFilterFileExt 8
    83      set shellCompDirectiveFilterDirs 16
    84      set shellCompDirectiveLegacyCustomComp 32
    85      set shellCompDirectiveLegacyCustomArgsComp 64
    86  
    87      if test -z "$directive"
    88          set directive 0
    89      end
    90  
    91      set compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2)
    92      if test $compErr -eq 1
    93          __podman_debug "Received error directive: aborting."
    94          # Might as well do file completion, in case it helps
    95          return 1
    96      end
    97  
    98      set legacyCustom (math (math --scale 0 $directive / $shellCompDirectiveLegacyCustomComp) % 2)
    99      set legacyCustomArgs (math (math --scale 0 $directive / $shellCompDirectiveLegacyCustomArgsComp) % 2)
   100      if test $legacyCustom -eq 1; or test $legacyCustomArgs -eq 1
   101          __podman_debug "Legacy bash custom completion not applicable to fish"
   102          # Do full file completion instead
   103          set --global __podman_comp_do_file_comp 1
   104          return 1
   105      end
   106  
   107      set filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2)
   108      set dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2)
   109      if test $filefilter -eq 1; or test $dirfilter -eq 1
   110          __podman_debug "File extension filtering or directory filtering not supported"
   111          # Do full file completion instead
   112          return 1
   113      end
   114  
   115      set nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2)
   116      set nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2)
   117  
   118      __podman_debug "nospace: $nospace, nofiles: $nofiles"
   119  
   120      # If we want to prevent a space, or if file completion is NOT disabled,
   121      # we need to count the number of valid completions.
   122      # To do so, we will filter on prefix as the completions we have received
   123      # may not already be filtered so as to allow fish to match on different
   124      # criteria than prefix.
   125      if test $nospace -ne 0; or test $nofiles -eq 0
   126          set prefix (commandline -t)
   127          __podman_debug "prefix: $prefix"
   128  
   129          set completions
   130          for comp in $__podman_comp_results
   131              if test (string match -e -r "^$prefix" "$comp")
   132                  set -a completions $comp
   133              end
   134          end
   135          set --global __podman_comp_results $completions
   136          __podman_debug "Filtered completions are: $__podman_comp_results"
   137  
   138          # Important not to quote the variable for count to work
   139          set numComps (count $__podman_comp_results)
   140          __podman_debug "numComps: $numComps"
   141  
   142          if test $numComps -eq 1; and test $nospace -ne 0
   143              # To support the "nospace" directive we trick the shell
   144              # by outputting an extra, longer completion.
   145              # We must first split on \t to get rid of the descriptions because
   146              # the extra character we add to the fake second completion must be
   147              # before the description.  We don't need descriptions anyway since
   148              # there is only a single real completion which the shell will expand
   149              # immediately.
   150              __podman_debug "Adding second completion to perform nospace directive"
   151              set split (string split --max 1 \t $__podman_comp_results[1])
   152              set --global __podman_comp_results $split[1] $split[1].
   153              __podman_debug "Completions are now: $__podman_comp_results"
   154          end
   155  
   156          if test $numComps -eq 0; and test $nofiles -eq 0
   157              # To be consistent with bash and zsh, we only trigger file
   158              # completion when there are no other completions
   159              __podman_debug "Requesting file completion"
   160              return 1
   161          end
   162      end
   163  
   164      return 0
   165  end
   166  
   167  # Since Fish completions are only loaded once the user triggers them, we trigger them ourselves
   168  # so we can properly delete any completions provided by another script.
   169  # The space after the the program name is essential to trigger completion for the program
   170  # and not completion of the program name itself.
   171  complete --do-complete "podman " > /dev/null 2>&1
   172  # Using '> /dev/null 2>&1' since '&>' is not supported in older versions of fish.
   173  
   174  # Remove any pre-existing completions for the program since we will be handling all of them.
   175  complete -c podman -e
   176  
   177  # The call to __podman_prepare_completions will setup __podman_comp_results
   178  # which provides the program's completion choices.
   179  complete -c podman -n '__podman_prepare_completions' -f -a '$__podman_comp_results'
   180  
   181  
   182  # This file is generated with "podman completion"; see: podman-completion(1)