box-setup/src_files/.local/bin/tmux-sessionizer

35 lines
951 B
Bash
Executable File

#!/bin/zsh
switch_to() {
[[ -z $TMUX ]] && tmux attach-session -t $1 || tmux switch-client -t $1
}
tmux_session_exists() {
[[ -n $(pgrep tmux) ]] && tmux list-sessions | grep -q "^$1:"
}
hydrate() {
local tmux_hydrate_path="$HOME/.local/bin/.tmux-session-hydrate"
[[ -f $2/.tmux-session-hydrate ]] && tmux_hydrate_path="$2/.tmux-session-hydrate"
[[ -f $tmux_hydrate_path ]] && tmux send-keys -t $1 "source $tmux_hydrate_path" c-M
}
local search_dirs=(
$HOME
$HOMEBOX
$DEVDIR
$DEVDIR/git
$DEVDIR/git/*
)
local selected_path=''
[[ $# -eq 1 ]] && selected_path=$1 ||
selected_path=$(find $search_dirs -mindepth 1 -maxdepth 1 -type d | fzf)
[[ -z $selected_path ]] && exit 0
local selected_name=$(basename "$selected_path" | tr . _)
! (tmux_session_exists $selected_name) &&
tmux new-session -d -s $selected_name -c $selected_path &&
hydrate $selected_name $selected_path
switch_to $selected_name