Skip to content

Commit

Permalink
Move worktree names code to the main wt script
Browse files Browse the repository at this point in the history
It is used in all completions. So, might as well move it into the main
script.
  • Loading branch information
mateusauler committed May 1, 2024
1 parent f03da74 commit 524e68b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
17 changes: 4 additions & 13 deletions completions/_wt_completion
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@
# 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
# Escape every element's special characters
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.
Expand Down
12 changes: 1 addition & 11 deletions completions/wt.fish
Original file line number Diff line number Diff line change
@@ -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)"
19 changes: 5 additions & 14 deletions completions/wt_completion
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions wt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <worktree-name>: 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."
Expand Down Expand Up @@ -83,6 +92,9 @@ if [ -z "${args[0]}" ]; then
fi

case "${args[0]}" in
names)
worktree_list_names
;;
list)
worktree_list
;;
Expand Down

0 comments on commit 524e68b

Please sign in to comment.