Dotfiles for my tiling window manager + terminal workflow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
6.9 KiB

  1. # Copyright 2016 The Kubernetes Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # fish completion for minikube -*- shell-script -*-
  15. function __minikube_debug
  16. set file "$BASH_COMP_DEBUG_FILE"
  17. if test -n "$file"
  18. echo "$argv" >> $file
  19. end
  20. end
  21. function __minikube_perform_completion
  22. __minikube_debug "Starting __minikube_perform_completion with: $argv"
  23. set args (string split -- " " "$argv")
  24. set lastArg "$args[-1]"
  25. __minikube_debug "args: $args"
  26. __minikube_debug "last arg: $lastArg"
  27. set emptyArg ""
  28. if test -z "$lastArg"
  29. __minikube_debug "Setting emptyArg"
  30. set emptyArg \"\"
  31. end
  32. __minikube_debug "emptyArg: $emptyArg"
  33. if not type -q "$args[1]"
  34. # This can happen when "complete --do-complete minikube" is called when running this script.
  35. __minikube_debug "Cannot find $args[1]. No completions."
  36. return
  37. end
  38. set requestComp "$args[1] __complete $args[2..-1] $emptyArg"
  39. __minikube_debug "Calling $requestComp"
  40. set results (eval $requestComp 2> /dev/null)
  41. set comps $results[1..-2]
  42. set directiveLine $results[-1]
  43. # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
  44. # completions must be prefixed with the flag
  45. set flagPrefix (string match -r -- '-.*=' "$lastArg")
  46. __minikube_debug "Comps: $comps"
  47. __minikube_debug "DirectiveLine: $directiveLine"
  48. __minikube_debug "flagPrefix: $flagPrefix"
  49. for comp in $comps
  50. printf "%s%s\n" "$flagPrefix" "$comp"
  51. end
  52. printf "%s\n" "$directiveLine"
  53. end
  54. # This function does three things:
  55. # 1- Obtain the completions and store them in the global __minikube_comp_results
  56. # 2- Set the __minikube_comp_do_file_comp flag if file completion should be performed
  57. # and unset it otherwise
  58. # 3- Return true if the completion results are not empty
  59. function __minikube_prepare_completions
  60. # Start fresh
  61. set --erase __minikube_comp_do_file_comp
  62. set --erase __minikube_comp_results
  63. # Check if the command-line is already provided. This is useful for testing.
  64. if not set --query __minikube_comp_commandLine
  65. # Use the -c flag to allow for completion in the middle of the line
  66. set __minikube_comp_commandLine (commandline -c)
  67. end
  68. __minikube_debug "commandLine is: $__minikube_comp_commandLine"
  69. set results (__minikube_perform_completion "$__minikube_comp_commandLine")
  70. set --erase __minikube_comp_commandLine
  71. __minikube_debug "Completion results: $results"
  72. if test -z "$results"
  73. __minikube_debug "No completion, probably due to a failure"
  74. # Might as well do file completion, in case it helps
  75. set --global __minikube_comp_do_file_comp 1
  76. return 1
  77. end
  78. set directive (string sub --start 2 $results[-1])
  79. set --global __minikube_comp_results $results[1..-2]
  80. __minikube_debug "Completions are: $__minikube_comp_results"
  81. __minikube_debug "Directive is: $directive"
  82. set shellCompDirectiveError 1
  83. set shellCompDirectiveNoSpace 2
  84. set shellCompDirectiveNoFileComp 4
  85. set shellCompDirectiveFilterFileExt 8
  86. set shellCompDirectiveFilterDirs 16
  87. if test -z "$directive"
  88. set directive 0
  89. end
  90. set compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2)
  91. if test $compErr -eq 1
  92. __minikube_debug "Received error directive: aborting."
  93. # Might as well do file completion, in case it helps
  94. set --global __minikube_comp_do_file_comp 1
  95. return 1
  96. end
  97. set filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2)
  98. set dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2)
  99. if test $filefilter -eq 1; or test $dirfilter -eq 1
  100. __minikube_debug "File extension filtering or directory filtering not supported"
  101. # Do full file completion instead
  102. set --global __minikube_comp_do_file_comp 1
  103. return 1
  104. end
  105. set nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2)
  106. set nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2)
  107. __minikube_debug "nospace: $nospace, nofiles: $nofiles"
  108. # Important not to quote the variable for count to work
  109. set numComps (count $__minikube_comp_results)
  110. __minikube_debug "numComps: $numComps"
  111. if test $numComps -eq 1; and test $nospace -ne 0
  112. # To support the "nospace" directive we trick the shell
  113. # by outputting an extra, longer completion.
  114. __minikube_debug "Adding second completion to perform nospace directive"
  115. set --append __minikube_comp_results $__minikube_comp_results[1].
  116. end
  117. if test $numComps -eq 0; and test $nofiles -eq 0
  118. __minikube_debug "Requesting file completion"
  119. set --global __minikube_comp_do_file_comp 1
  120. end
  121. # If we don't want file completion, we must return true even if there
  122. # are no completions found. This is because fish will perform the last
  123. # completion command, even if its condition is false, if no other
  124. # completion command was triggered
  125. return (not set --query __minikube_comp_do_file_comp)
  126. end
  127. # Since Fish completions are only loaded once the user triggers them, we trigger them ourselves
  128. # so we can properly delete any completions provided by another script.
  129. # The space after the the program name is essential to trigger completion for the program
  130. # and not completion of the program name itself.
  131. complete --do-complete "minikube " > /dev/null 2>&1
  132. # Using '> /dev/null 2>&1' since '&>' is not supported in older versions of fish.
  133. # Remove any pre-existing completions for the program since we will be handling all of them.
  134. complete -c minikube -e
  135. # The order in which the below two lines are defined is very important so that __minikube_prepare_completions
  136. # is called first. It is __minikube_prepare_completions that sets up the __minikube_comp_do_file_comp variable.
  137. #
  138. # This completion will be run second as complete commands are added FILO.
  139. # It triggers file completion choices when __minikube_comp_do_file_comp is set.
  140. complete -c minikube -n 'set --query __minikube_comp_do_file_comp'
  141. # This completion will be run first as complete commands are added FILO.
  142. # The call to __minikube_prepare_completions will setup both __minikube_comp_results and __minikube_comp_do_file_comp.
  143. # It provides the program's completion choices.
  144. complete -c minikube -n '__minikube_prepare_completions' -f -a '$__minikube_comp_results'