File tree Expand file tree Collapse file tree 3 files changed +10
-40
lines changed Expand file tree Collapse file tree 3 files changed +10
-40
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
- # wt list: list all the available worktrees
5
- # | awk '{ print $1; }': grab the first column of the output
6
- # | tr "\n" " ": replace line break character with space to put the worktrees on single line
7
- # separated by space
8
-
9
- set list (wt list | awk ' { print $1; }' | tr " \n" " " )
10
- set opts " "
11
-
12
- for item in (string split " " " $list " )
13
- set -a opts (basename -- " $item " )
14
- end
15
-
16
- complete -c wt -f -n ' __fish_is_nth_token 1' -a " $opts "
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
You can’t perform that action at this time.
0 commit comments