Created
August 15, 2016 19:35
-
-
Save arooni/1a42d63d638ef40f7ec9099f0aaad7eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
# A useful wrapper to invoke tmux that I got from a guy on IRC #tmux | |
# 5/20/2016 -- Modified by David Parkinson | |
# Basically invokes a new sesion if no argument, and group attaches to an existing session | |
# and kills any group sessions that are unattached. Very useful. | |
# from https://ntnn.de/plain//tmux_attach_or_new | |
# Now added tmuxifier logic | |
link=0 | |
# Give me a random # between 0 & 9999 | |
NUMBER=$(shuf -i 0-9999 -n 1) | |
#!/usr/bin/env sh | |
link=0 | |
case $1 in | |
(-l|--link) | |
link=1 | |
shift | |
;; | |
esac | |
tmuxifier_has() { | |
if command -v tmuxifier > /dev/null $2 &>/dev/null; then | |
return 1; | |
fi | |
if tmuxifier list-${1} | grep $2 &>/dev/null; then | |
return 0; | |
fi | |
return 1; | |
} | |
session_is_attached() { | |
if tmux list-sessions | grep "^$1" | grep attached; then | |
return 0; | |
fi | |
return 1; | |
} | |
if [ $# -eq 0 ]; then | |
# no parameter given, starting new session with tmux' naming scheme | |
tmux new-session | |
elif [ $# -eq 1 ]; then | |
if tmux has-session -t "$1"; then | |
# attach and link if --link was passed or the session has no | |
# attached client - otherwise group-attach | |
if [ $link -eq 1 ] || ! session_is_attached "$1"; then | |
tmux -2 attach-session -t "$1" | |
else | |
# get a random number from /dev/random to prevent attaching | |
# to an existing grouped session | |
random=$(od -An -d -N2 /dev/random) | |
# define grouped session name | |
group_sess_name="${1}-group-${random## }" | |
tmux -2 new-session -t "$1" -s "$group_sess_name" | |
# kill session if it exists after exiting the client | |
if tmux has-session -t "$group_sess_name"; then | |
tmux kill-session -t "$group_sess_name" | |
fi | |
fi | |
elif tmuxifier_has sessions $1; then | |
# if the parameter is the name of a session layout start that | |
tmuxifier load-session "$1" | |
elif tmuxifier_has windows $1; then | |
# if the parameter is the name of a window layout start that | |
tmuxifier load-window "$1" | |
else | |
# the parameter is not a running session nor a layout - start | |
# a new session with that name | |
tmux -2 new-session -s "$1" | |
fi | |
else | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment