Make probject.sh comments less
This commit is contained in:
45
project.sh
45
project.sh
@@ -4,12 +4,10 @@ set -e
|
|||||||
|
|
||||||
projects_dir=$HOME/Projects
|
projects_dir=$HOME/Projects
|
||||||
|
|
||||||
# Print the sorted, de-duplicated list of selectable projects.
|
|
||||||
list_projects() {
|
list_projects() {
|
||||||
local projects=() dir relative clone worktree projects_real
|
local projects=() dir relative clone worktree projects_real
|
||||||
|
|
||||||
# git reports symlink-resolved paths, which need not spell $projects_dir the
|
# git reports symlink-resolved paths, which may not match how we spell it.
|
||||||
# way we do, so keep the resolved form around to match them against too.
|
|
||||||
projects_real=$(cd "$projects_dir" 2>/dev/null && pwd -P) ||
|
projects_real=$(cd "$projects_dir" 2>/dev/null && pwd -P) ||
|
||||||
projects_real=$projects_dir
|
projects_real=$projects_dir
|
||||||
|
|
||||||
@@ -18,20 +16,15 @@ list_projects() {
|
|||||||
relative="${dir#$projects_dir/}"
|
relative="${dir#$projects_dir/}"
|
||||||
relative="${relative%/}"
|
relative="${relative%/}"
|
||||||
|
|
||||||
# All depth-2 directories without @ are included unconditionally
|
|
||||||
if [[ "$relative" != *@* ]]; then
|
if [[ "$relative" != *@* ]]; then
|
||||||
projects+=("$relative")
|
projects+=("$relative")
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# A `repo@branch` directory is the parent of the worktrees hanging off the
|
# `repo@branch` holds the worktrees of the `repo` clone, one level deeper
|
||||||
# `repo` clone, nested one level deeper per `/` in the branch name. Ask
|
# per `/` in the branch name. Asking git beats a scan deep enough to reach
|
||||||
# that clone which worktrees it has instead of scanning: git already knows,
|
# them, which walks every working tree under $projects_dir. `worktree list`
|
||||||
# whereas a scan deep enough to reach them has to walk every working tree
|
# also names the clone itself, already added above; sort -u collapses it.
|
||||||
# under $projects_dir to find a handful of entries. Submodules aren't
|
|
||||||
# worktrees, so they never turn up and need no filtering out. `git worktree
|
|
||||||
# list` also names the clone itself, which the branch above already added —
|
|
||||||
# the final sort -u collapses the pair.
|
|
||||||
clone="$projects_dir/${relative%%@*}"
|
clone="$projects_dir/${relative%%@*}"
|
||||||
while IFS= read -r worktree; do
|
while IFS= read -r worktree; do
|
||||||
case $worktree in
|
case $worktree in
|
||||||
@@ -49,25 +42,18 @@ list_projects() {
|
|||||||
printf '%s\n' "${projects[@]}" | sort -u
|
printf '%s\n' "${projects[@]}" | sort -u
|
||||||
}
|
}
|
||||||
|
|
||||||
# Second pass: runs inside the popup. Read the list the first pass already
|
# Second pass: runs inside the popup, on the list the first pass built.
|
||||||
# built (so the directory scan only happens once) and open the selection.
|
|
||||||
if [[ "${1:-}" == --pick ]]; then
|
if [[ "${1:-}" == --pick ]]; then
|
||||||
listfile=$2
|
listfile=$2
|
||||||
trap 'rm -f "$listfile"' EXIT
|
trap 'rm -f "$listfile"' EXIT
|
||||||
|
|
||||||
# Cancelling fzf (Esc/^C) exits non-zero; treat it as "no selection" and
|
|
||||||
# leave quietly, rather than letting the status propagate out of the popup
|
|
||||||
# and print '...project.sh returned 130' in the parent pane.
|
|
||||||
project=$(
|
project=$(
|
||||||
fzf --layout=reverse --info=hidden --border=rounded --cycle < "$listfile"
|
fzf --layout=reverse --info=hidden --border=rounded --cycle < "$listfile"
|
||||||
) || exit 0
|
) || exit 0
|
||||||
[ -n "$project" ] || exit 0
|
[ -n "$project" ] || exit 0
|
||||||
|
|
||||||
# Inside a session for this same project, e.g. one created by `session -p`,
|
# In a session for this same project the slug just repeats the session name,
|
||||||
# the slug only repeats the session name, so name the window after the branch
|
# so use the branch instead. tmux rewrote `.` and `:` in the session name.
|
||||||
# checked out there instead — a worktree's whole identity. Everything up to
|
|
||||||
# the `@` is the clone the worktrees hang off, and tmux rewrote `.` and `:` in
|
|
||||||
# the session name, so match both before comparing.
|
|
||||||
window_name=$project
|
window_name=$project
|
||||||
session=$(tmux display -p '#S')
|
session=$(tmux display -p '#S')
|
||||||
root=${project%%@*}
|
root=${project%%@*}
|
||||||
@@ -82,9 +68,8 @@ if [[ "${1:-}" == --pick ]]; then
|
|||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# First pass: scan once, size a popup to fit the longest project name, then
|
# First pass: size the popup, then re-launch inside it. fzf has no width of its
|
||||||
# re-launch ourselves inside it. fzf has no width of its own — it fills the
|
# own — it fills the popup — so the popup width is what we scale.
|
||||||
# popup — so the popup width is what we scale.
|
|
||||||
listfile=$(mktemp)
|
listfile=$(mktemp)
|
||||||
list_projects > "$listfile"
|
list_projects > "$listfile"
|
||||||
|
|
||||||
@@ -95,23 +80,19 @@ while IFS= read -r line; do
|
|||||||
fi
|
fi
|
||||||
done < "$listfile"
|
done < "$listfile"
|
||||||
|
|
||||||
# We're launched from run-shell, which owns no client, so resolve the client
|
# run-shell owns no client, so resolve the one that triggered us: without -c
|
||||||
# that triggered us. It both anchors the popup (-c, without which the popup
|
# the popup gets no tty and fzf can't draw.
|
||||||
# gets no tty and fzf can't draw) and gives the terminal width for the cap.
|
|
||||||
client=$(tmux display -p '#{client_name}')
|
client=$(tmux display -p '#{client_name}')
|
||||||
cols=$(tmux display -p -c "$client" '#{client_width}')
|
cols=$(tmux display -p -c "$client" '#{client_width}')
|
||||||
|
|
||||||
# Clamp between the original fixed width and 90% of the terminal. In between,
|
|
||||||
# pad for fzf's chrome: rounded border (2) + pointer gutter (2) + scrollbar (1)
|
|
||||||
# + a column of breathing room.
|
|
||||||
min=60
|
min=60
|
||||||
max=$(( cols * 90 / 100 ))
|
max=$(( cols * 90 / 100 ))
|
||||||
|
# 6 = border (2) + pointer gutter (2) + scrollbar (1) + breathing room.
|
||||||
width=$(( longest + 6 ))
|
width=$(( longest + 6 ))
|
||||||
(( width < min )) && width=$min
|
(( width < min )) && width=$min
|
||||||
(( width > max )) && width=$max
|
(( width > max )) && width=$max
|
||||||
|
|
||||||
flags=(-c "$client" -w "$width" -h 10)
|
flags=(-c "$client" -w "$width" -h 10)
|
||||||
# Match the borderless popup the keybinding used on tmux >= 3.3.
|
|
||||||
if ~/.config/tmux/check-version.sh ">= 3.3"; then
|
if ~/.config/tmux/check-version.sh ">= 3.3"; then
|
||||||
flags+=(-B)
|
flags+=(-B)
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user