99 lines
2.8 KiB
Bash
99 lines
2.8 KiB
Bash
#!/usr/bin/env bash
|
|
#===============================================================================
|
|
#
|
|
# created: 2015-09-09
|
|
# updated: 2026-07-21
|
|
# usage: tm [session_name]
|
|
# desc: Interactive and declarative tmux session manager.
|
|
# notes:
|
|
# author: chawley
|
|
#
|
|
#===============================================================================
|
|
|
|
set -euo pipefail
|
|
|
|
readonly TMUX_CONF="${HOME}/.tmux.conf"
|
|
|
|
# Ensure we are not nesting tmux sessions
|
|
if [[ -n "${TMUX:-}" ]]; then
|
|
echo "Error: Already inside a tmux session. Nesting is disabled." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Define session layouts as data maps: "window_index:window_name:command"
|
|
# Index 1 is reserved for the initial session window (renamed inline).
|
|
declare -A SESSIONS
|
|
|
|
SESSIONS[mine]="
|
|
1:local(phansible@aamon):
|
|
2:phansible@overlook:exec ssh phansible@overlook.tailc38dc9.ts.net
|
|
3:weechat:exec ssh overlook-ts && docker start weechat && docker attach weechat
|
|
4:chawley@overlook:exec ssh overlook-ts
|
|
5:chawley@shawshank:exec ssh shawshank-ts
|
|
6:root@derry:exec ssh derry-ts
|
|
"
|
|
|
|
list_active_sessions() {
|
|
echo "----------------------------------"
|
|
echo "Active Tmux Sessions"
|
|
echo "----------------------------------"
|
|
if tmux has-session 2>/dev/null; then
|
|
tmux list-sessions
|
|
else
|
|
echo "No active tmux sessions."
|
|
fi
|
|
}
|
|
|
|
# Resolve target session (argument or fuzzy picker via fzf if available)
|
|
session="${1:-}"
|
|
|
|
if [[ -z "${session}" ]]; then
|
|
if command -v fzf &>/dev/null; then
|
|
session=$(printf '\%s\n' "${!SESSIONS[@]}" | fzf --prompt="Select Tmux Session > " --height=10 --reverse || true)
|
|
fi
|
|
fi
|
|
|
|
if [[ -z "${session}" ]]; then
|
|
echo "Usage: $(basename "$0") <session_name>"
|
|
echo ""
|
|
list_active_sessions
|
|
exit 1
|
|
fi
|
|
|
|
if [[ -z "${SESSIONS[$session]:-}" ]]; then
|
|
echo "Error: Unknown session layout '${session}'" >&2
|
|
echo ""
|
|
list_active_sessions
|
|
exit 1
|
|
fi
|
|
|
|
# Attach if session exists; otherwise construct and attach
|
|
if tmux has-session -t "=${session}" 2>/dev/null; then
|
|
echo "Attaching to existing session: ${session}"
|
|
exec tmux -f "${TMUX_CONF}" attach-session -t "=${session}"
|
|
fi
|
|
|
|
echo "Building session: ${session}"
|
|
|
|
# Process layout line by line
|
|
first_window=true
|
|
while IFS=':' read -r idx name cmd; do
|
|
[[ -z "${idx}" ]] && continue
|
|
|
|
if [[ "${first_window}" == true ]]; then
|
|
tmux -f "${TMUX_CONF}" new-session -d -s "${session}" -n "${name}"
|
|
if [[ -n "${cmd}" ]]; then
|
|
tmux send-keys -t "=${session}:1" "${cmd}" C-m
|
|
fi
|
|
first_window=false
|
|
else
|
|
if [[ -n "${cmd}" ]]; then
|
|
tmux -f "${TMUX_CONF}" new-window -t "=${session}:${idx}" -n "${name}" "${cmd}"
|
|
else
|
|
tmux -f "${TMUX_CONF}" new-window -t "=${session}:${idx}" -n "${name}"
|
|
fi
|
|
fi
|
|
done <<< "${SESSIONS[$session]}"
|
|
|
|
exec tmux -f "${TMUX_CONF}" attach-session -t "=${session}"
|