Skip to content

Commit

Permalink
Fix zsh completion when worktree names have spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusauler committed May 1, 2024
1 parent 06c80d8 commit c1682c7
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions completions/_wt_completion
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
# AUTOCOMPLETION FOR ZSH
# Reference: https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html

# wt list: list all the available worktrees
# | awk '{ print $1; }': grab the first column of the output
# | tr "\n" " ": replace line break character with space to put the worktrees on single line
# separated by space

list="$(wt list | awk '{ print $1; }' | tr "\n" " ")"
# 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
# shwordsplit: iterate over a string separated by space (like sh/bash)
setopt shwordsplit
for item in $list; do
base="$(basename -- "$item")"
opts+=(["$base"]="$base")
# Escape every element's special characters
item=$(printf '%q' "$item")
opts+=(["$item"]="$item")
done
unsetopt shwordsplit

# 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.
compadd -Qa opts

0 comments on commit c1682c7

Please sign in to comment.