this repo has no description
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# Accept input from argument or stdin
23if [ -n "$1" ]; then
24 input=$1
25elif [ ! -t 0 ]; then
26 read -r input
27else
28 echo "Usage: tmopen <path_or_url> [session_name]"
29 exit 1
30fi
31is_url=false
32
33# Check if the input is a URL
34if [[ $input == http* ]]; then
35 is_url=true
36fi
37
38if [ "$is_url" = true ]; then
39 # Handle URL (git repository)
40 TEMP_DIR=$(mktemp -d)
41 echo "Cloning repository to temporary directory: $TEMP_DIR"
42 git clone "$input" "$TEMP_DIR"
43 project_path=$TEMP_DIR
44 # For URLs, get the session name from the repo name in the URL
45 auto_session_name=$(get_repo_name_from_url "$input")
46else
47 # Handle local path
48 if [ ! -d "$input" ]; then
49 echo "Error: Directory does not exist: $input"
50 exit 1
51 fi
52 project_path=$(realpath "$input")
53 # For local paths, get the session name from the directory name
54 auto_session_name=$(get_session_name "$project_path")
55fi
56
57# Use the provided session name if available, otherwise use the auto-generated one
58if [ -n "$2" ]; then
59 session_name="$2"
60 echo "Using provided session name: $session_name"
61else
62 session_name="$auto_session_name"
63 echo "Using auto-generated session name: $session_name"
64fi
65
66# Check if the session already exists
67if tmux has-session -t "$session_name" 2>/dev/null; then
68 echo "Session '$session_name' already exists..."
69else
70 echo "Creating new session '$session_name' at $project_path"
71 tmux new-session -d -s "$session_name" -c "$project_path"
72fi
73
74# Connect to the new session
75if [ -n "$TMUX" ]; then
76 tmux switch-client -t "$session_name"
77else
78 tmux attach-session -t "$session_name"
79fi