Compare commits
4 Commits
0ede957833
...
d647d36f10
Author | SHA1 | Date | |
---|---|---|---|
d647d36f10 | |||
0684b34a09 | |||
72c5886271 | |||
be5f66f574 |
@ -5,61 +5,73 @@ set -o emacs
|
||||
bind '"\e": "\C-W\c-d"'
|
||||
|
||||
OPEN_MODE="new_pane"
|
||||
CACHE_FILE=~/.cache/project_list
|
||||
|
||||
# Close tmux popup if any and quit
|
||||
exit_script () {
|
||||
tmux display-popup -C
|
||||
|
||||
exit $1
|
||||
}
|
||||
|
||||
debug () {
|
||||
if [ "$VERBOSE" = true ]; then
|
||||
echo $1
|
||||
fi
|
||||
if [ "$VERBOSE" = true ]; then
|
||||
echo $1
|
||||
fi
|
||||
}
|
||||
|
||||
spinner() {
|
||||
local s="⣾⣽⣻⢿⡿⣟⣯⣷"
|
||||
local -i i=0
|
||||
while :; do
|
||||
printf "%s\\r" "${s:$((i++%4)):1}" >&2
|
||||
printf "%s\\r" "${s:$((i++%8)):1}" >&2
|
||||
sleep .1
|
||||
done
|
||||
|
||||
}
|
||||
|
||||
open_local () {
|
||||
# To avoid using pre-entered string in current buffer
|
||||
tmux send-keys C-c
|
||||
tmux send-keys "cd $PROJECT_PATH" Enter
|
||||
# To avoid using pre-entered string in current buffer
|
||||
tmux send-keys C-c
|
||||
tmux send-keys "cd $PROJECT_PATH" Enter
|
||||
|
||||
# Close tmux popup if any
|
||||
tmux display-popup -C
|
||||
exit_script 0
|
||||
}
|
||||
|
||||
open_new_pane () {
|
||||
tmux has-session -t $SESSION_NAME 2>/dev/null
|
||||
if [ $? != 0 ]; then
|
||||
debug "Session $SESSION_NAME missing, creating it."
|
||||
tmux new-session -s $SESSION_NAME -d -n $PROJECT_NAME -c $PROJECT_PATH
|
||||
fi
|
||||
tmux has-session -t $SESSION_NAME 2>/dev/null
|
||||
if [ $? != 0 ]; then
|
||||
debug "Session $SESSION_NAME missing, creating it."
|
||||
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
|
||||
debug "Window $PROJECT_NAME in $SESSION_NAME not found, creating it."
|
||||
tmux new-window -t $SESSION_NAME -n $PROJECT_NAME -d -c $PROJECT_PATH
|
||||
fi
|
||||
tmux has-session -t $SESSION_NAME:$PROJECT_NAME 2>/dev/null
|
||||
if [ $? != 0 ]; then
|
||||
debug "Window $PROJECT_NAME in $SESSION_NAME not found, creating it."
|
||||
tmux new-window -t $SESSION_NAME -n $PROJECT_NAME -d -c $PROJECT_PATH
|
||||
fi
|
||||
|
||||
tmux switch-client -t $SESSION_NAME:$PROJECT_NAME
|
||||
tmux switch-client -t $SESSION_NAME:$PROJECT_NAME
|
||||
|
||||
# Close tmux popup if any
|
||||
tmux display-popup -C
|
||||
exit_script 0
|
||||
}
|
||||
|
||||
update_project_list_cache() {
|
||||
PROJECT_LIST_UPDATE=$(find ~/* -name .git -type d -not -path "~/.*" -prune 2>/dev/null | sed 's/\/.git/\n/g' | sed "s/$(echo $HOME | sed 's/\//\\\//g')//" | cut -d'/' -f2-)
|
||||
|
||||
echo $PROJECT_LIST_UPDATE > $CACHE_FILE
|
||||
}
|
||||
|
||||
while getopts "lv" option; do
|
||||
debug "Reading option $option"
|
||||
case $option in
|
||||
l)
|
||||
OPEN_MODE="local"
|
||||
;;
|
||||
v)
|
||||
VERBOSE=true
|
||||
;;
|
||||
esac
|
||||
debug "Reading option $option"
|
||||
case $option in
|
||||
l)
|
||||
OPEN_MODE="local"
|
||||
;;
|
||||
v)
|
||||
VERBOSE=true
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
declare -A CUSTOM_SESSIONS
|
||||
@ -68,15 +80,28 @@ CUSTOM_SESSIONS[Projects]=Halia
|
||||
spinner &
|
||||
spinner_pid=$!
|
||||
|
||||
PROJECT_LIST=$(find ~/* -name .git -type d -not -path "~/.*" -prune 2>/dev/null)
|
||||
if [ ! -f "$CACHE_FILE" ]; then
|
||||
debug "Start synchronous project list cache update"
|
||||
update_project_list_cache
|
||||
else
|
||||
debug "Start asynchronous project list cache update"
|
||||
update_project_list_cache &
|
||||
fi
|
||||
|
||||
PROJECT_LIST=$(cat $CACHE_FILE)
|
||||
|
||||
kill $spinner_pid
|
||||
|
||||
SELECTED=$(echo $PROJECT_LIST | sed 's/\/.git/\n/g' | sed "s/$(echo $HOME | sed 's/\//\\\//g')//" | cut -d'/' -f2- | fzf)
|
||||
SELECTED=$(echo $PROJECT_LIST | tr ' ' '\n' | fzf)
|
||||
|
||||
debug "Selected $SELECTED"
|
||||
|
||||
PROJECT_PATH=$(echo $PROJECT_LIST | sed 's/\/.git/\n/g' | grep "${SELECTED}$")
|
||||
# Quit if nothing has been selected
|
||||
if [ ! $SELECTED ]; then
|
||||
exit_script 1
|
||||
fi
|
||||
|
||||
PROJECT_PATH=$(realpath ~/${SELECTED})
|
||||
PROJECT_ROOT=$(echo $SELECTED | cut -d '/' -f1)
|
||||
PROJECT_NAME=$(echo $SELECTED | rev | cut -d '/' -f1 | rev)
|
||||
|
||||
@ -86,10 +111,10 @@ debug "Project path is $PROJECT_PATH"
|
||||
debug "Opening in $OPEN_MODE"
|
||||
|
||||
case $OPEN_MODE in
|
||||
"new_pane")
|
||||
open_new_pane
|
||||
;;
|
||||
"local")
|
||||
open_local
|
||||
;;
|
||||
"new_pane")
|
||||
open_new_pane
|
||||
;;
|
||||
"local")
|
||||
open_local
|
||||
;;
|
||||
esac
|
||||
|
Loading…
Reference in New Issue
Block a user