|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Convert templated variables to shell variables |
| 4 | +SESSIONS='${SESSIONS}' |
| 5 | + |
| 6 | +# Function to check if tmux is installed |
| 7 | +check_tmux() { |
| 8 | + if ! command -v tmux &> /dev/null; then |
| 9 | + echo "tmux is not installed. Please run the tmux setup script first." |
| 10 | + exit 1 |
| 11 | + fi |
| 12 | +} |
| 13 | + |
| 14 | +# Function to handle a single session |
| 15 | +handle_session() { |
| 16 | + local session_name="$1" |
| 17 | + |
| 18 | + # Check if the session exists |
| 19 | + if tmux has-session -t "$session_name" 2>/dev/null; then |
| 20 | + echo "Session '$session_name' exists, attaching to it..." |
| 21 | + tmux attach-session -t "$session_name" |
| 22 | + else |
| 23 | + echo "Session '$session_name' does not exist, creating it..." |
| 24 | + tmux new-session -d -s "$session_name" |
| 25 | + tmux attach-session -t "$session_name" |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +# Main function |
| 30 | +main() { |
| 31 | + # Check if tmux is installed |
| 32 | + check_tmux |
| 33 | + |
| 34 | + # If no sessions are specified, create or attach to a default session |
| 35 | + if [ "$SESSIONS" = "[]" ] || [ -z "$SESSIONS" ]; then |
| 36 | + echo "No sessions specified, using default session..." |
| 37 | + handle_session "default" |
| 38 | + exit 0 |
| 39 | + fi |
| 40 | + |
| 41 | + # Parse the JSON array by removing brackets and quotes, then split by commas |
| 42 | + # Remove the opening and closing brackets |
| 43 | + sessions_str=$${SESSIONS#[} |
| 44 | + sessions_str=$${sessions_str%]} |
| 45 | + |
| 46 | + # Remove quotes and split by commas |
| 47 | + sessions_str=$(echo "$sessions_str" | sed s/\"//g) |
| 48 | + IFS=',' read -ra SESSION_ARRAY <<< "$sessions_str" |
| 49 | + |
| 50 | + # Handle each session |
| 51 | + for session in "$${SESSION_ARRAY[@]}"; do |
| 52 | + # Trim whitespace |
| 53 | + session=$(echo "$session" | sed s/^[[:space:]]*//\;s/[[:space:]]*$//) |
| 54 | + handle_session "$session" |
| 55 | + done |
| 56 | +} |
| 57 | + |
| 58 | +# Run the main function |
| 59 | +main |
0 commit comments