From 455487e9e45cf78f4fea7fb91dab927b95e42bf4 Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Tue, 24 Mar 2020 14:57:16 +1300 Subject: [PATCH] add wrapper for single session support --- .bin/tmux | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 .bin/tmux diff --git a/.bin/tmux b/.bin/tmux new file mode 100755 index 0000000..d6ff4b2 --- /dev/null +++ b/.bin/tmux @@ -0,0 +1,49 @@ +#!/bin/sh +set -eu + +LASTSESSIONIDFILE="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/tmux-last-session-id" + +real_tmux() { + local exec; exec=exec + if [ -n "${_tmux_eval_only:-}" ]; then + exec= + fi + $exec $(which -a tmux | grep -v $HOME | head -1) "$@" +} + +single_tmux_session() { + local id; id="$1"; shift + real_tmux new -t "$id" "$@" +} + +tmux_session_exists() { + _tmux_eval_only=1 real_tmux has-session -t "$1" +} + +if [ "${1:-}" = --connect-to-last ]; then + shift + if [ -f "$LASTSESSIONIDFILE" ]; then + UUID="$(cat $LASTSESSIONIDFILE)" + if tmux_session_exists $UUID; then + echo >&2 Attaching to existing session with ID $UUID… + single_tmux_session "$(cat $LASTSESSIONIDFILE)" "$@" + else + echo >&2 W: No tmux session with ID $UUID found, generating a new one… + fi + else + echo >&2 W: No tmux session ID stored, generating a new one… + fi + +elif [ -n "${1:-}" ]; then + real_tmux "$@" +fi + +# only without argument create a new session with a random name/id to which we +# can then attach + +UUID=$(uuidgen) +echo "$UUID" > $LASTSESSIONIDFILE + +echo >&2 Starting a new session with ID $UUID… + +single_tmux_session $UUID "$@" -- 2.39.2