From 524e68bd5d230121d6551f26803a04bc8bee0478 Mon Sep 17 00:00:00 2001 From: Mateus Auler Date: Wed, 1 May 2024 18:57:10 -0300 Subject: [PATCH] Move worktree names code to the main wt script It is used in all completions. So, might as well move it into the main script. --- completions/_wt_completion | 17 ++++------------- completions/wt.fish | 12 +----------- completions/wt_completion | 19 +++++-------------- wt | 12 ++++++++++++ 4 files changed, 22 insertions(+), 38 deletions(-) diff --git a/completions/_wt_completion b/completions/_wt_completion index 476d551..bea3239 100644 --- a/completions/_wt_completion +++ b/completions/_wt_completion @@ -3,22 +3,12 @@ # AUTOCOMPLETION FOR ZSH # Reference: https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html -# `list` is populated with the names of all Git worktrees in the current directory. -# git rev-parse --git-dir: Check if the current directory is a Git repository. -# git worktree list --porcelain: List all worktrees in an easily parse-able way. -# awk '/^worktree / { sub("worktree ", ""); print; }': Extract the worktree names and remove the `worktree ` prefix. -# xargs -I{} basename {}: Get the base name of each worktree path, preserving whitespace. -list="$(git rev-parse --git-dir &> /dev/null \ - && git worktree list --porcelain \ - | awk '/^worktree / { sub("worktree ", ""); print; }' \ - | xargs -I{} basename {})" - -# Split the output into an array, line-by-line. -list=("${(@f)${list}}") - # Declare an associative array named `opts` declare -A opts +# Split the worktree names into an array, line-by-line. +list=("${(@f)$(wt names)}") + # Create associative array with key same as its value # Completion keywords are taken as keys of arrays and the possible matches are their values for item in $list; do @@ -26,6 +16,7 @@ for item in $list; do item=$(printf '%q' "$item") opts+=(["$item"]="$item") done + # Add the keys of `opts` as completion options for the `wt` command. # `-Q` quotes the completion options. # `-a` specifies that the options are taken from an associative array. diff --git a/completions/wt.fish b/completions/wt.fish index e049d44..24c1cbb 100644 --- a/completions/wt.fish +++ b/completions/wt.fish @@ -1,14 +1,4 @@ # AUTOCOMPLETION FOR FISH # Reference: https://fishshell.com/docs/current/completions.html -# if git rev-parse --git-dir &> /dev/null: Only check for completions if the user is in a git repository -# git worktree list --porcelain: list all the available worktrees in a format that's easy to parse -# | awk '/^worktree / { sub("worktree ", ""); print; }': grab only lines beginning with worktree and keep only their path -# | xargs -I{} basename {}: get the basename of each worktree -# separated by space - -function _wt_complete - git rev-parse --git-dir &> /dev/null && git worktree list --porcelain | awk '/^worktree / { sub("worktree ", ""); print; }' | xargs -I{} basename {} -end - -complete -c wt -f -n '__fish_is_nth_token 1' -a "(_wt_complete)" +complete -c wt -f -n '__fish_is_nth_token 1' -a "(wt names)" diff --git a/completions/wt_completion b/completions/wt_completion index ee17473..723df49 100644 --- a/completions/wt_completion +++ b/completions/wt_completion @@ -3,29 +3,20 @@ # AUTOCOMPLETION FOR BASH # Reference: https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html -# git rev-parse --git-dir &> /dev/null: check if the user is in a git repo -# && git worktree list --porcelain: list all the available worktrees in a format that's easy to parse -# | awk '/^worktree / { sub("worktree ", ""); print; }': grab only lines beginning with worktree and keep only their path -# | xargs -I{} basename {}: get the basename of each worktree -# separated by space - _wt() { local cur + # The currently typed prefix to be completed cur="${COMP_WORDS[COMP_CWORD]}" COMPREPLY=() # Only show suggestions for the root command (wt) # Pass autocompletion suggestion as "words (-W)" to `compgen` separated by space + # Escape each suggestion special characters if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]]; then - local list suggestions - - list=$(git rev-parse --git-dir &> /dev/null \ - && git worktree list --porcelain \ - | awk '/^worktree / { sub("worktree ", ""); print; }' \ - | xargs -I{} basename {}) - + # Use the newline as a separator for the suggestions local IFS=$'\n' - suggestions=$(compgen -W "$list" -- "$cur") + local suggestions + suggestions=$(compgen -W "$(wt names)" -- "$cur") for word in $suggestions; do COMPREPLY+=("$(printf '%q' "$word")") done diff --git a/wt b/wt index 766b613..7875ef0 100755 --- a/wt +++ b/wt @@ -20,11 +20,20 @@ worktree_list() { git worktree list } +worktree_list_names() { + if git rev-parse --git-dir &> /dev/null; then + git worktree list --porcelain \ + | awk '/^worktree / { sub("worktree ", ""); print; }' \ + | xargs -I{} basename {} + fi +} + help_message() { echo -e "wt lets you switch between your git worktrees with speed.\n" echo "Usage:" echo -e "\twt : search for worktree names and change to that directory." echo -e "\twt list: list out all the git worktrees." + echo -e "\twt names: list out only the git worktree names." echo -e "\twt update: update to the latest release of worktree switcher." echo -e "\twt version: show the CLI version." echo -e "\twt help: shows this help message." @@ -83,6 +92,9 @@ if [ -z "${args[0]}" ]; then fi case "${args[0]}" in +names) + worktree_list_names + ;; list) worktree_list ;;