Skip to content

Commit 1cc473c

Browse files
committed
Use names sub-command in completions
1 parent 65cf677 commit 1cc473c

File tree

3 files changed

+10
-40
lines changed

3 files changed

+10
-40
lines changed

completions/_wt_completion

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,20 @@
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`
207
declare -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
2414
for item in $list; do
2515
# Escape every element's special characters
2616
item=$(printf '%q' "$item")
2717
opts+=(["$item"]="$item")
2818
done
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.

completions/wt.fish

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
# AUTOCOMPLETION FOR FISH
22
# Reference: https://fishshell.com/docs/current/completions.html
33

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)"

completions/wt_completion

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,20 @@
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

0 commit comments

Comments
 (0)