From: martin f. krafft Date: Tue, 24 Mar 2020 01:57:16 +0000 (+1300) Subject: add wrapper for single session support X-Git-Url: https://git.madduck.net/etc/tmux.git/commitdiff_plain/455487e9e45cf78f4fea7fb91dab927b95e42bf4?hp=0111482029a08e9cd29719b326a83f5856939985 add wrapper for single session support --- 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 "$@"