File tree Expand file tree Collapse file tree 4 files changed +22
-38
lines changed Expand file tree Collapse file tree 4 files changed +22
-38
lines changed Original file line number Diff line number Diff line change 33# AUTOCOMPLETION FOR ZSH
44# Reference: https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html
55
6- # `list` is populated with the names of all Git worktrees in the current directory.
7- # git rev-parse --git-dir: Check if the current directory is a Git repository.
8- # git worktree list --porcelain: List all worktrees in an easily parse-able way.
9- # awk '/^worktree / { sub("worktree ", ""); print; }': Extract the worktree names and remove the `worktree ` prefix.
10- # xargs -I{} basename {}: Get the base name of each worktree path, preserving whitespace.
11- list="$(git rev-parse --git-dir &> /dev/null \
12- && git worktree list --porcelain \
13- | awk '/^worktree / { sub("worktree ", ""); print; }' \
14- | xargs -I{} basename {})"
15-
16- # Split the output into an array, line-by-line.
17- list=("${(@f)${list}}")
18-
196# Declare an associative array named `opts`
207declare -A opts
218
9+ # Split the worktree names into an array, line-by-line.
10+ list=("${(@f)$(wt names)}")
11+
2212# Create associative array with key same as its value
2313# Completion keywords are taken as keys of arrays and the possible matches are their values
2414for item in $list; do
2515 # Escape every element's special characters
2616 item=$(printf '%q' "$item")
2717 opts+=(["$item"]="$item")
2818done
19+
2920# Add the keys of `opts` as completion options for the `wt` command.
3021# `-Q` quotes the completion options.
3122# `-a` specifies that the options are taken from an associative array.
Original file line number Diff line number Diff line change 11# AUTOCOMPLETION FOR FISH
22# Reference: https://fishshell.com/docs/current/completions.html
33
4- # if git rev-parse --git-dir &> /dev/null: Only check for completions if the user is in a git repository
5- # git worktree list --porcelain: list all the available worktrees in a format that's easy to parse
6- # | awk '/^worktree / { sub("worktree ", ""); print; }': grab only lines beginning with worktree and keep only their path
7- # | xargs -I{} basename {}: get the basename of each worktree
8- # separated by space
9-
10- function _wt_complete
11- git rev-parse --git-dir & > /dev/null && git worktree list --porcelain | awk ' /^worktree / { sub("worktree ", ""); print; }' | xargs -I {} basename {}
12- end
13-
14- complete -c wt -f -n ' __fish_is_nth_token 1' -a " (_wt_complete)"
4+ complete -c wt -f -n ' __fish_is_nth_token 1' -a " (wt names)"
Original file line number Diff line number Diff line change 33# AUTOCOMPLETION FOR BASH
44# Reference: https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html
55
6- # git rev-parse --git-dir &> /dev/null: check if the user is in a git repo
7- # && git worktree list --porcelain: list all the available worktrees in a format that's easy to parse
8- # | awk '/^worktree / { sub("worktree ", ""); print; }': grab only lines beginning with worktree and keep only their path
9- # | xargs -I{} basename {}: get the basename of each worktree
10- # separated by space
11-
126_wt () {
137 local cur
8+ # The currently typed prefix to be completed
149 cur=" ${COMP_WORDS[COMP_CWORD]} "
1510 COMPREPLY=()
1611
1712 # Only show suggestions for the root command (wt)
1813 # Pass autocompletion suggestion as "words (-W)" to `compgen` separated by space
14+ # Escape each suggestion special characters
1915 if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]]; then
20- local list suggestions
21-
22- list=$( git rev-parse --git-dir & > /dev/null \
23- && git worktree list --porcelain \
24- | awk ' /^worktree / { sub("worktree ", ""); print; }' \
25- | xargs -I{} basename {})
26-
16+ # Use the newline as a separator for the suggestions
2717 local IFS=$' \n '
28- suggestions=$( compgen -W " $list " -- " $cur " )
18+ local suggestions
19+ suggestions=$( compgen -W " $( wt names) " -- " $cur " )
2920 for word in $suggestions ; do
3021 COMPREPLY+=(" $( printf ' %q' " $word " ) " )
3122 done
Original file line number Diff line number Diff line change @@ -20,11 +20,20 @@ worktree_list() {
2020 git worktree list
2121}
2222
23+ worktree_list_names () {
24+ if git rev-parse --git-dir & > /dev/null; then
25+ git worktree list --porcelain \
26+ | awk ' /^worktree / { sub("worktree ", ""); print; }' \
27+ | xargs -I{} basename {}
28+ fi
29+ }
30+
2331help_message () {
2432 echo -e " wt lets you switch between your git worktrees with speed.\n"
2533 echo " Usage:"
2634 echo -e " \twt <worktree-name>: search for worktree names and change to that directory."
2735 echo -e " \twt list: list out all the git worktrees."
36+ echo -e " \twt names: list out only the git worktree names."
2837 echo -e " \twt update: update to the latest release of worktree switcher."
2938 echo -e " \twt version: show the CLI version."
3039 echo -e " \twt help: shows this help message."
@@ -86,6 +95,9 @@ case "${args[0]}" in
8695list)
8796 worktree_list
8897 ;;
98+ names)
99+ worktree_list_names
100+ ;;
89101update)
90102 update
91103 ;;
You can’t perform that action at this time.
0 commit comments