]> git.madduck.net Git - etc/tmux.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

add wrapper for single session support
authormartin f. krafft <madduck@madduck.net>
Tue, 24 Mar 2020 01:57:16 +0000 (14:57 +1300)
committermartin f. krafft <madduck@madduck.net>
Tue, 24 Mar 2020 01:57:16 +0000 (14:57 +1300)
.bin/tmux [new file with mode: 0755]

diff --git a/.bin/tmux b/.bin/tmux
new file mode 100755 (executable)
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 "$@"