#!/usr/bin/env bash # Default projects directory PROJECTS_DIR="$HOME/projects" # Function to print usage print_usage() { echo "Usage: $0 [-n] " echo "Options:" echo " -n Create a new project directory if it does not exist" } # Check if at least one argument is provided if [ $# -eq 0 ]; then print_usage exit 1 fi # Initialize variables CREATE_NEW=false PROJECT_NAME="" # Parse options while getopts ":n" opt; do case $opt in n) CREATE_NEW=true ;; \?) echo "Invalid option: -$OPTARG" print_usage exit 1 ;; esac done # Shift the options so $1 now refers to the project name argument shift $((OPTIND - 1)) # The argument after options is the project name PROJECT_NAME="$1" create_session() { tmux has-session -t $PROJECT_NAME &> /dev/null if [ $? != 0 ]; then cd "$PROJECTS_DIR/$PROJECT_NAME" tmux new -session -s $PROJECT_NAME -n nvim -d tmux new-window -t $PROJECT_NAME: -n terminal if [ -d "venv" ]; then tmux send-keys -t "$PROJECT_NAME:nvim" "source venv/bin/activate" C-m tmux send-keys -t "$PROJECT_NAME:terminal" "source venv/bin/activate" C-m fi tmux send-keys -t "$PROJECT_NAME:nvim" "nvim ." C-m tmux attach -t "$PROJECT_NAME:nvim" else tmux attach -t $PROJECT_NAME fi } # Check if the project directory exists if [ -d "$PROJECTS_DIR/$PROJECT_NAME" ]; then create_session else if [ "$CREATE_NEW" = true ]; then mkdir -p "$PROJECTS_DIR/$PROJECT_NAME" create_session else echo "Error: Project directory '$PROJECT_NAME' does not exist in $PROJECTS_DIR." exit 1 fi fi