]> git.madduck.net Git - etc/tmux.git/blob - .bin/tmux

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:

fall back to python uuid gen
[etc/tmux.git] / .bin / tmux
1 #!/bin/sh
2 set -eu
3
4 LASTSESSIONIDFILE="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/tmux-last-session-id"
5
6 real_tmux() {
7   local exec; exec=exec
8   if [ -n "${_tmux_eval_only:-}" ]; then
9     exec=
10   fi
11   $exec $(which -a tmux | grep -v $HOME | head -1) "$@"
12 }
13
14 single_tmux_session() {
15   local id; id="$1"; shift
16   real_tmux new -t "$id" "$@"
17 }
18
19 tmux_session_exists() {
20   _tmux_eval_only=1 real_tmux has-session -t "$1"
21 }
22
23 if [ "${1:-}" = --connect-to-last ]; then
24   shift
25   if [ -f "$LASTSESSIONIDFILE" ]; then
26     UUID="$(cat $LASTSESSIONIDFILE)"
27     if tmux_session_exists $UUID; then
28       echo >&2 Attaching to existing session with ID $UUID…
29       single_tmux_session "$(cat $LASTSESSIONIDFILE)" "$@"
30     else
31       echo >&2 W: No tmux session with ID $UUID found, generating a new one…
32     fi
33   else
34     echo >&2 W: No tmux session ID stored, generating a new one…
35   fi
36
37 elif [ -n "${1:-}" ]; then
38   real_tmux "$@"
39 fi
40
41 # only without argument create a new session with a random name/id to which we
42 # can then attach
43
44 if ! command -v uuidgen >/dev/null; then
45   uuidgen() { python3 -c 'import uuid; print(uuid.uuid1())'; }
46 fi
47 UUID=$(uuidgen)
48 echo "$UUID" > $LASTSESSIONIDFILE
49
50 echo >&2 Starting a new session with ID $UUID…
51
52 single_tmux_session $UUID "$@"