diff --git a/completions/_wt b/completions/_wt new file mode 100644 index 0000000..54e5973 --- /dev/null +++ b/completions/_wt @@ -0,0 +1,70 @@ +#compdef wt + +_wt_worktree_names() { + local -a names + # Skip first entry (main worktree), extract branch names from brackets + names=(${(f)"$(git worktree list 2>/dev/null | tail -n +2 | sed -n 's/.*\[\(.*\)\].*/\1/p')"}) + compadd -a names +} + +_wt_git_branches() { + local -a branches + branches=(${(f)"$(git branch -a 2>/dev/null | sed 's/^[* ]*//' | sed 's|remotes/origin/||' | sort -u)"}) + compadd -a branches +} + +_wt() { + local -a subcmds + subcmds=( + 'new:Create new worktree' + 'ls:List all worktrees with status' + 'cd:Change to worktree (main if no name)' + 'up:Run up commands (spin up dev environment)' + 'down:Run down commands (spin down dev environment)' + 'rm:Remove worktree' + 'purge:Interactive cleanup of clean worktrees' + 'prune:Clean up stale worktree references' + 'config:Show path to config file' + 'update:Update wt to latest version' + 'uninstall:Uninstall wt-cli' + 'version:Show version' + 'help:Show help' + ) + + if (( CURRENT == 2 )); then + _describe 'command' subcmds + return + fi + + case "${words[2]}" in + cd) + _wt_worktree_names + ;; + rm) + if [[ "${words[CURRENT-1]}" == "-f" ]]; then + _wt_worktree_names + elif [[ "${words[CURRENT]}" == -* ]]; then + compadd -- -f + else + _wt_worktree_names + fi + ;; + new) + case "${words[CURRENT-1]}" in + -b|--branch) + _wt_git_branches + ;; + -p|--pr|-u|--up|-n|--no-cd) + # no completion + ;; + *) + if [[ "${words[CURRENT]}" == -* ]]; then + compadd -- -b -p -u -n + fi + ;; + esac + ;; + esac +} + +_wt "$@" diff --git a/install.sh b/install.sh index f7e7f25..eccf6f7 100644 --- a/install.sh +++ b/install.sh @@ -33,6 +33,11 @@ echo "Downloading wt.sh..." curl -sSL "$BASE_URL/wt.sh" -o "$WT_SH" chmod +x "$WT_SH" +# Download zsh completions +echo "Downloading zsh completions..." +mkdir -p "$WT_DIR/completions" +curl -sSL "$BASE_URL/completions/_wt" -o "$WT_DIR/completions/_wt" + # Create default config if [[ ! -f "$CONFIG_FILE" ]]; then mkdir -p "$CONFIG_DIR" diff --git a/wt.sh b/wt.sh index 90c6484..d4644c2 100644 --- a/wt.sh +++ b/wt.sh @@ -2,7 +2,7 @@ # wt - Git worktree management CLI # https://github.com/jorgensandhaug/wt-cli -_WT_VERSION="1.0.0" +_WT_VERSION="1.1.0" _wt_config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/wt" _wt_config_file="$_wt_config_dir/config.json" @@ -788,3 +788,9 @@ wt() { fi _wt_main "$@" } + +# zsh completion +if [[ -n "$ZSH_VERSION" ]]; then + fpath=(~/.wt/completions $fpath) + autoload -Uz compinit && compinit -C +fi