3v1n0 / zsh-bash-completions-fallback

Simple zsh plugin to support bash completions for a command if no native one is available
GNU Lesser General Public License v3.0
58 stars 5 forks source link

Custom bash completions in zsh #13

Open xatnight opened 5 months ago

xatnight commented 5 months ago

Hi! First off all, thanks for this wonderful script.

I'm currently in the process of migrating from oh-my-zsh to a setup with no plugin managers and I'm trying to learn how to setup my stuff, but I'm too stupid to make your script work with my custom bash completions. With oh-my-zsh I would put my completion scripts into a folder that's included in my fpath and then autoload them into my .zshrc. For example for the binary kns it would look like this:

autoload -Uz _kns
autoload -U +X bashcompinit && bashcompinit
complete -F _kns kns

With zsh-bash-completions-fallback installed I thought I simply have to put the completion script into /usr/share/bash-completion/completions and be done but I think I'm missing something.

The script in question if it's needed (/usr/share/bash-completion/completions/_kns):

_kns() {
    if ! kubectl config current-context > /dev/null 2>&1 ; then
        return
    fi

    local cur="${COMP_WORDS[COMP_CWORD]}"
    if [[ ${cur} == -* || ${COMP_CWORD} -ne 1 ]]; then
        return
    fi

    local IFS=$'\n'
    local suggestions=$(kubectl get ns -o custom-columns=NAME:.metadata.name --no-headers)
    COMPREPLY=($(compgen -W "$suggestions" -- ${cur}))
}

complete -F _kns kns