Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: f0e0518a25
Fetching contributors…

Cannot retrieve contributors at this time

executable file 186 lines (153 sloc) 4.539 kB
#! /bin/sh
VERSION=0.3
version() {
echo "$0 version $VERSION"
}
usage() {
cat << EOF
usage: $0 [options] [session name]
options:
-h Show this message
-v Output version information
-V Verbose
tmux options:
-d Detach other clients when connecting to session, the same as tmux's
attach-session -d option
-S Specify a socket file to use with tmux, the same as tmux's -S option
etmux options:
-c If a session script doesn't exist for this session name, create one,
linking it to the current directory
EOF
}
# DEFAULTS
detach=
socket=
create=
help=
version=
verbose=false
# options
short="dS:cvVh"
long="detach,socket:,create,version,help"
getopt -T > /dev/null 2> /dev/null
if [ $? -eq 4 ]; then
# the getopt supports long arguments
opts=`getopt -o $short --long $long -n "$0" -- "$@"`
else
# fall back to basic getopt
opts=`getopt $short "$@"`
fi
if [ $? != 0 ] ; then echo "getopt failed, terminating..." >&2 ; exit 1 ; fi
eval set -- "$opts"
while true
do case "$1" in
d|-d|--detach) detach="-d" ; shift ;;
c|-c|--create) create=true ; shift ;;
S|-S|--socket)
# socket has a required argument
socket="$2"
socket_flag="-S \"$2\""
shift 2 ;;
v|-v|--version) version=true; shift ;;
h|-h|--help) help=true; shift ;;
V|-V|--verbose) verbose=true; shift ;;
--) shift ; break ;;
esac
done
if [ $help ]; then
version
usage
exit
fi
if [ $version ]; then
version
exit
fi
# the first argument passed in will be our session name
session=$1
# make sure we have a session name
if [ -z "$session" ]; then
session='0'
fi
#default ETMUX_PATH
if [ -z "$ETMUX_PATH" ]; then
ETMUX_PATH=~/.etmux-sessions
fi
if [ -z "$socket" ] && [ ! -z "$TMUX" ]; then
# they haven't specified their own socket, so we get the location of the
# current one, to make sure all commands use the right socket
socket=`echo $TMUX | sed "s/^\(.*\),[0-9]\{1,\},[0-9]\{1,\}$/\1/"`
socket_flag="-S \"$socket\""
fi
# go ahead and create join here, since both code paths use it
if [ -z "$TMUX" ]; then
join_cmd="tmux $socket_flag attach-session $detach -t \"$session\""
else
$verbose && echo "in tmux now, so will switch"
if [ ! -z "$detach" ]; then
detach="detach-client -s $session \\;"
fi
join_cmd="tmux $socket_flag $detach switch-client -t \"$session\""
fi
checkForSessionScript() {
# split the ETMUX_PATH string
old_ifs=$IFS; IFS=':'; paths=$ETMUX_PATH; IFS=$old_ifs
# loop through each dir in path to see if we have a match
for x in $paths; do
$verbose && echo "looking in $x..."
# if we are supposed to create the script and it doesn't exist...
if [ $create ] && [ ! -f "$x/$session" ]; then
$verbose && echo "$x/$session not found, creating..."
createDefaultSessionScript > "$x/$session"
chmod u+x "$x/$session"
fi
if [ -x "$x/$session" ]; then
echo "$x/$session"
break
fi
done
}
createDefaultSessionScript() {
echo "DEFAULT"
}
runSessionScript() {
$verbose && echo "found $1, running..."
# create custom script
filename=`mktemp /tmp/etmux-session-XXXXXXX`
echo "start() {\n\t$start_cmd\n}\n" >> $filename
echo "join() {\n\t$join_cmd\n}\n" >> $filename
if [ ! -z "$socket_flag" ]; then
echo "alias tmux='tmux $socket_flag'"
fi
cat "$1" >> $filename
# run the custom script
env socket="$socket" etmux=$filename session=$session sh $filename
#remove custom script
rm $filename
# exit so we don't do anything else
exit
}
# does the session exist already?
if [ -z "$socket" ]; then
has=`tmux start-server \; list-sessions | sed -n -e "s/$session: [0-9]\{1,\} windows /\0/p"`
else
has=`tmux -S "$socket" start-server \; list-sessions | sed -n -e "s/$session: [0-9]\{1,\} windows /\0/p"`
fi
# session doesn't exist
if [ -z "$has" ]; then
$verbose && echo "creating session '$session'..."
start_cmd="env TMUX= tmux $socket_flag new-session -d -s \"$session\""
sessionScript=`checkForSessionScript`
if [ -z "$sessionScript" ]; then
# no script, just start and join ourselves
$verbose && echo "No custom script for session, starting normally..."
eval $start_cmd
eval $join_cmd
else
runSessionScript $sessionScript
fi
else
#session exists
$verbose && echo "joining session '$session'..."
eval $join_cmd
fi
Jump to Line
Something went wrong with that request. Please try again.