]> 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:

differentiate between -s/-t for new sessions on older clients
[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   local flag; flag=t
17   real_tmux list-sessions | grep -q "^${id}:" || flag=s
18   real_tmux new -$flag "$id" "$@"
19 }
20
21 tmux_session_exists() {
22   _tmux_eval_only=1 real_tmux has-session -t "$1"
23 }
24
25 if [ "${1:-}" = last ]; then
26   shift
27   if [ -f "$LASTSESSIONIDFILE" ]; then
28     UUID="$(cat $LASTSESSIONIDFILE)"
29     if tmux_session_exists $UUID; then
30       echo >&2 Attaching to existing session with ID $UUID…
31       single_tmux_session "$(cat $LASTSESSIONIDFILE)" "$@"
32     else
33       echo >&2 W: No tmux session with ID $UUID found, generating a new one…
34     fi
35   else
36     echo >&2 W: No tmux session ID stored, generating a new one…
37   fi
38
39 else
40   case "${1:-;}" in
41     (\;) :;;
42     (*) real_tmux "$@";;
43   esac
44 fi
45
46 # only without argument create a new session with a random name/id to which we
47 # can then attach
48
49 hascmd() { command -v "$@" >/dev/null;}
50
51 if   hascmd uuid; then alias uuidgen=uuid
52 elif hascmd uuidgen; then :
53 elif hascmd python3; then
54   uuidgen() { python3 -c 'import uuid; print(uuid.uuid1())'; }
55 elif hascmd python; then
56   uuidgen() { python -c 'import uuid; print(uuid.uuid1())'; }
57 else
58   uuidgen() { dd if=/dev/urandom bs=16 count=1 status=none | base64; }
59 fi
60 UUID=$(uuidgen)
61 echo "$UUID" > $LASTSESSIONIDFILE
62
63 echo >&2 Starting a new session with ID $UUID…
64
65 single_tmux_session $UUID "$@"