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

if panetitle is hostname, add windowname
[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:-}" = 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 else
38   case "${1:-;}" in
39     (\;) :;;
40     (*) real_tmux "$@";;
41   esac
42 fi
43
44 # only without argument create a new session with a random name/id to which we
45 # can then attach
46
47 hascmd() { command -v "$@" >/dev/null;}
48
49 if   hascmd uuid; then alias uuidgen=uuid
50 elif hascmd uuidgen; then :
51 elif hascmd python3; then
52   uuidgen() { python3 -c 'import uuid; print(uuid.uuid1())'; }
53 elif hascmd python; then
54   uuidgen() { python -c 'import uuid; print(uuid.uuid1())'; }
55 else
56   uuidgen() { dd if=/dev/urandom bs=16 count=1 status=none | base64; }
57 fi
58 UUID=$(uuidgen)
59 echo "$UUID" > $LASTSESSIONIDFILE
60
61 echo >&2 Starting a new session with ID $UUID…
62
63 single_tmux_session $UUID "$@"