kickstart.nvim/tmux-s

68 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
TS_SEARCH_PATHS=("$HOME/repo:1" "$HOME/repo/astro:2" "$HOME/repo/tripletex-frontend/scripts/migrate-repo/:2")
TS_MAX_DEPTH=2
find_dirs() {
# list TMUX sessions
if [[ -n "${TMUX}" ]]; then
current_session=$(tmux display-message -p '#S')
tmux list-sessions -F "[TMUX] #{session_name}" 2>/dev/null | grep -vFx "[TMUX] $current_session"
else
tmux list-sessions -F "[TMUX] #{session_name}" 2>/dev/null
fi
# if the path ends with :number, it will search for directories with a max depth of number ;)
# if there is no number, it will search for directories with a max depth defined by TS_MAX_DEPTH or 1 if not set
for entry in "${TS_SEARCH_PATHS[@]}"; do
# Check if entry as :number as suffix then adapt the maxdepth parameter
if [[ "$entry" =~ ^([^:]+):([0-9]+)$ ]]; then
path="${BASH_REMATCH[1]}"
depth="${BASH_REMATCH[2]}"
else
path="$entry"
fi
depth="${depth:-${TS_MAX_DEPTH:-1}}"
[[ -d "$path" ]] && find "$path" -mindepth 1 -maxdepth "$depth" \( -path '*/.git' -o -path '*/node_modules' \) -prune -o -type d -print | sed "s|^$HOME/||"
done
}
has_session() {
tmux list-sessions | grep -q "^$1:"
}
switch_to() {
if [[ -z $TMUX ]]; then
log "attaching to session $1"
tmux attach-session -t "$1"
else
log "switching to session $1"
tmux switch-client -t "$1"
fi
}
selected=$(find_dirs | fzf)
if [[ "$selected" =~ ^\[TMUX\]\ (.+)$ ]]; then
selected="${BASH_REMATCH[1]}"
fi
selected_name=$(basename "$selected")
if ! has_session "$selected_name"; then
cd "$HOME/$selected"
tmux new-session -ds "$selected_name" -c "$HOME/$selected"
tmux rename-window -t "$selected_name":1 "Code"
tmux send-keys -t "$selected_name":1 "nvim" C-m
tmux new-window -t "$selected_name":2 -n "Server"
tmux new-window -t "$selected_name":3 -n "Claude"
tmux new-window -t "$selected_name":4 -n "Whatever"
fi
switch_to "$selected_name"