Files
dotfiles/scripts/scripts/tmux-sessionator
2023-10-04 23:11:05 -04:00

88 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env zsh
# Adapted from: https://github.com/ThePrimeagen/.dotfiles/blob/master/bin/.local/scripts/tmux-sessionizer
DEBUG=
#################### Options ####################
declare -a paths=()
declare chooseOpt=
zparseopts -D -- \
{c,-choose}=chooseOpt
#################### Helpers ####################
function debug_print {
if [ -n "$DEBUG" ]; then
echo "DEBUG: $1"
fi
}
function path_prepend() {
declare arg
for arg in "$@"; do
debug_print "arg: $arg"
# Check that arg is a directory but not a symlink
# Bc on some of my machines ~/projects is a symlink to $REPOS
test -d $arg && ! test -L $arg || continue
paths=${paths//$arg}
paths=${paths/#$arg}
paths=${paths/%$arg}
paths=($arg $paths)
done
}
function setup_fuzzy_find_paths() {
path_prepend "$HOME" \
"$HOME/projects" \
"$REPOS/local" \
$(find "$REPOS/github.com" -mindepth 1 -maxdepth 1 -type d -print 2> /dev/null)
}
#################### MAIN ####################
declare choose="${chooseOpt[-1]}"
if [[ -n $choose ]]; then
debug_print "Choose from existing."
selected=$(
tmux list-sessions -F \#S | gum filter \
--placeholder "Pick a session..."
)
elif [[ "$#" -eq 1 ]]; then
debug_print "Using existing session: $1"
selected=$1
else
setup_fuzzy_find_paths
debug_print "fuzzy find paths: $paths"
if [ -n "$DEBUG" ]; then
debug_print "Exiting because in debug mode."
exit 0
fi
selected=$(find "${(@)paths}" -mindepth 1 -maxdepth 1 -type d | fzf)
fi
if [[ -z $selected ]]; then
exit 0
fi
selected_name=$(basename "$selected" | tr . _)
tmux_running=$(pgrep tmux)
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
tmux new-session -s "$selected_name" -c "$selected"
exit 0
fi
# Create a session if it doesn't exist.
if ! tmux has-session -t "$selected_name" 2> /dev/null; then
tmux new-session -ds "$selected_name" -c "$selected"
fi
tmux switch-client -t "$selected_name"