Update t script

This commit is contained in:
Oliver Davies 2024-06-15 15:33:57 +01:00
parent 9fcce5ad14
commit 4d24228c30

View file

@ -7,14 +7,17 @@ let
script-t = writeShellApplication { script-t = writeShellApplication {
name = "t"; name = "t";
text = ''
# Credit to ThePrimeagen.
set -o nounset runtimeInputs = with pkgs; [
set -o pipefail openssl
tmux
];
text = ''
# Credit to ThePrimeagen and Jess Archer.
if [[ $# -eq 1 ]]; then if [[ $# -eq 1 ]]; then
selected=$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.
@ -24,28 +27,47 @@ let
! -name "*.old" ! -name "*.old"
) )
selected=$(echo "''${items}" | fzf) SELECTED_PATH=$(echo "''${items}" | fzf)
fi fi
if [[ -z "''${selected}" ]]; then SESSION_NAME=$(basename "''${SELECTED_PATH}" | sed 's/\./_/g')
exit 0
# Attach to an existing session, if one exists.
if tmux has-session -t "''${SESSION_NAME}" 2> /dev/null; then
tmux attach -t "''${SESSION_NAME}" || tmux switch-client -t "''${SESSION_NAME}"
exit
fi fi
session_name=$(basename "''${selected}" | sed 's/\./_/g') # TODO: support .ignored/.tmux
session_path="''${selected}" if [[ -x "''${SELECTED_PATH}/.tmux" ]]; then
DIGEST="$(openssl sha512 "''${SELECTED_PATH}/.tmux")"
# Git worktrees. # Prompt the first time we see a given .tmux file before running it.
if [[ -e "''${selected}/main" ]]; then if ! grep -q "''${DIGEST}" ~/..tmux.digests 2> /dev/null; then
session_path="''${selected}/main" 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
fi fi
if tmux has-session -t "''${session_name}" 2> /dev/null; then # If there is no session, create one.
tmux attach -t "''${session_name}" if ! tmux has-session -t "''${SESSION_NAME}" 2> /dev/null; then
tmux new-session -d -c "''${SELECTED_PATH}" -s "''${SESSION_NAME}"
fi fi
tmux new-session -d -s "''${session_name}" -c "''${session_path}" tmux switch-client -t "''${SESSION_NAME}" || tmux attach-session -t "''${SESSION_NAME}"
tmux switch-client -t "''${session_name}" || tmux attach -t "''${session_name}"
''; '';
}; };
in in