2022-10-03 16:46:49 +00:00
|
|
|
#!/usr/bin/env bash
|
2021-09-17 18:42:11 +00:00
|
|
|
|
2023-12-19 20:26:32 +00:00
|
|
|
# Credit to ThePrimeagen.
|
|
|
|
|
2023-12-14 08:58:05 +00:00
|
|
|
set -o nounset
|
|
|
|
set -o pipefail
|
|
|
|
|
2021-09-17 18:42:11 +00:00
|
|
|
if [[ $# -eq 1 ]]; then
|
|
|
|
selected=$1
|
|
|
|
else
|
2023-12-19 20:26:32 +00:00
|
|
|
# Get the session name from fuzzy-finding list of directories and generating a
|
|
|
|
# tmux-safe version.
|
2024-01-25 18:18:16 +00:00
|
|
|
|
|
|
|
# Find all project directories within ~/Code, ignoring ~/Code/tmp as those
|
|
|
|
# are only at a single level and I don't want sub-directories within those
|
|
|
|
# directories to be shown.
|
|
|
|
items+=$(find "${HOME}/Code" \
|
|
|
|
-maxdepth 3 \
|
|
|
|
-mindepth 3 \
|
|
|
|
-type d \
|
|
|
|
! -name "*-old" \
|
|
|
|
! -name "*.old" \
|
|
|
|
! -path "${HOME}/Code/tmp/*"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add the top-level directories within ~/Code/tmp.
|
2024-01-25 17:50:48 +00:00
|
|
|
items+=" "
|
2024-01-25 18:18:16 +00:00
|
|
|
items+=$(find "${HOME}/Code/tmp" \
|
|
|
|
-maxdepth 1 \
|
|
|
|
-mindepth 1 \
|
|
|
|
-type d \
|
|
|
|
! -name "*-old" \
|
|
|
|
! -name "*.old"
|
|
|
|
)
|
2023-12-19 20:26:32 +00:00
|
|
|
|
2024-01-25 18:18:16 +00:00
|
|
|
selected=$(echo "${items}" | tr ' ' "\n" | sort | fzf --reverse)
|
2021-09-17 18:42:11 +00:00
|
|
|
fi
|
|
|
|
|
2023-12-14 08:58:05 +00:00
|
|
|
if [[ -z "${selected}" ]]; then
|
2021-09-17 18:42:11 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2023-12-14 08:58:05 +00:00
|
|
|
session_name=$(basename "${selected}" | sed 's/\./-/g')
|
2023-12-19 10:53:34 +00:00
|
|
|
session_path="${selected}"
|
2023-12-19 15:23:08 +00:00
|
|
|
|
|
|
|
# Git worktrees.
|
2023-12-19 10:53:34 +00:00
|
|
|
if [[ -e "${selected}/main" ]]; then
|
|
|
|
session_path="${selected}/main"
|
|
|
|
fi
|
2021-09-17 21:35:47 +00:00
|
|
|
|
2023-12-19 10:53:34 +00:00
|
|
|
if tmux has-session -t "${session_name}" 2> /dev/null; then
|
|
|
|
tmux attach -t "${session_name}"
|
2021-09-17 21:35:47 +00:00
|
|
|
fi
|
|
|
|
|
2023-12-19 15:23:08 +00:00
|
|
|
# If a .tmux file exists, run it with the generated session name and path.
|
2023-12-19 10:53:34 +00:00
|
|
|
if [[ -e "${session_path}/.tmux" ]]; then
|
2023-12-19 15:23:08 +00:00
|
|
|
"${session_path}/.tmux" "${session_name}" "${session_path}"
|
|
|
|
exit
|
|
|
|
elif [[ -e "${session_path}/.ignored/.tmux" ]]; then
|
|
|
|
"${session_path}/.ignored/.tmux" "${session_name}" "${session_path}"
|
2023-12-14 08:58:05 +00:00
|
|
|
exit
|
2021-09-17 18:42:11 +00:00
|
|
|
fi
|
|
|
|
|
2023-12-19 15:23:08 +00:00
|
|
|
tmux new-session -d -s "${session_name}" -c "${session_path}"
|
2023-12-14 08:58:05 +00:00
|
|
|
|
2023-12-19 15:23:08 +00:00
|
|
|
tmux switch-client -t "${session_name}" || tmux attach -t "${session_name}"
|