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 3
3
# AUTOCOMPLETION FOR ZSH
4
4
# Reference: https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html
5
5
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
-
19
6
# Declare an associative array named `opts`
20
7
declare -A opts
21
8
9
+ # Split the worktree names into an array, line-by-line.
10
+ list=("${(@f)$(wt names)}")
11
+
22
12
# Create associative array with key same as its value
23
13
# Completion keywords are taken as keys of arrays and the possible matches are their values
24
14
for item in $list; do
25
15
# Escape every element's special characters
26
16
item=$(printf '%q' "$item")
27
17
opts+=(["$item"]="$item")
28
18
done
19
+
29
20
# Add the keys of `opts` as completion options for the `wt` command.
30
21
# `-Q` quotes the completion options.
31
22
# `-a` specifies that the options are taken from an associative array.
Original file line number Diff line number Diff line change 1
1
# AUTOCOMPLETION FOR FISH
2
2
# Reference: https://fishshell.com/docs/current/completions.html
3
3
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 3
3
# AUTOCOMPLETION FOR BASH
4
4
# Reference: https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html
5
5
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
-
12
6
_wt () {
13
7
local cur
8
+ # The currently typed prefix to be completed
14
9
cur=" ${COMP_WORDS[COMP_CWORD]} "
15
10
COMPREPLY=()
16
11
17
12
# Only show suggestions for the root command (wt)
18
13
# Pass autocompletion suggestion as "words (-W)" to `compgen` separated by space
14
+ # Escape each suggestion special characters
19
15
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
27
17
local IFS=$' \n '
28
- suggestions=$( compgen -W " $list " -- " $cur " )
18
+ local suggestions
19
+ suggestions=$( compgen -W " $( wt names) " -- " $cur " )
29
20
for word in $suggestions ; do
30
21
COMPREPLY+=(" $( printf ' %q' " $word " ) " )
31
22
done
Original file line number Diff line number Diff line change @@ -20,11 +20,20 @@ worktree_list() {
20
20
git worktree list
21
21
}
22
22
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
+
23
31
help_message () {
24
32
echo -e " wt lets you switch between your git worktrees with speed.\n"
25
33
echo " Usage:"
26
34
echo -e " \twt <worktree-name>: search for worktree names and change to that directory."
27
35
echo -e " \twt list: list out all the git worktrees."
36
+ echo -e " \twt names: list out only the git worktree names."
28
37
echo -e " \twt update: update to the latest release of worktree switcher."
29
38
echo -e " \twt version: show the CLI version."
30
39
echo -e " \twt help: shows this help message."
@@ -83,6 +92,9 @@ if [ -z "${args[0]}" ]; then
83
92
fi
84
93
85
94
case " ${args[0]} " in
95
+ names)
96
+ worktree_list_names
97
+ ;;
86
98
list)
87
99
worktree_list
88
100
;;
You can’t perform that action at this time.
0 commit comments