t: extract a function that finds and executes

...`.tmux` files
This commit is contained in:
Oliver Davies 2024-07-08 22:25:42 +01:00
parent 9677ef8ecd
commit 75731bda19

View file

@ -11,8 +11,41 @@
text = '' text = ''
# Credit to ThePrimeagen and Jess Archer. # Credit to ThePrimeagen and Jess Archer.
function execute_tmux_file {
local tmux_file="$1"
selected_path="$2"
session_name="$3"
DIGEST="$(openssl sha512 "$tmux_file")"
# Prompt the first time we see a given .tmux file before running it.
if ! grep -q "$DIGEST" ~/..tmux.digests 2> /dev/null; then
cat "$tmux_file"
read -r -n 1 -p "Trust (and run) this .tmux file? (t = trust, otherwise = skip) "
if [[ $REPLY =~ ^[Tt]$ ]]; then
echo "$DIGEST" >> ~/..tmux.digests
create_session_and_run_tmux_file "$tmux_file" "$selected_path" "$session_name"
fi
else
create_session_and_run_tmux_file "$tmux_file" "$selected_path" "$session_name"
fi
}
function create_session_and_run_tmux_file {
tmux_file="$1"
selected_path="$2"
session_name="$3"
tmux new-session -d -c "$selected_path" -s "$session_name"
(cd "$selected_path" && "$tmux_file" "$session_name")
}
function main {
if [[ $# -eq 1 ]]; then if [[ $# -eq 1 ]]; then
SELECTED_PATH=$1 selected_path=$1
else else
# Get the session name from fuzzy-finding list of directories and generating a # Get the session name from fuzzy-finding list of directories and generating a
# tmux-safe version. # tmux-safe version.
@ -23,67 +56,31 @@
! -name "*.old" ! -name "*.old"
) )
SELECTED_PATH=$(echo "''${items}" | fzf) selected_path=$(echo "''${items}" | fzf)
fi fi
SESSION_NAME=$(basename "''${SELECTED_PATH}" | sed 's/\./_/g') session_name=$(basename "$selected_path" | sed 's/\./_/g')
# Attach to an existing session, if one exists. # Attach to an existing session, if one exists.
if tmux has-session -t "''${SESSION_NAME}" 2> /dev/null; then if tmux has-session -t "$session_name" 2> /dev/null; then
tmux attach -t "''${SESSION_NAME}" || tmux switch-client -t "''${SESSION_NAME}" tmux attach -t "$session_name" || tmux switch-client -t "$session_name"
exit exit
fi fi
# TODO: refactor to a function that works for both .tmux and .ignored/.tmux files. if [[ -x "$selected_path/.tmux" ]]; then
if [[ -x "''${SELECTED_PATH}/.tmux" ]]; then execute_tmux_file "$selected_path/.tmux" "$selected_path" "$session_name"
DIGEST="$(openssl sha512 "''${SELECTED_PATH}/.tmux")" elif [[ -x "$selected_path/.ignored/.tmux" ]]; then
execute_tmux_file "$selected_path/.ignored/.tmux" "$selected_path" "$session_name"
# Prompt the first time we see a given .tmux file before running it.
if ! grep -q "''${DIGEST}" ~/..tmux.digests 2> /dev/null; then
cat "''${SELECTED_PATH}/.tmux"
read -r -n 1 -p "Trust (and run) this .tmux file? (t = trust, otherwise = skip) "
if [[ $REPLY =~ ^[Tt]$ ]]; then
echo "''${DIGEST}" >> ~/..tmux.digests
# Create a new session and run the .tmux script.
tmux new-session -d -c "''${SELECTED_PATH}" -s "''${SESSION_NAME}"
(cd "''${SELECTED_PATH}" && "''${SELECTED_PATH}/.tmux" "''${SESSION_NAME}")
fi
else
# Create a new session and run the .tmux script.
tmux new-session -d -c "''${SELECTED_PATH}" -s "''${SESSION_NAME}"
(cd "''${SELECTED_PATH}" && "''${SELECTED_PATH}/.tmux" "''${SESSION_NAME}")
fi
elif [[ -x "''${SELECTED_PATH}/.ignored/.tmux" ]]; then
DIGEST="$(openssl sha512 "''${SELECTED_PATH}/.ignored/.tmux")"
# Prompt the first time we see a given .ignored/.tmux file before running it.
if ! grep -q "''${DIGEST}" ~/..ignored/.tmux.digests 2> /dev/null; then
cat "''${SELECTED_PATH}/.ignored/.tmux"
read -r -n 1 -p "Trust (and run) this .tmux file? (t = trust, otherwise = skip) "
if [[ $REPLY =~ ^[Tt]$ ]]; then
echo "''${DIGEST}" >> ~/..tmux.digests
# Create a new session and run the .ignored/.tmux script.
tmux new-session -d -c "''${SELECTED_PATH}" -s "''${SESSION_NAME}"
(cd "''${SELECTED_PATH}" && "''${SELECTED_PATH}/.ignored/.tmux" "''${SESSION_NAME}")
fi
else
# Create a new session and run the .tmux script.
tmux new-session -d -c "''${SELECTED_PATH}" -s "''${SESSION_NAME}"
(cd "''${SELECTED_PATH}" && "''${SELECTED_PATH}/.tmux" "''${SESSION_NAME}")
fi
fi fi
# If there is no session, create one. # If there is no session, create one.
if ! tmux has-session -t "''${SESSION_NAME}" 2> /dev/null; then if ! tmux has-session -t "$session_name" 2> /dev/null; then
tmux new-session -d -c "''${SELECTED_PATH}" -s "''${SESSION_NAME}" tmux new-session -d -c "$selected_path" -s "$session_name"
fi fi
tmux switch-client -t "''${SESSION_NAME}" || tmux attach-session -t "''${SESSION_NAME}" tmux switch-client -t "$session_name" || tmux attach-session -t "$session_name"
}
main "$@"
''; '';
} }