Skip to content

Commit 127582d

Browse files
committed
Add interactive worktree selection using fzf
1 parent e507bca commit 127582d

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

wt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44
# Switch between git worktrees with speed.
55

66
args=("$@")
7-
VERSION="0.1.1"
7+
VERSION="0.1.2"
88
TMP_PATH=$(mktemp)
99
BINARY_PATH=$(which wt)
1010
JQ_URL="https://stedolan.github.io/jq/download"
@@ -14,6 +14,20 @@ RELEASE_API_URL="https://api.github.com/repos/yankeexe/git-worktree-switcher/rel
1414

1515
# Escape forward slash
1616
arg=$(echo "${args[0]}" | sed 's/\//\\\//g')
17+
is_interactive=false
18+
19+
for arg in "${args[@]}"; do
20+
if [[ "$arg" == "-i" ]]; then
21+
if command -v fzf &> /dev/null
22+
then is_interactive=true
23+
else
24+
echo "Error: fzf is not installed for interactive mode"
25+
echo "Install from: https://github.com/junegunn/fzf#installation"
26+
exit 1
27+
fi
28+
break
29+
fi
30+
done
1731

1832
# show worktree list
1933
worktree_list() {
@@ -24,6 +38,7 @@ help_message() {
2438
echo -e "wt lets you switch between your git worktrees with speed.\n"
2539
echo "Usage:"
2640
echo -e "\twt <worktree-name>: search for worktree names and change to that directory."
41+
echo -e "\twt -i: interactively select a worktree using fzf."
2742
echo -e "\twt list: list out all the git worktrees."
2843
echo -e "\twt update: update to the latest release of worktree switcher."
2944
echo -e "\twt version: show the CLI version."
@@ -99,7 +114,15 @@ version)
99114
goto_main_worktree
100115
;;
101116
*)
102-
directory=$(git worktree list --porcelain | grep -E 'worktree ' | awk '/'"$arg"'/ {print; exit}' | cut -d ' ' -f2-)
117+
if [[ "$is_interactive" == true ]]; then
118+
directory=$(git worktree list --porcelain |
119+
awk '/worktree/ {wt=$2} /branch/ {sub("refs/heads/", "", $2); print wt " [" $2 "]"}' |
120+
fzf --query "" --height=10% --no-multi --exit-0 |
121+
awk '{print $1}')
122+
else
123+
directory=$(git worktree list --porcelain | grep -E 'worktree ' | awk '/'"$arg"'/ {print; exit}' | cut -d ' ' -f2-)
124+
fi
125+
103126
;;
104127
esac
105128

0 commit comments

Comments
 (0)