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

38 lines
1.3 KiB
Bash
Executable File

#!/bin/zsh
switch_to() {
[[ -z $TMUX ]] && tmux attach-session -t $1 || tmux switch-client -t $1
}
hydrate() {
local tmux_hydrate_path="$HOME/.local/bin/.tmux-session-hydrate-default"
[[ -f $2/.tmux-session-hydrate ]] && tmux_hydrate_path="$2/.tmux-session-hydrate"
# TODO: add special case: [[ "$1" = "hub" ]] && tmux_hydrate_path="$HOME/.local/bin/.tmux-session-hydrate-hub"
[[ -f $tmux_hydrate_path ]] && tmux send-keys -t $1 "source $tmux_hydrate_path" c-M
}
local name_regex="^\([-_A-Za-z0-9]*\):.*$"
local existing_sessions=$([[ -n $(pgrep tmux) ]] && tmux list-sessions | sed "s/$name_regex/\1/" || echo '')
local search_dirs=(
$DIR_HOME_BOX
$DIR_DEV
$DIR_DEV/git/*
)
local target_name=''
local target_path=''
[[ $# -eq 1 ]] && target_path=$1 ||
target_path=$(find $search_dirs -mindepth 1 -maxdepth 1 -type d | fzf)
if [[ $target_path = "existing" ]]; then target_name=$(echo $existing_sessions | fzf);
elif [[ $target_path = "hub" ]]; then target_name="hub" && target_path="$HOME";
elif [[ -n $target_path ]]; then target_name=$(basename "$target_path" | tr . _);
fi
[[ -z $target_name ]] && exit 0
! (echo $existing_sessions | grep -q "$target_name") &&
tmux new-session -d -s $target_name -c $target_path &&
hydrate $target_name $target_path
switch_to $target_name