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

39 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

#!/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=(
$HOMEBOX
$DEVDIR
2025-01-20 16:59:10 -06:00
$DEVDIR/git/*
)
local selected_path=''
[[ $# -eq 1 ]] && selected_path=$1 ||
selected_path=$(
find $search_dirs -mindepth 1 -maxdepth 1 -type d |
(echo 'hub' && cat) |
fzf
)
[[ -z $selected_path ]] && exit 0
local selected_name=''
[[ "$selected_path" = "hub" ]] && selected_name="hub" && selected_path=$HOME ||
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