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,41 +9,8 @@
]; ];
text = '' 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"
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
@ -61,26 +28,11 @@
session_name=$(basename "$selected_path" | sed 's/\./_/g') session_name=$(basename "$selected_path" | sed 's/\./_/g')
# Attach to an existing session, if one exists. if tmux switch-client -t="$session_name" 2>/dev/null; then
if tmux has-session -t "$session_name" 2> /dev/null; then exit 0
tmux attach -t "$session_name" || tmux switch-client -t "$session_name"
exit
fi fi
if [[ -x "$selected_path/.tmux" ]]; then ( (tmux new-session -c "$selected_path" -d -s "$session_name" && tmux switch-client -t "$session_name") 2>/dev/null ) ||
execute_tmux_file "$selected_path/.tmux" "$selected_path" "$session_name" tmux new-session -c "$selected_path" -A -s "$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 "$@"
''; '';
} }