109 lines
3.6 KiB
Bash
Executable File
109 lines
3.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#===============================================================================
|
|
#
|
|
# CREATED : 2026-02-21
|
|
# UPDATED :
|
|
# USAGE :
|
|
# DESC : Hack tmux session launcher. Reads a YAML file to create a tmux # session with multiple windows and commands.
|
|
# NOTES :
|
|
# AUTHOR : chawley
|
|
#
|
|
#===============================================================================
|
|
|
|
# Path to the YAML configuration file
|
|
TMUX_CONFIG_FILE="${HOME}/.config/tmux-menu/sessions.yaml"
|
|
|
|
# Check for yq (YAML processor)
|
|
if ! command -v yq &> /dev/null; then
|
|
echo "Error: 'yq' command not found."
|
|
echo "Please install yq (https://github.com/mikefarah/yq) to use this script."
|
|
echo "Example: 'sudo snap install yq' or 'brew install yq'"
|
|
exit 1
|
|
fi
|
|
# check for command line argument
|
|
# Tips from:
|
|
# http://stackoverflow.com/questions/7832080/test-if-a-variable-is-set-in-bash-when-using-set-o-nounset
|
|
if [ -z "${1:-}" ]; then
|
|
session="undefined"
|
|
else
|
|
session=$1
|
|
fi
|
|
|
|
echo ""
|
|
echo ""
|
|
echo "##################################"
|
|
echo "TMUX Hack Session Launcher"
|
|
echo "##################################"
|
|
echo "Launching Session: $session"
|
|
|
|
# NO TMUX NESTING ALLOWED
|
|
if [ ! -z $TMUX ]; then
|
|
echo "You are in a session already. NO NESTING ALLOWED"
|
|
echo "-------------------------------------------------"
|
|
exit 1
|
|
fi
|
|
|
|
|
|
# if session is active, switch to it.
|
|
if tmux has-session -t "${session}" 2>/dev/null; then
|
|
echo "Session '${session}' already active. Attaching..."
|
|
tmux attach-session -t "${session}"
|
|
exit 0
|
|
fi
|
|
|
|
# Check if the config file exists
|
|
if [ ! -f "${TMUX_CONFIG_FILE}" ]; then
|
|
echo "Error: tmux session configuration file not found at '${TMUX_CONFIG_FILE}'."
|
|
echo "Please create it with your session definitions."
|
|
exit 1
|
|
fi
|
|
|
|
# Check if the requested session exists in the YAML file
|
|
if ! yq e ".sessions.${session}" "${TMUX_CONFIG_FILE}" &> /dev/null; then
|
|
echo "### ERROR No Such Session ###"
|
|
echo "Usage: $(basename "$0") <session_name>"
|
|
echo "Available Sessions from '${TMUX_CONFIG_FILE}':"
|
|
echo "---------------------------------------"
|
|
yq e '.sessions | keys | .[]' "${TMUX_CONFIG_FILE}" 2>/dev/null || echo "No sessions defined."
|
|
echo ""
|
|
echo "Active tmux Sessions:"
|
|
echo "---------------------"
|
|
tmux list-sessions 2>/dev/null || echo "No active sessions."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Creating new session '${session}' from '${TMUX_CONFIG_FILE}'..."
|
|
|
|
# Get the number of windows for the session
|
|
num_windows=$(yq e ".sessions.${session}.windows | length" "${TMUX_CONFIG_FILE}")
|
|
|
|
if [[ "${num_windows}" -eq 0 ]]; then
|
|
echo "Error: Session '${session}' has no windows defined in '${TMUX_CONFIG_FILE}'."
|
|
exit 1
|
|
fi
|
|
|
|
# Create the first window (which also creates the session)
|
|
first_window_name=$(yq e ".sessions.${session}.windows[0].name" "${TMUX_CONFIG_FILE}")
|
|
first_window_command=$(yq e ".sessions.${session}.windows[0].command // \"\"" "${TMUX_CONFIG_FILE}")
|
|
|
|
if [[ -z "${first_window_command}" ]]; then
|
|
tmux new-session -d -s "${session}" -n "${first_window_name}"
|
|
else
|
|
tmux new-session -d -s "${session}" -n "${first_window_name}" "${first_window_command}"
|
|
fi
|
|
|
|
# Create subsequent windows
|
|
for ((i=1; i<num_windows; i++)); do
|
|
window_name=$(yq e ".sessions.${session}.windows[${i}].name" "${TMUX_CONFIG_FILE}")
|
|
window_command=$(yq e ".sessions.${session}.windows[${i}].command // \"\"" "${TMUX_CONFIG_FILE}")
|
|
|
|
if [[ -z "${window_command}" ]]; then
|
|
tmux new-window -t "${session}:${i}" -n "${window_name}"
|
|
else
|
|
tmux new-window -t "${session}:${i}" -n "${window_name}" "${window_command}"
|
|
fi
|
|
done
|
|
|
|
# Attach to the newly created session
|
|
tmux attach-session -t "${session}"
|