Files
tmux/project.sh

101 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
projects_dir=$HOME/Projects
list_projects() {
local projects=() dir relative clone worktree projects_real
# git reports symlink-resolved paths, which may not match how we spell it.
projects_real=$(cd "$projects_dir" 2>/dev/null && pwd -P) ||
projects_real=$projects_dir
for dir in "$projects_dir"/*/*/; do
[ -d "$dir" ] || continue
relative="${dir#$projects_dir/}"
relative="${relative%/}"
if [[ "$relative" != *@* ]]; then
projects+=("$relative")
continue
fi
# `repo@branch` holds the worktrees of the `repo` clone, one level deeper
# per `/` in the branch name. Asking git beats a scan deep enough to reach
# them, which walks every working tree under $projects_dir. `worktree list`
# also names the clone itself, already added above; sort -u collapses it.
clone="$projects_dir/${relative%%@*}"
while IFS= read -r worktree; do
case $worktree in
"$projects_dir"/*) worktree=${worktree#"$projects_dir"/} ;;
"$projects_real"/*) worktree=${worktree#"$projects_real"/} ;;
*) continue ;;
esac
# Registered but deleted worktrees linger until `git worktree prune`.
[ -d "$projects_dir/$worktree" ] || continue
projects+=("$worktree")
done < <(git -C "$clone" worktree list --porcelain 2>/dev/null |
sed -n 's|^worktree ||p')
done
printf '%s\n' "${projects[@]}" | sort -u
}
# Second pass: runs inside the popup, on the list the first pass built.
if [[ "${1:-}" == --pick ]]; then
listfile=$2
trap 'rm -f "$listfile"' EXIT
project=$(
fzf --layout=reverse --info=hidden --border=rounded --cycle < "$listfile"
) || exit 0
[ -n "$project" ] || exit 0
# In a session for this same project the slug just repeats the session name,
# so use the branch instead. tmux rewrote `.` and `:` in the session name.
window_name=$project
session=$(tmux display -p '#S')
root=${project%%@*}
if [ "${root//[.:]/_}" = "$session" ]; then
branch=$(git -C "$projects_dir/$project" symbolic-ref --short HEAD 2>/dev/null ||
git -C "$projects_dir/$project" rev-parse --short HEAD 2>/dev/null || true)
window_name=${branch:-$project}
fi
tmux new-window -n "$window_name" -c "$projects_dir/$project"
~/.local/share/tmux/layouts/window-auto
exit
fi
# First pass: size the popup, then re-launch inside it. fzf has no width of its
# own — it fills the popup — so the popup width is what we scale.
listfile=$(mktemp)
list_projects > "$listfile"
longest=0
while IFS= read -r line; do
if (( ${#line} > longest )); then
longest=${#line}
fi
done < "$listfile"
# run-shell owns no client, so resolve the one that triggered us: without -c
# the popup gets no tty and fzf can't draw.
client=$(tmux display -p '#{client_name}')
cols=$(tmux display -p -c "$client" '#{client_width}')
min=60
max=$(( cols * 90 / 100 ))
# 6 = border (2) + pointer gutter (2) + scrollbar (1) + breathing room.
width=$(( longest + 6 ))
(( width < min )) && width=$min
(( width > max )) && width=$max
flags=(-c "$client" -w "$width" -h 10)
if ~/.config/tmux/check-version.sh ">= 3.3"; then
flags+=(-B)
fi
tmux display-popup "${flags[@]}" -E "'$0' --pick '$listfile'"