From be5f66f5744a0d6496b5f4cdb465e8881cec949c Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sat, 13 Jan 2024 15:59:14 +0100 Subject: [PATCH] feat(tmux-projectinator): Add cache file --- .bin/tmux-projectinator | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/.bin/tmux-projectinator b/.bin/tmux-projectinator index 5a801a1..560a6cc 100755 --- a/.bin/tmux-projectinator +++ b/.bin/tmux-projectinator @@ -5,6 +5,7 @@ set -o emacs bind '"\e": "\C-W\c-d"' OPEN_MODE="new_pane" +CACHE_FILE=~/.cache/project_list debug () { if [ "$VERBOSE" = true ]; then @@ -50,6 +51,12 @@ open_new_pane () { tmux display-popup -C } +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 @@ -68,15 +75,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 0 +fi + +PROJECT_PATH=$(realpath ~/${SELECTED}) PROJECT_ROOT=$(echo $SELECTED | cut -d '/' -f1) PROJECT_NAME=$(echo $SELECTED | rev | cut -d '/' -f1 | rev)