diff --git a/fish/dot-config/fish/completions/argocd.fish b/fish/dot-config/fish/completions/argocd.fish new file mode 100644 index 0000000..4527d05 --- /dev/null +++ b/fish/dot-config/fish/completions/argocd.fish @@ -0,0 +1,235 @@ +# fish completion for argocd -*- shell-script -*- + +function __argocd_debug + set -l file "$BASH_COMP_DEBUG_FILE" + if test -n "$file" + echo "$argv" >> $file + end +end + +function __argocd_perform_completion + __argocd_debug "Starting __argocd_perform_completion" + + # Extract all args except the last one + set -l args (commandline -opc) + # Extract the last arg and escape it in case it is a space + set -l lastArg (string escape -- (commandline -ct)) + + __argocd_debug "args: $args" + __argocd_debug "last arg: $lastArg" + + # Disable ActiveHelp which is not supported for fish shell + set -l requestComp "ARGOCD_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg" + + __argocd_debug "Calling $requestComp" + set -l results (eval $requestComp 2> /dev/null) + + # Some programs may output extra empty lines after the directive. + # Let's ignore them or else it will break completion. + # Ref: https://github.com/spf13/cobra/issues/1279 + for line in $results[-1..1] + if test (string trim -- $line) = "" + # Found an empty line, remove it + set results $results[1..-2] + else + # Found non-empty line, we have our proper output + break + end + end + + set -l comps $results[1..-2] + set -l directiveLine $results[-1] + + # For Fish, when completing a flag with an = (e.g., -n=) + # completions must be prefixed with the flag + set -l flagPrefix (string match -r -- '-.*=' "$lastArg") + + __argocd_debug "Comps: $comps" + __argocd_debug "DirectiveLine: $directiveLine" + __argocd_debug "flagPrefix: $flagPrefix" + + for comp in $comps + printf "%s%s\n" "$flagPrefix" "$comp" + end + + printf "%s\n" "$directiveLine" +end + +# this function limits calls to __argocd_perform_completion, by caching the result behind $__argocd_perform_completion_once_result +function __argocd_perform_completion_once + __argocd_debug "Starting __argocd_perform_completion_once" + + if test -n "$__argocd_perform_completion_once_result" + __argocd_debug "Seems like a valid result already exists, skipping __argocd_perform_completion" + return 0 + end + + set --global __argocd_perform_completion_once_result (__argocd_perform_completion) + if test -z "$__argocd_perform_completion_once_result" + __argocd_debug "No completions, probably due to a failure" + return 1 + end + + __argocd_debug "Performed completions and set __argocd_perform_completion_once_result" + return 0 +end + +# this function is used to clear the $__argocd_perform_completion_once_result variable after completions are run +function __argocd_clear_perform_completion_once_result + __argocd_debug "" + __argocd_debug "========= clearing previously set __argocd_perform_completion_once_result variable ==========" + set --erase __argocd_perform_completion_once_result + __argocd_debug "Succesfully erased the variable __argocd_perform_completion_once_result" +end + +function __argocd_requires_order_preservation + __argocd_debug "" + __argocd_debug "========= checking if order preservation is required ==========" + + __argocd_perform_completion_once + if test -z "$__argocd_perform_completion_once_result" + __argocd_debug "Error determining if order preservation is required" + return 1 + end + + set -l directive (string sub --start 2 $__argocd_perform_completion_once_result[-1]) + __argocd_debug "Directive is: $directive" + + set -l shellCompDirectiveKeepOrder 32 + set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2) + __argocd_debug "Keeporder is: $keeporder" + + if test $keeporder -ne 0 + __argocd_debug "This does require order preservation" + return 0 + end + + __argocd_debug "This doesn't require order preservation" + return 1 +end + + +# This function does two things: +# - Obtain the completions and store them in the global __argocd_comp_results +# - Return false if file completion should be performed +function __argocd_prepare_completions + __argocd_debug "" + __argocd_debug "========= starting completion logic ==========" + + # Start fresh + set --erase __argocd_comp_results + + __argocd_perform_completion_once + __argocd_debug "Completion results: $__argocd_perform_completion_once_result" + + if test -z "$__argocd_perform_completion_once_result" + __argocd_debug "No completion, probably due to a failure" + # Might as well do file completion, in case it helps + return 1 + end + + set -l directive (string sub --start 2 $__argocd_perform_completion_once_result[-1]) + set --global __argocd_comp_results $__argocd_perform_completion_once_result[1..-2] + + __argocd_debug "Completions are: $__argocd_comp_results" + __argocd_debug "Directive is: $directive" + + set -l shellCompDirectiveError 1 + set -l shellCompDirectiveNoSpace 2 + set -l shellCompDirectiveNoFileComp 4 + set -l shellCompDirectiveFilterFileExt 8 + set -l shellCompDirectiveFilterDirs 16 + + if test -z "$directive" + set directive 0 + end + + set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2) + if test $compErr -eq 1 + __argocd_debug "Received error directive: aborting." + # Might as well do file completion, in case it helps + return 1 + end + + set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2) + set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2) + if test $filefilter -eq 1; or test $dirfilter -eq 1 + __argocd_debug "File extension filtering or directory filtering not supported" + # Do full file completion instead + return 1 + end + + set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2) + set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2) + + __argocd_debug "nospace: $nospace, nofiles: $nofiles" + + # If we want to prevent a space, or if file completion is NOT disabled, + # we need to count the number of valid completions. + # To do so, we will filter on prefix as the completions we have received + # may not already be filtered so as to allow fish to match on different + # criteria than the prefix. + if test $nospace -ne 0; or test $nofiles -eq 0 + set -l prefix (commandline -t | string escape --style=regex) + __argocd_debug "prefix: $prefix" + + set -l completions (string match -r -- "^$prefix.*" $__argocd_comp_results) + set --global __argocd_comp_results $completions + __argocd_debug "Filtered completions are: $__argocd_comp_results" + + # Important not to quote the variable for count to work + set -l numComps (count $__argocd_comp_results) + __argocd_debug "numComps: $numComps" + + if test $numComps -eq 1; and test $nospace -ne 0 + # We must first split on \t to get rid of the descriptions to be + # able to check what the actual completion will be. + # We don't need descriptions anyway since there is only a single + # real completion which the shell will expand immediately. + set -l split (string split --max 1 \t $__argocd_comp_results[1]) + + # Fish won't add a space if the completion ends with any + # of the following characters: @=/:., + set -l lastChar (string sub -s -1 -- $split) + if not string match -r -q "[@=/:.,]" -- "$lastChar" + # In other cases, to support the "nospace" directive we trick the shell + # by outputting an extra, longer completion. + __argocd_debug "Adding second completion to perform nospace directive" + set --global __argocd_comp_results $split[1] $split[1]. + __argocd_debug "Completions are now: $__argocd_comp_results" + end + end + + if test $numComps -eq 0; and test $nofiles -eq 0 + # To be consistent with bash and zsh, we only trigger file + # completion when there are no other completions + __argocd_debug "Requesting file completion" + return 1 + end + end + + return 0 +end + +# Since Fish completions are only loaded once the user triggers them, we trigger them ourselves +# so we can properly delete any completions provided by another script. +# Only do this if the program can be found, or else fish may print some errors; besides, +# the existing completions will only be loaded if the program can be found. +if type -q "argocd" + # The space after the program name is essential to trigger completion for the program + # and not completion of the program name itself. + # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish. + complete --do-complete "argocd " > /dev/null 2>&1 +end + +# Remove any pre-existing completions for the program since we will be handling all of them. +complete -c argocd -e + +# this will get called after the two calls below and clear the $__argocd_perform_completion_once_result global +complete -c argocd -n '__argocd_clear_perform_completion_once_result' +# The call to __argocd_prepare_completions will setup __argocd_comp_results +# which provides the program's completion choices. +# If this doesn't require order preservation, we don't use the -k flag +complete -c argocd -n 'not __argocd_requires_order_preservation && __argocd_prepare_completions' -f -a '$__argocd_comp_results' +# otherwise we use the -k flag +complete -k -c argocd -n '__argocd_requires_order_preservation && __argocd_prepare_completions' -f -a '$__argocd_comp_results' diff --git a/fish/dot-config/fish/completions/helm.fish b/fish/dot-config/fish/completions/helm.fish new file mode 100644 index 0000000..69aed4f --- /dev/null +++ b/fish/dot-config/fish/completions/helm.fish @@ -0,0 +1,235 @@ +# fish completion for helm -*- shell-script -*- + +function __helm_debug + set -l file "$BASH_COMP_DEBUG_FILE" + if test -n "$file" + echo "$argv" >> $file + end +end + +function __helm_perform_completion + __helm_debug "Starting __helm_perform_completion" + + # Extract all args except the last one + set -l args (commandline -opc) + # Extract the last arg and escape it in case it is a space + set -l lastArg (string escape -- (commandline -ct)) + + __helm_debug "args: $args" + __helm_debug "last arg: $lastArg" + + # Disable ActiveHelp which is not supported for fish shell + set -l requestComp "HELM_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg" + + __helm_debug "Calling $requestComp" + set -l results (eval $requestComp 2> /dev/null) + + # Some programs may output extra empty lines after the directive. + # Let's ignore them or else it will break completion. + # Ref: https://github.com/spf13/cobra/issues/1279 + for line in $results[-1..1] + if test (string trim -- $line) = "" + # Found an empty line, remove it + set results $results[1..-2] + else + # Found non-empty line, we have our proper output + break + end + end + + set -l comps $results[1..-2] + set -l directiveLine $results[-1] + + # For Fish, when completing a flag with an = (e.g., -n=) + # completions must be prefixed with the flag + set -l flagPrefix (string match -r -- '-.*=' "$lastArg") + + __helm_debug "Comps: $comps" + __helm_debug "DirectiveLine: $directiveLine" + __helm_debug "flagPrefix: $flagPrefix" + + for comp in $comps + printf "%s%s\n" "$flagPrefix" "$comp" + end + + printf "%s\n" "$directiveLine" +end + +# this function limits calls to __helm_perform_completion, by caching the result behind $__helm_perform_completion_once_result +function __helm_perform_completion_once + __helm_debug "Starting __helm_perform_completion_once" + + if test -n "$__helm_perform_completion_once_result" + __helm_debug "Seems like a valid result already exists, skipping __helm_perform_completion" + return 0 + end + + set --global __helm_perform_completion_once_result (__helm_perform_completion) + if test -z "$__helm_perform_completion_once_result" + __helm_debug "No completions, probably due to a failure" + return 1 + end + + __helm_debug "Performed completions and set __helm_perform_completion_once_result" + return 0 +end + +# this function is used to clear the $__helm_perform_completion_once_result variable after completions are run +function __helm_clear_perform_completion_once_result + __helm_debug "" + __helm_debug "========= clearing previously set __helm_perform_completion_once_result variable ==========" + set --erase __helm_perform_completion_once_result + __helm_debug "Successfully erased the variable __helm_perform_completion_once_result" +end + +function __helm_requires_order_preservation + __helm_debug "" + __helm_debug "========= checking if order preservation is required ==========" + + __helm_perform_completion_once + if test -z "$__helm_perform_completion_once_result" + __helm_debug "Error determining if order preservation is required" + return 1 + end + + set -l directive (string sub --start 2 $__helm_perform_completion_once_result[-1]) + __helm_debug "Directive is: $directive" + + set -l shellCompDirectiveKeepOrder 32 + set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2) + __helm_debug "Keeporder is: $keeporder" + + if test $keeporder -ne 0 + __helm_debug "This does require order preservation" + return 0 + end + + __helm_debug "This doesn't require order preservation" + return 1 +end + + +# This function does two things: +# - Obtain the completions and store them in the global __helm_comp_results +# - Return false if file completion should be performed +function __helm_prepare_completions + __helm_debug "" + __helm_debug "========= starting completion logic ==========" + + # Start fresh + set --erase __helm_comp_results + + __helm_perform_completion_once + __helm_debug "Completion results: $__helm_perform_completion_once_result" + + if test -z "$__helm_perform_completion_once_result" + __helm_debug "No completion, probably due to a failure" + # Might as well do file completion, in case it helps + return 1 + end + + set -l directive (string sub --start 2 $__helm_perform_completion_once_result[-1]) + set --global __helm_comp_results $__helm_perform_completion_once_result[1..-2] + + __helm_debug "Completions are: $__helm_comp_results" + __helm_debug "Directive is: $directive" + + set -l shellCompDirectiveError 1 + set -l shellCompDirectiveNoSpace 2 + set -l shellCompDirectiveNoFileComp 4 + set -l shellCompDirectiveFilterFileExt 8 + set -l shellCompDirectiveFilterDirs 16 + + if test -z "$directive" + set directive 0 + end + + set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2) + if test $compErr -eq 1 + __helm_debug "Received error directive: aborting." + # Might as well do file completion, in case it helps + return 1 + end + + set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2) + set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2) + if test $filefilter -eq 1; or test $dirfilter -eq 1 + __helm_debug "File extension filtering or directory filtering not supported" + # Do full file completion instead + return 1 + end + + set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2) + set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2) + + __helm_debug "nospace: $nospace, nofiles: $nofiles" + + # If we want to prevent a space, or if file completion is NOT disabled, + # we need to count the number of valid completions. + # To do so, we will filter on prefix as the completions we have received + # may not already be filtered so as to allow fish to match on different + # criteria than the prefix. + if test $nospace -ne 0; or test $nofiles -eq 0 + set -l prefix (commandline -t | string escape --style=regex) + __helm_debug "prefix: $prefix" + + set -l completions (string match -r -- "^$prefix.*" $__helm_comp_results) + set --global __helm_comp_results $completions + __helm_debug "Filtered completions are: $__helm_comp_results" + + # Important not to quote the variable for count to work + set -l numComps (count $__helm_comp_results) + __helm_debug "numComps: $numComps" + + if test $numComps -eq 1; and test $nospace -ne 0 + # We must first split on \t to get rid of the descriptions to be + # able to check what the actual completion will be. + # We don't need descriptions anyway since there is only a single + # real completion which the shell will expand immediately. + set -l split (string split --max 1 \t $__helm_comp_results[1]) + + # Fish won't add a space if the completion ends with any + # of the following characters: @=/:., + set -l lastChar (string sub -s -1 -- $split) + if not string match -r -q "[@=/:.,]" -- "$lastChar" + # In other cases, to support the "nospace" directive we trick the shell + # by outputting an extra, longer completion. + __helm_debug "Adding second completion to perform nospace directive" + set --global __helm_comp_results $split[1] $split[1]. + __helm_debug "Completions are now: $__helm_comp_results" + end + end + + if test $numComps -eq 0; and test $nofiles -eq 0 + # To be consistent with bash and zsh, we only trigger file + # completion when there are no other completions + __helm_debug "Requesting file completion" + return 1 + end + end + + return 0 +end + +# Since Fish completions are only loaded once the user triggers them, we trigger them ourselves +# so we can properly delete any completions provided by another script. +# Only do this if the program can be found, or else fish may print some errors; besides, +# the existing completions will only be loaded if the program can be found. +if type -q "helm" + # The space after the program name is essential to trigger completion for the program + # and not completion of the program name itself. + # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish. + complete --do-complete "helm " > /dev/null 2>&1 +end + +# Remove any pre-existing completions for the program since we will be handling all of them. +complete -c helm -e + +# this will get called after the two calls below and clear the $__helm_perform_completion_once_result global +complete -c helm -n '__helm_clear_perform_completion_once_result' +# The call to __helm_prepare_completions will setup __helm_comp_results +# which provides the program's completion choices. +# If this doesn't require order preservation, we don't use the -k flag +complete -c helm -n 'not __helm_requires_order_preservation && __helm_prepare_completions' -f -a '$__helm_comp_results' +# otherwise we use the -k flag +complete -k -c helm -n '__helm_requires_order_preservation && __helm_prepare_completions' -f -a '$__helm_comp_results' diff --git a/fish/dot-config/fish/completions/kubectl.fish b/fish/dot-config/fish/completions/kubectl.fish new file mode 100644 index 0000000..8fa455e --- /dev/null +++ b/fish/dot-config/fish/completions/kubectl.fish @@ -0,0 +1,249 @@ + +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# fish completion for kubectl -*- shell-script -*- + +function __kubectl_debug + set -l file "$BASH_COMP_DEBUG_FILE" + if test -n "$file" + echo "$argv" >> $file + end +end + +function __kubectl_perform_completion + __kubectl_debug "Starting __kubectl_perform_completion" + + # Extract all args except the last one + set -l args (commandline -opc) + # Extract the last arg and escape it in case it is a space + set -l lastArg (string escape -- (commandline -ct)) + + __kubectl_debug "args: $args" + __kubectl_debug "last arg: $lastArg" + + # Disable ActiveHelp which is not supported for fish shell + set -l requestComp "KUBECTL_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg" + + __kubectl_debug "Calling $requestComp" + set -l results (eval $requestComp 2> /dev/null) + + # Some programs may output extra empty lines after the directive. + # Let's ignore them or else it will break completion. + # Ref: https://github.com/spf13/cobra/issues/1279 + for line in $results[-1..1] + if test (string trim -- $line) = "" + # Found an empty line, remove it + set results $results[1..-2] + else + # Found non-empty line, we have our proper output + break + end + end + + set -l comps $results[1..-2] + set -l directiveLine $results[-1] + + # For Fish, when completing a flag with an = (e.g., -n=) + # completions must be prefixed with the flag + set -l flagPrefix (string match -r -- '-.*=' "$lastArg") + + __kubectl_debug "Comps: $comps" + __kubectl_debug "DirectiveLine: $directiveLine" + __kubectl_debug "flagPrefix: $flagPrefix" + + for comp in $comps + printf "%s%s\n" "$flagPrefix" "$comp" + end + + printf "%s\n" "$directiveLine" +end + +# this function limits calls to __kubectl_perform_completion, by caching the result behind $__kubectl_perform_completion_once_result +function __kubectl_perform_completion_once + __kubectl_debug "Starting __kubectl_perform_completion_once" + + if test -n "$__kubectl_perform_completion_once_result" + __kubectl_debug "Seems like a valid result already exists, skipping __kubectl_perform_completion" + return 0 + end + + set --global __kubectl_perform_completion_once_result (__kubectl_perform_completion) + if test -z "$__kubectl_perform_completion_once_result" + __kubectl_debug "No completions, probably due to a failure" + return 1 + end + + __kubectl_debug "Performed completions and set __kubectl_perform_completion_once_result" + return 0 +end + +# this function is used to clear the $__kubectl_perform_completion_once_result variable after completions are run +function __kubectl_clear_perform_completion_once_result + __kubectl_debug "" + __kubectl_debug "========= clearing previously set __kubectl_perform_completion_once_result variable ==========" + set --erase __kubectl_perform_completion_once_result + __kubectl_debug "Successfully erased the variable __kubectl_perform_completion_once_result" +end + +function __kubectl_requires_order_preservation + __kubectl_debug "" + __kubectl_debug "========= checking if order preservation is required ==========" + + __kubectl_perform_completion_once + if test -z "$__kubectl_perform_completion_once_result" + __kubectl_debug "Error determining if order preservation is required" + return 1 + end + + set -l directive (string sub --start 2 $__kubectl_perform_completion_once_result[-1]) + __kubectl_debug "Directive is: $directive" + + set -l shellCompDirectiveKeepOrder 32 + set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2) + __kubectl_debug "Keeporder is: $keeporder" + + if test $keeporder -ne 0 + __kubectl_debug "This does require order preservation" + return 0 + end + + __kubectl_debug "This doesn't require order preservation" + return 1 +end + + +# This function does two things: +# - Obtain the completions and store them in the global __kubectl_comp_results +# - Return false if file completion should be performed +function __kubectl_prepare_completions + __kubectl_debug "" + __kubectl_debug "========= starting completion logic ==========" + + # Start fresh + set --erase __kubectl_comp_results + + __kubectl_perform_completion_once + __kubectl_debug "Completion results: $__kubectl_perform_completion_once_result" + + if test -z "$__kubectl_perform_completion_once_result" + __kubectl_debug "No completion, probably due to a failure" + # Might as well do file completion, in case it helps + return 1 + end + + set -l directive (string sub --start 2 $__kubectl_perform_completion_once_result[-1]) + set --global __kubectl_comp_results $__kubectl_perform_completion_once_result[1..-2] + + __kubectl_debug "Completions are: $__kubectl_comp_results" + __kubectl_debug "Directive is: $directive" + + set -l shellCompDirectiveError 1 + set -l shellCompDirectiveNoSpace 2 + set -l shellCompDirectiveNoFileComp 4 + set -l shellCompDirectiveFilterFileExt 8 + set -l shellCompDirectiveFilterDirs 16 + + if test -z "$directive" + set directive 0 + end + + set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2) + if test $compErr -eq 1 + __kubectl_debug "Received error directive: aborting." + # Might as well do file completion, in case it helps + return 1 + end + + set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2) + set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2) + if test $filefilter -eq 1; or test $dirfilter -eq 1 + __kubectl_debug "File extension filtering or directory filtering not supported" + # Do full file completion instead + return 1 + end + + set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2) + set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2) + + __kubectl_debug "nospace: $nospace, nofiles: $nofiles" + + # If we want to prevent a space, or if file completion is NOT disabled, + # we need to count the number of valid completions. + # To do so, we will filter on prefix as the completions we have received + # may not already be filtered so as to allow fish to match on different + # criteria than the prefix. + if test $nospace -ne 0; or test $nofiles -eq 0 + set -l prefix (commandline -t | string escape --style=regex) + __kubectl_debug "prefix: $prefix" + + set -l completions (string match -r -- "^$prefix.*" $__kubectl_comp_results) + set --global __kubectl_comp_results $completions + __kubectl_debug "Filtered completions are: $__kubectl_comp_results" + + # Important not to quote the variable for count to work + set -l numComps (count $__kubectl_comp_results) + __kubectl_debug "numComps: $numComps" + + if test $numComps -eq 1; and test $nospace -ne 0 + # We must first split on \t to get rid of the descriptions to be + # able to check what the actual completion will be. + # We don't need descriptions anyway since there is only a single + # real completion which the shell will expand immediately. + set -l split (string split --max 1 \t $__kubectl_comp_results[1]) + + # Fish won't add a space if the completion ends with any + # of the following characters: @=/:., + set -l lastChar (string sub -s -1 -- $split) + if not string match -r -q "[@=/:.,]" -- "$lastChar" + # In other cases, to support the "nospace" directive we trick the shell + # by outputting an extra, longer completion. + __kubectl_debug "Adding second completion to perform nospace directive" + set --global __kubectl_comp_results $split[1] $split[1]. + __kubectl_debug "Completions are now: $__kubectl_comp_results" + end + end + + if test $numComps -eq 0; and test $nofiles -eq 0 + # To be consistent with bash and zsh, we only trigger file + # completion when there are no other completions + __kubectl_debug "Requesting file completion" + return 1 + end + end + + return 0 +end + +# Since Fish completions are only loaded once the user triggers them, we trigger them ourselves +# so we can properly delete any completions provided by another script. +# Only do this if the program can be found, or else fish may print some errors; besides, +# the existing completions will only be loaded if the program can be found. +if type -q "kubectl" + # The space after the program name is essential to trigger completion for the program + # and not completion of the program name itself. + # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish. + complete --do-complete "kubectl " > /dev/null 2>&1 +end + +# Remove any pre-existing completions for the program since we will be handling all of them. +complete -c kubectl -e + +# this will get called after the two calls below and clear the $__kubectl_perform_completion_once_result global +complete -c kubectl -n '__kubectl_clear_perform_completion_once_result' +# The call to __kubectl_prepare_completions will setup __kubectl_comp_results +# which provides the program's completion choices. +# If this doesn't require order preservation, we don't use the -k flag +complete -c kubectl -n 'not __kubectl_requires_order_preservation && __kubectl_prepare_completions' -f -a '$__kubectl_comp_results' +# otherwise we use the -k flag +complete -k -c kubectl -n '__kubectl_requires_order_preservation && __kubectl_prepare_completions' -f -a '$__kubectl_comp_results' diff --git a/fish/dot-config/fish/completions/minikube.fish b/fish/dot-config/fish/completions/minikube.fish new file mode 100644 index 0000000..e75dc8b --- /dev/null +++ b/fish/dot-config/fish/completions/minikube.fish @@ -0,0 +1,249 @@ + +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# fish completion for minikube -*- shell-script -*- + +function __minikube_debug + set -l file "$BASH_COMP_DEBUG_FILE" + if test -n "$file" + echo "$argv" >> $file + end +end + +function __minikube_perform_completion + __minikube_debug "Starting __minikube_perform_completion" + + # Extract all args except the last one + set -l args (commandline -opc) + # Extract the last arg and escape it in case it is a space + set -l lastArg (string escape -- (commandline -ct)) + + __minikube_debug "args: $args" + __minikube_debug "last arg: $lastArg" + + # Disable ActiveHelp which is not supported for fish shell + set -l requestComp "MINIKUBE_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg" + + __minikube_debug "Calling $requestComp" + set -l results (eval $requestComp 2> /dev/null) + + # Some programs may output extra empty lines after the directive. + # Let's ignore them or else it will break completion. + # Ref: https://github.com/spf13/cobra/issues/1279 + for line in $results[-1..1] + if test (string trim -- $line) = "" + # Found an empty line, remove it + set results $results[1..-2] + else + # Found non-empty line, we have our proper output + break + end + end + + set -l comps $results[1..-2] + set -l directiveLine $results[-1] + + # For Fish, when completing a flag with an = (e.g., -n=) + # completions must be prefixed with the flag + set -l flagPrefix (string match -r -- '-.*=' "$lastArg") + + __minikube_debug "Comps: $comps" + __minikube_debug "DirectiveLine: $directiveLine" + __minikube_debug "flagPrefix: $flagPrefix" + + for comp in $comps + printf "%s%s\n" "$flagPrefix" "$comp" + end + + printf "%s\n" "$directiveLine" +end + +# this function limits calls to __minikube_perform_completion, by caching the result behind $__minikube_perform_completion_once_result +function __minikube_perform_completion_once + __minikube_debug "Starting __minikube_perform_completion_once" + + if test -n "$__minikube_perform_completion_once_result" + __minikube_debug "Seems like a valid result already exists, skipping __minikube_perform_completion" + return 0 + end + + set --global __minikube_perform_completion_once_result (__minikube_perform_completion) + if test -z "$__minikube_perform_completion_once_result" + __minikube_debug "No completions, probably due to a failure" + return 1 + end + + __minikube_debug "Performed completions and set __minikube_perform_completion_once_result" + return 0 +end + +# this function is used to clear the $__minikube_perform_completion_once_result variable after completions are run +function __minikube_clear_perform_completion_once_result + __minikube_debug "" + __minikube_debug "========= clearing previously set __minikube_perform_completion_once_result variable ==========" + set --erase __minikube_perform_completion_once_result + __minikube_debug "Successfully erased the variable __minikube_perform_completion_once_result" +end + +function __minikube_requires_order_preservation + __minikube_debug "" + __minikube_debug "========= checking if order preservation is required ==========" + + __minikube_perform_completion_once + if test -z "$__minikube_perform_completion_once_result" + __minikube_debug "Error determining if order preservation is required" + return 1 + end + + set -l directive (string sub --start 2 $__minikube_perform_completion_once_result[-1]) + __minikube_debug "Directive is: $directive" + + set -l shellCompDirectiveKeepOrder 32 + set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2) + __minikube_debug "Keeporder is: $keeporder" + + if test $keeporder -ne 0 + __minikube_debug "This does require order preservation" + return 0 + end + + __minikube_debug "This doesn't require order preservation" + return 1 +end + + +# This function does two things: +# - Obtain the completions and store them in the global __minikube_comp_results +# - Return false if file completion should be performed +function __minikube_prepare_completions + __minikube_debug "" + __minikube_debug "========= starting completion logic ==========" + + # Start fresh + set --erase __minikube_comp_results + + __minikube_perform_completion_once + __minikube_debug "Completion results: $__minikube_perform_completion_once_result" + + if test -z "$__minikube_perform_completion_once_result" + __minikube_debug "No completion, probably due to a failure" + # Might as well do file completion, in case it helps + return 1 + end + + set -l directive (string sub --start 2 $__minikube_perform_completion_once_result[-1]) + set --global __minikube_comp_results $__minikube_perform_completion_once_result[1..-2] + + __minikube_debug "Completions are: $__minikube_comp_results" + __minikube_debug "Directive is: $directive" + + set -l shellCompDirectiveError 1 + set -l shellCompDirectiveNoSpace 2 + set -l shellCompDirectiveNoFileComp 4 + set -l shellCompDirectiveFilterFileExt 8 + set -l shellCompDirectiveFilterDirs 16 + + if test -z "$directive" + set directive 0 + end + + set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2) + if test $compErr -eq 1 + __minikube_debug "Received error directive: aborting." + # Might as well do file completion, in case it helps + return 1 + end + + set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2) + set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2) + if test $filefilter -eq 1; or test $dirfilter -eq 1 + __minikube_debug "File extension filtering or directory filtering not supported" + # Do full file completion instead + return 1 + end + + set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2) + set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2) + + __minikube_debug "nospace: $nospace, nofiles: $nofiles" + + # If we want to prevent a space, or if file completion is NOT disabled, + # we need to count the number of valid completions. + # To do so, we will filter on prefix as the completions we have received + # may not already be filtered so as to allow fish to match on different + # criteria than the prefix. + if test $nospace -ne 0; or test $nofiles -eq 0 + set -l prefix (commandline -t | string escape --style=regex) + __minikube_debug "prefix: $prefix" + + set -l completions (string match -r -- "^$prefix.*" $__minikube_comp_results) + set --global __minikube_comp_results $completions + __minikube_debug "Filtered completions are: $__minikube_comp_results" + + # Important not to quote the variable for count to work + set -l numComps (count $__minikube_comp_results) + __minikube_debug "numComps: $numComps" + + if test $numComps -eq 1; and test $nospace -ne 0 + # We must first split on \t to get rid of the descriptions to be + # able to check what the actual completion will be. + # We don't need descriptions anyway since there is only a single + # real completion which the shell will expand immediately. + set -l split (string split --max 1 \t $__minikube_comp_results[1]) + + # Fish won't add a space if the completion ends with any + # of the following characters: @=/:., + set -l lastChar (string sub -s -1 -- $split) + if not string match -r -q "[@=/:.,]" -- "$lastChar" + # In other cases, to support the "nospace" directive we trick the shell + # by outputting an extra, longer completion. + __minikube_debug "Adding second completion to perform nospace directive" + set --global __minikube_comp_results $split[1] $split[1]. + __minikube_debug "Completions are now: $__minikube_comp_results" + end + end + + if test $numComps -eq 0; and test $nofiles -eq 0 + # To be consistent with bash and zsh, we only trigger file + # completion when there are no other completions + __minikube_debug "Requesting file completion" + return 1 + end + end + + return 0 +end + +# Since Fish completions are only loaded once the user triggers them, we trigger them ourselves +# so we can properly delete any completions provided by another script. +# Only do this if the program can be found, or else fish may print some errors; besides, +# the existing completions will only be loaded if the program can be found. +if type -q "minikube" + # The space after the program name is essential to trigger completion for the program + # and not completion of the program name itself. + # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish. + complete --do-complete "minikube " > /dev/null 2>&1 +end + +# Remove any pre-existing completions for the program since we will be handling all of them. +complete -c minikube -e + +# this will get called after the two calls below and clear the $__minikube_perform_completion_once_result global +complete -c minikube -n '__minikube_clear_perform_completion_once_result' +# The call to __minikube_prepare_completions will setup __minikube_comp_results +# which provides the program's completion choices. +# If this doesn't require order preservation, we don't use the -k flag +complete -c minikube -n 'not __minikube_requires_order_preservation && __minikube_prepare_completions' -f -a '$__minikube_comp_results' +# otherwise we use the -k flag +complete -k -c minikube -n '__minikube_requires_order_preservation && __minikube_prepare_completions' -f -a '$__minikube_comp_results' diff --git a/fish/dot-config/fish/completions/oc.fish b/fish/dot-config/fish/completions/oc.fish new file mode 100644 index 0000000..a97c016 --- /dev/null +++ b/fish/dot-config/fish/completions/oc.fish @@ -0,0 +1,236 @@ + +# fish completion for oc -*- shell-script -*- + +function __oc_debug + set -l file "$BASH_COMP_DEBUG_FILE" + if test -n "$file" + echo "$argv" >> $file + end +end + +function __oc_perform_completion + __oc_debug "Starting __oc_perform_completion" + + # Extract all args except the last one + set -l args (commandline -opc) + # Extract the last arg and escape it in case it is a space + set -l lastArg (string escape -- (commandline -ct)) + + __oc_debug "args: $args" + __oc_debug "last arg: $lastArg" + + # Disable ActiveHelp which is not supported for fish shell + set -l requestComp "OC_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg" + + __oc_debug "Calling $requestComp" + set -l results (eval $requestComp 2> /dev/null) + + # Some programs may output extra empty lines after the directive. + # Let's ignore them or else it will break completion. + # Ref: https://github.com/spf13/cobra/issues/1279 + for line in $results[-1..1] + if test (string trim -- $line) = "" + # Found an empty line, remove it + set results $results[1..-2] + else + # Found non-empty line, we have our proper output + break + end + end + + set -l comps $results[1..-2] + set -l directiveLine $results[-1] + + # For Fish, when completing a flag with an = (e.g., -n=) + # completions must be prefixed with the flag + set -l flagPrefix (string match -r -- '-.*=' "$lastArg") + + __oc_debug "Comps: $comps" + __oc_debug "DirectiveLine: $directiveLine" + __oc_debug "flagPrefix: $flagPrefix" + + for comp in $comps + printf "%s%s\n" "$flagPrefix" "$comp" + end + + printf "%s\n" "$directiveLine" +end + +# this function limits calls to __oc_perform_completion, by caching the result behind $__oc_perform_completion_once_result +function __oc_perform_completion_once + __oc_debug "Starting __oc_perform_completion_once" + + if test -n "$__oc_perform_completion_once_result" + __oc_debug "Seems like a valid result already exists, skipping __oc_perform_completion" + return 0 + end + + set --global __oc_perform_completion_once_result (__oc_perform_completion) + if test -z "$__oc_perform_completion_once_result" + __oc_debug "No completions, probably due to a failure" + return 1 + end + + __oc_debug "Performed completions and set __oc_perform_completion_once_result" + return 0 +end + +# this function is used to clear the $__oc_perform_completion_once_result variable after completions are run +function __oc_clear_perform_completion_once_result + __oc_debug "" + __oc_debug "========= clearing previously set __oc_perform_completion_once_result variable ==========" + set --erase __oc_perform_completion_once_result + __oc_debug "Successfully erased the variable __oc_perform_completion_once_result" +end + +function __oc_requires_order_preservation + __oc_debug "" + __oc_debug "========= checking if order preservation is required ==========" + + __oc_perform_completion_once + if test -z "$__oc_perform_completion_once_result" + __oc_debug "Error determining if order preservation is required" + return 1 + end + + set -l directive (string sub --start 2 $__oc_perform_completion_once_result[-1]) + __oc_debug "Directive is: $directive" + + set -l shellCompDirectiveKeepOrder 32 + set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2) + __oc_debug "Keeporder is: $keeporder" + + if test $keeporder -ne 0 + __oc_debug "This does require order preservation" + return 0 + end + + __oc_debug "This doesn't require order preservation" + return 1 +end + + +# This function does two things: +# - Obtain the completions and store them in the global __oc_comp_results +# - Return false if file completion should be performed +function __oc_prepare_completions + __oc_debug "" + __oc_debug "========= starting completion logic ==========" + + # Start fresh + set --erase __oc_comp_results + + __oc_perform_completion_once + __oc_debug "Completion results: $__oc_perform_completion_once_result" + + if test -z "$__oc_perform_completion_once_result" + __oc_debug "No completion, probably due to a failure" + # Might as well do file completion, in case it helps + return 1 + end + + set -l directive (string sub --start 2 $__oc_perform_completion_once_result[-1]) + set --global __oc_comp_results $__oc_perform_completion_once_result[1..-2] + + __oc_debug "Completions are: $__oc_comp_results" + __oc_debug "Directive is: $directive" + + set -l shellCompDirectiveError 1 + set -l shellCompDirectiveNoSpace 2 + set -l shellCompDirectiveNoFileComp 4 + set -l shellCompDirectiveFilterFileExt 8 + set -l shellCompDirectiveFilterDirs 16 + + if test -z "$directive" + set directive 0 + end + + set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2) + if test $compErr -eq 1 + __oc_debug "Received error directive: aborting." + # Might as well do file completion, in case it helps + return 1 + end + + set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2) + set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2) + if test $filefilter -eq 1; or test $dirfilter -eq 1 + __oc_debug "File extension filtering or directory filtering not supported" + # Do full file completion instead + return 1 + end + + set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2) + set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2) + + __oc_debug "nospace: $nospace, nofiles: $nofiles" + + # If we want to prevent a space, or if file completion is NOT disabled, + # we need to count the number of valid completions. + # To do so, we will filter on prefix as the completions we have received + # may not already be filtered so as to allow fish to match on different + # criteria than the prefix. + if test $nospace -ne 0; or test $nofiles -eq 0 + set -l prefix (commandline -t | string escape --style=regex) + __oc_debug "prefix: $prefix" + + set -l completions (string match -r -- "^$prefix.*" $__oc_comp_results) + set --global __oc_comp_results $completions + __oc_debug "Filtered completions are: $__oc_comp_results" + + # Important not to quote the variable for count to work + set -l numComps (count $__oc_comp_results) + __oc_debug "numComps: $numComps" + + if test $numComps -eq 1; and test $nospace -ne 0 + # We must first split on \t to get rid of the descriptions to be + # able to check what the actual completion will be. + # We don't need descriptions anyway since there is only a single + # real completion which the shell will expand immediately. + set -l split (string split --max 1 \t $__oc_comp_results[1]) + + # Fish won't add a space if the completion ends with any + # of the following characters: @=/:., + set -l lastChar (string sub -s -1 -- $split) + if not string match -r -q "[@=/:.,]" -- "$lastChar" + # In other cases, to support the "nospace" directive we trick the shell + # by outputting an extra, longer completion. + __oc_debug "Adding second completion to perform nospace directive" + set --global __oc_comp_results $split[1] $split[1]. + __oc_debug "Completions are now: $__oc_comp_results" + end + end + + if test $numComps -eq 0; and test $nofiles -eq 0 + # To be consistent with bash and zsh, we only trigger file + # completion when there are no other completions + __oc_debug "Requesting file completion" + return 1 + end + end + + return 0 +end + +# Since Fish completions are only loaded once the user triggers them, we trigger them ourselves +# so we can properly delete any completions provided by another script. +# Only do this if the program can be found, or else fish may print some errors; besides, +# the existing completions will only be loaded if the program can be found. +if type -q "oc" + # The space after the program name is essential to trigger completion for the program + # and not completion of the program name itself. + # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish. + complete --do-complete "oc " > /dev/null 2>&1 +end + +# Remove any pre-existing completions for the program since we will be handling all of them. +complete -c oc -e + +# this will get called after the two calls below and clear the $__oc_perform_completion_once_result global +complete -c oc -n '__oc_clear_perform_completion_once_result' +# The call to __oc_prepare_completions will setup __oc_comp_results +# which provides the program's completion choices. +# If this doesn't require order preservation, we don't use the -k flag +complete -c oc -n 'not __oc_requires_order_preservation && __oc_prepare_completions' -f -a '$__oc_comp_results' +# otherwise we use the -k flag +complete -k -c oc -n '__oc_requires_order_preservation && __oc_prepare_completions' -f -a '$__oc_comp_results'