dotfiles/bin/t

45 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env bash
set -o nounset
set -o pipefail
if [[ $# -eq 1 ]]; then
selected=$1
else
selected=$(zoxide query -l | grep -v /tmp | grep -v "^${HOME}/\." | grep -v "^${HOME}/Code$" | grep -v "/main$" | fzf --reverse)
fi
if [[ -z "${selected}" ]]; then
exit 0
fi
session_name=$(basename "${selected}" | sed 's/\./-/g')
tmux_running=$(pgrep tmux)
session_path="${selected}"
if [[ -e "${selected}/main" ]]; then
session_path="${selected}/main"
fi
if tmux has-session -t "${session_name}" 2> /dev/null; then
tmux attach -t "${session_name}"
fi
# If a .tmux file exists within the selected directory, run it with the
# generated session name.
if [[ -e "${session_path}/.tmux" ]]; then
"${session_path}/.tmux" "${session_name}" "${selected}/main"
exit
fi
if [[ -z "${TMUX:-}" ]] && [[ -z "${tmux_running}" ]]; then
tmux new-session -s "${session_name}" -c "${session_path}"
exit 0
fi
if ! tmux has-session -t "${session_name}" 2> /dev/null; then
tmux new-session -ds "${session_name}" -c "${session_path}"
fi
tmux switch-client -t "${session_name}"