Files
.dotfiles/.local/bin/project
2025-06-08 14:22:17 +10:00

72 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Default projects directory
PROJECTS_DIR="$HOME/projects"
# Function to print usage
print_usage() {
echo "Usage: $0 [-c] <project_name>"
echo "Options:"
echo " -c 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 ":c" opt; do
case $opt in
c)
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