this repo has no description
at main 2.0 kB view raw
1#!/usr/bin/env bash 2 3# tmopen - Open a tmux session for a given project 4# Usage: tmopen <path_or_url> [session_name] 5 6set -e 7 8# Function to get the session name from a path 9get_session_name() { 10 local path=$1 11 basename "$path" | tr '.' '-' 12} 13 14# Function to extract repo name from URL 15get_repo_name_from_url() { 16 local url=$1 17 # Extract the part after the last slash and before .git (if present) 18 basename=$(basename "$url") 19 echo "${basename%.git}" | tr '.' '-' 20} 21 22# Check if a parameter was provided 23if [ -z "$1" ]; then 24 echo "Usage: tmopen <path_or_url> [session_name]" 25 exit 1 26fi 27 28input=$1 29is_url=false 30 31# Check if the input is a URL 32if [[ $input == http* ]]; then 33 is_url=true 34fi 35 36if [ "$is_url" = true ]; then 37 # Handle URL (git repository) 38 TEMP_DIR=$(mktemp -d) 39 echo "Cloning repository to temporary directory: $TEMP_DIR" 40 git clone "$input" "$TEMP_DIR" 41 project_path=$TEMP_DIR 42 # For URLs, get the session name from the repo name in the URL 43 auto_session_name=$(get_repo_name_from_url "$input") 44else 45 # Handle local path 46 if [ ! -d "$input" ]; then 47 echo "Error: Directory does not exist: $input" 48 exit 1 49 fi 50 project_path=$(realpath "$input") 51 # For local paths, get the session name from the directory name 52 auto_session_name=$(get_session_name "$project_path") 53fi 54 55# Use the provided session name if available, otherwise use the auto-generated one 56if [ -n "$2" ]; then 57 session_name="$2" 58 echo "Using provided session name: $session_name" 59else 60 session_name="$auto_session_name" 61 echo "Using auto-generated session name: $session_name" 62fi 63 64# Check if the session already exists 65if tmux has-session -t "$session_name" 2>/dev/null; then 66 echo "Session '$session_name' already exists..." 67else 68 echo "Creating new session '$session_name' at $project_path" 69 tmux new-session -d -s "$session_name" -c "$project_path" 70fi 71 72# Connect to the new session 73if [ -n "$TMUX" ]; then 74 tmux switch-client -t "$session_name" 75else 76 tmux attach-session -t "$session_name" 77fi