2023-11-11 13:13:02 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# Quit on escape
|
|
|
|
set -o emacs
|
|
|
|
bind '"\e": "\C-W\c-d"'
|
|
|
|
|
2023-11-17 08:47:06 +00:00
|
|
|
OPEN_MODE="new_pane"
|
|
|
|
|
|
|
|
while getopts ":l:v" option; do
|
|
|
|
case $option in
|
|
|
|
l)
|
|
|
|
OPEN_MODE="local"
|
|
|
|
;;
|
|
|
|
v)
|
|
|
|
VERBOSE=true
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
debug () {
|
|
|
|
if [ "$VERBOSE" = true ]; then
|
|
|
|
echo $1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
open_local () {
|
|
|
|
tmux send-keys "cd $PROJECT_PATH" Enter
|
|
|
|
|
|
|
|
# Close tmux popup if any
|
|
|
|
tmux display-popup -C
|
|
|
|
}
|
|
|
|
|
|
|
|
open_new_pane () {
|
2023-11-12 23:14:45 +00:00
|
|
|
tmux has-session -t $SESSION_NAME 2>/dev/null
|
|
|
|
if [ $? != 0 ]; then
|
2023-11-17 08:47:06 +00:00
|
|
|
debug "Session $SESSION_NAME missing, creating it."
|
2023-11-13 23:14:35 +00:00
|
|
|
tmux new-session -s $SESSION_NAME -d -n $PROJECT_NAME -c $PROJECT_PATH
|
2023-11-12 23:14:45 +00:00
|
|
|
fi
|
|
|
|
|
2023-11-13 23:14:35 +00:00
|
|
|
tmux has-session -t $SESSION_NAME:$PROJECT_NAME 2>/dev/null
|
2023-11-12 23:14:45 +00:00
|
|
|
if [ $? != 0 ]; then
|
2023-11-17 08:47:06 +00:00
|
|
|
debug "Window $PROJECT_NAME in $SESSION_NAME not found, creating it."
|
|
|
|
tmux new-window -t $SESSION_NAME -n $PROJECT_NAME -d -c $PROJECT_PATH
|
2023-11-12 23:14:45 +00:00
|
|
|
fi
|
|
|
|
|
2023-11-13 23:14:35 +00:00
|
|
|
tmux switch-client -t $SESSION_NAME:$PROJECT_NAME
|
2023-11-12 23:14:45 +00:00
|
|
|
|
|
|
|
# Close tmux popup if any
|
|
|
|
tmux display-popup -C
|
|
|
|
}
|
|
|
|
|
2023-11-11 13:13:02 +00:00
|
|
|
declare -A CUSTOM_SESSIONS
|
|
|
|
CUSTOM_SESSIONS[Projects]=Halia
|
|
|
|
|
2023-11-13 23:14:35 +00:00
|
|
|
PROJECT_LIST=$(find ~/* -name .git -type d -prune 2>/dev/null)
|
2023-11-11 13:13:02 +00:00
|
|
|
|
2023-11-13 23:14:35 +00:00
|
|
|
SELECTED=$(echo $PROJECT_LIST | sed 's/\/.git/\n/g' | sed "s/$(echo $HOME | sed 's/\//\\\//g')//" | cut -d'/' -f2- | fzf)
|
|
|
|
|
2023-11-17 08:47:43 +00:00
|
|
|
debug "Selected $SELECTED"
|
|
|
|
|
|
|
|
PROJECT_PATH=$(echo $PROJECT_LIST | sed 's/\/.git/\n/g' | grep "${SELECTED}$")
|
2023-11-13 23:14:35 +00:00
|
|
|
PROJECT_ROOT=$(echo $SELECTED | cut -d '/' -f1)
|
|
|
|
PROJECT_NAME=$(echo $SELECTED | rev | cut -d '/' -f1 | rev)
|
|
|
|
|
|
|
|
SESSION_NAME=$([ -z "${CUSTOM_SESSIONS[$PROJECT_ROOT]}" ] && echo "$PROJECT_ROOT" || echo "${CUSTOM_SESSIONS[$PROJECT_ROOT]}")
|
2023-11-11 13:13:02 +00:00
|
|
|
|
2023-11-17 08:47:06 +00:00
|
|
|
debug "Project path is $PROJECT_PATH"
|
2023-11-11 13:13:02 +00:00
|
|
|
|
2023-11-17 08:47:06 +00:00
|
|
|
case $OPEN_MODE in
|
|
|
|
"new_pane")
|
|
|
|
open_new_pane
|
|
|
|
;;
|
|
|
|
"local")
|
|
|
|
open_local
|
|
|
|
;;
|
|
|
|
esac
|