dotfiles/.bin/tmux-projectinator

49 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# Quit on escape
set -o emacs
bind '"\e": "\C-W\c-d"'
move_pane () {
tmux has-session -t $SESSION_NAME 2>/dev/null
if [ $? != 0 ]; then
tmux new-session -s $SESSION_NAME -d -n $PROJECT_NAME -c $PROJECT_PATH
fi
tmux has-session -t $SESSION_NAME:$PROJECT_NAME 2>/dev/null
if [ $? != 0 ]; then
tmux new-window -t $SESSION_NAME: -n $PROJECT_NAME -d -c $PROJECT_PATH
fi
tmux switch-client -t $SESSION_NAME:$PROJECT_NAME
# Close tmux popup if any
tmux display-popup -C
}
declare -A CUSTOM_SESSIONS
CUSTOM_SESSIONS[Projects]=Halia
PROJECT_LIST=$(find ~/* -name .git -type d -prune 2>/dev/null)
SELECTED=$(echo $PROJECT_LIST | sed 's/\/.git/\n/g' | sed "s/$(echo $HOME | sed 's/\//\\\//g')//" | cut -d'/' -f2- | fzf)
PROJECT_PATH=$(echo $PROJECT_LIST | sed 's/\/.git/\n/g' | grep $SELECTED)
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]}")
while getopts ":l" option; do
case $option in
l)
tmux send-keys "cd $PROJECT_PATH" Enter
# Close tmux popup if any
tmux display-popup -C
;;
esac
done
if [ $OPTIND -eq 1 ]; then move_pane "$SELECTED"; fi