Update t script

Stop automatically running `.tmux` files as I don't always want
everything to run when opening a project. This also simplifies the code
within the script.

This also refactors the logic for creating and switching sessions as it
didn't work 100% of the time. This is nearer to Jess Archer's version of
the script with less error output and changes needed for shellcheck.
This commit is contained in:
Oliver Davies 2024-10-28 16:29:22 +00:00
parent a7f2d90542
commit 1bc80ccef3

View file

@ -9,78 +9,30 @@
];
text = ''
# Credit to ThePrimeagen and Jess Archer.
# Based on similar scripts by ThePrimeagen and Jess Archer.
function execute_tmux_file {
local tmux_file="$1"
selected_path="$2"
session_name="$3"
if [[ $# -eq 1 ]]; then
selected_path=$1
else
# Get the session name from fuzzy-finding list of directories and generating a
# tmux-safe version.
items=$(find "$REPOS" ~/Documents \
-maxdepth 1 -mindepth 1 -type d \
! -name "_archive" \
! -name "*-old" \
! -name "*.old"
)
DIGEST="$(openssl sha512 "$tmux_file")"
selected_path=$(echo "''${items}" | sort | fzf --reverse)
fi
# 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"
session_name=$(basename "$selected_path" | sed 's/\./_/g')
read -r -n 1 -p "Trust (and run) this .tmux file? (t = trust, otherwise = skip) "
if tmux switch-client -t="$session_name" 2>/dev/null; then
exit 0
fi
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
selected_path=$1
else
# Get the session name from fuzzy-finding list of directories and generating a
# tmux-safe version.
items=$(find "$REPOS" ~/Documents \
-maxdepth 1 -mindepth 1 -type d \
! -name "_archive" \
! -name "*-old" \
! -name "*.old"
)
selected_path=$(echo "''${items}" | sort | fzf --reverse)
fi
session_name=$(basename "$selected_path" | sed 's/\./_/g')
# 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
if [[ -x "$selected_path/.tmux" ]]; then
execute_tmux_file "$selected_path/.tmux" "$selected_path" "$session_name"
elif [[ -x "$selected_path/.ignored/.tmux" ]]; then
execute_tmux_file "$selected_path/.ignored/.tmux" "$selected_path" "$session_name"
fi
# If there is no session, create one.
if ! tmux has-session -t "$session_name" 2> /dev/null; then
tmux new-session -d -c "$selected_path" -s "$session_name"
fi
tmux switch-client -t "$session_name" 2>/dev/null || tmux attach-session -t "$session_name"
}
main "$@"
( (tmux new-session -c "$selected_path" -d -s "$session_name" && tmux switch-client -t "$session_name") 2>/dev/null ) ||
tmux new-session -c "$selected_path" -A -s "$session_name"
'';
}