diff --git a/zsh/containers.zsh b/zsh/containers.zsh index 2730122..87365fd 100644 --- a/zsh/containers.zsh +++ b/zsh/containers.zsh @@ -32,19 +32,5 @@ fi if command-exists orb; then alias oc="orb" - - _setup-orb-completion() { - local target - target="${ZSH_COMPLETIONS}/_orb" - - if [ ! -f "$target" ] || [ "$target" -ot "$(command -v orb)" ]; then - echo "Setting up completion for orb -- $target" - mkdir -p "$(dirname "$target")" - orb completion zsh > "$target" - chmod +x "$target" - autoload -U compinit && compinit - fi - } - - _setup-orb-completion + setup-completions orb "$(command -v orb)" orb completion zsh fi diff --git a/zsh/kubernetes.zsh b/zsh/kubernetes.zsh index 81ee2fa..ae5704d 100644 --- a/zsh/kubernetes.zsh +++ b/zsh/kubernetes.zsh @@ -9,19 +9,7 @@ alias hl="helm" alias mk="minikube" if command-exists kubectl; then - _setup-kubectl-completion() { - local target - target="$ZSH_COMPLETIONS/_kubectl" - - if [ ! -f "$target" ] || [ "$target" -ot "$(command -v kubectl)" ]; then - echo "Setting up completion for kubectl -- $target" - mkdir -p "$ZSH_COMPLETIONS" - kubectl completion zsh > "$target" - chmod +x "$target" - autoload -U compinit && compinit - fi - } - _setup-kubectl-completion + setup-completions kubectl "$(command -v kubectl)" kubectl completion zsh if command-exists brew-prefix; then switch() { diff --git a/zsh/rust.zsh b/zsh/rust.zsh index a1ee9cc..7c005d6 100644 --- a/zsh/rust.zsh +++ b/zsh/rust.zsh @@ -22,18 +22,10 @@ fi # ============================================================================== if command-exists rustup; then - _rustup() { - unset -f _rustup - eval "$(rustup completions zsh)" - } - compctl -K _rustup rustup + setup-completions rustup "$(command -v rustup)" rustup completions zsh if command-exists cargo; then - _cargo() { - unset -f _cargo - eval "$(rustup completions zsh cargo)" - } - compctl -K _cargo cargo + setup-completions cargo "$(command -v cargo)" rustup completions zsh cargo fi fi diff --git a/zshrc b/zshrc index 3b11b09..106ac96 100644 --- a/zshrc +++ b/zshrc @@ -67,6 +67,42 @@ zstyle ':completion:*:make:*' tag-order targets if [ -d "$ZSH_COMPLETIONS" ]; then fpath=("$ZSH_COMPLETIONS" $fpath); fi if [ -d "$DOTZSH_SITEFUNS" ]; then fpath=("$DOTZSH_SITEFUNS" $fpath); fi +# setup-completions is a helper function to setup completions for a given +# command. It takes the command name, the source of the completion, and the +# command to run to generate the completions. +# +# Source should be a file that the completions are generated from. For example, +# for rustup, the source is the rustup binary. If completions file has already +# been generated, the source file is used to determine if the completions need +# to be re-generated. +# +# The command to run to generate the completions should be a command that +# generates zsh completions. For example, for rustup, the command is: +# +# rustup completions zsh +# +# Example usage: +# +# setup-completions rustup "$(command -v rustup)" rustup completions zsh +# +# This will generate the completions for rustup and place them in the +# ZSH_COMPLETIONS directory. +setup-completions() { + local cmd="$1" + local source="$2" + shift 2 + local target + target="${ZSH_COMPLETIONS}/_${cmd}" + + if [ ! -f "$target" ] || [ "$target" -ot "$source" ]; then + echo "Setting up completion for $cmd -- $target" + mkdir -p "$(dirname "$target")" + "$@" > "$target" + chmod +x "$target" + autoload -U compinit && compinit + fi +} + # ============================================================================== # Edit command line # ============================================================================== @@ -94,7 +130,6 @@ fi MISE_HOME="$HOME/.local/share/mise" MISE_ZSH_INIT="$MISE_HOME/shell/init.zsh" -MISE_COMPLETIONS_PATH="${ZSH_COMPLETIONS}/_mise" export MISE_INSTALL_PATH="$MISE_HOME/bin/mise" if ! command-exists mise; then @@ -111,12 +146,7 @@ if command-exists mise; then fi source "$MISE_ZSH_INIT" - if [ ! -f "$MISE_COMPLETIONS_PATH" ] || [ "$MISE_COMPLETIONS_PATH" -ot "$MISE_INSTALL_PATH" ]; then - echo "Setting up completion for mise -- $MISE_COMPLETIONS_PATH" - mkdir -p "$(dirname "$MISE_COMPLETIONS_PATH")" - mise completions zsh > "$MISE_COMPLETIONS_PATH" - chmod +x "$MISE_COMPLETIONS_PATH" - fi + setup-completions mise "$MISE_INSTALL_PATH" mise completions zsh fi # ==============================================================================