···11+#!/usr/bin/env bash
22+33+# tmopen - Open a tmux session for a given project
44+# Usage: tmopen <path_or_url> [session_name]
55+66+set -e
77+88+# Function to get the session name from a path
99+get_session_name() {
1010+ local path=$1
1111+ basename "$path" | tr '.' '-'
1212+}
1313+1414+# Function to extract repo name from URL
1515+get_repo_name_from_url() {
1616+ local url=$1
1717+ # Extract the part after the last slash and before .git (if present)
1818+ basename=$(basename "$url")
1919+ echo "${basename%.git}" | tr '.' '-'
2020+}
2121+2222+# Check if a parameter was provided
2323+if [ -z "$1" ]; then
2424+ echo "Usage: tmopen <path_or_url> [session_name]"
2525+ exit 1
2626+fi
2727+2828+input=$1
2929+is_url=false
3030+3131+# Check if the input is a URL
3232+if [[ $input == http* ]]; then
3333+ is_url=true
3434+fi
3535+3636+if [ "$is_url" = true ]; then
3737+ # Handle URL (git repository)
3838+ TEMP_DIR=$(mktemp -d)
3939+ echo "Cloning repository to temporary directory: $TEMP_DIR"
4040+ git clone "$input" "$TEMP_DIR"
4141+ project_path=$TEMP_DIR
4242+ # For URLs, get the session name from the repo name in the URL
4343+ auto_session_name=$(get_repo_name_from_url "$input")
4444+else
4545+ # Handle local path
4646+ if [ ! -d "$input" ]; then
4747+ echo "Error: Directory does not exist: $input"
4848+ exit 1
4949+ fi
5050+ project_path=$(realpath "$input")
5151+ # For local paths, get the session name from the directory name
5252+ auto_session_name=$(get_session_name "$project_path")
5353+fi
5454+5555+# Use the provided session name if available, otherwise use the auto-generated one
5656+if [ -n "$2" ]; then
5757+ session_name="$2"
5858+ echo "Using provided session name: $session_name"
5959+else
6060+ session_name="$auto_session_name"
6161+ echo "Using auto-generated session name: $session_name"
6262+fi
6363+6464+# Check if the session already exists
6565+if tmux has-session -t "$session_name" 2>/dev/null; then
6666+ echo "Session '$session_name' already exists..."
6767+else
6868+ echo "Creating new session '$session_name' at $project_path"
6969+ tmux new-session -d -s "$session_name" -c "$project_path"
7070+fi
7171+7272+# Connect to the new session
7373+if [ -n "$TMUX" ]; then
7474+ tmux switch-client -t "$session_name"
7575+else
7676+ tmux attach-session -t "$session_name"
7777+fi