dotfiles/bin/t

42 lines
864 B
Plaintext
Raw Normal View History

2022-10-03 16:46:49 +00:00
#!/usr/bin/env bash
# Credit to ThePrimeagen.
2023-12-14 08:58:05 +00:00
set -o nounset
set -o pipefail
if [[ $# -eq 1 ]]; then
selected=$1
else
# Get the session name from fuzzy-finding list of directories and generating a
# tmux-safe version.
2024-05-04 20:43:40 +00:00
items=$(find ~/Code/* ~/Code ~ ~/Documents /tmp \
-maxdepth 1 -mindepth 1 -type d \
! -name "*-old" \
! -name "*.old"
)
selected=$(echo "${items}" | fzf)
fi
2023-12-14 08:58:05 +00:00
if [[ -z "${selected}" ]]; then
exit 0
fi
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
2023-12-19 10:53:34 +00:00
if tmux has-session -t "${session_name}" 2> /dev/null; then
tmux attach -t "${session_name}"
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}"