-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetmux
executable file
·202 lines (169 loc) · 4.9 KB
/
etmux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#! /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
EOF
}
# DEFAULTS
detach=
socket=
help=
version=
verbose=false
# options
short="dS:cvVh"
long="detach,socket:,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 ;;
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="${PWD##*/}"
fi
#default ETMUX_PATH
if [ -z "$ETMUX_PATH" ]; then
ETMUX_PATH=~/.etmux-sessions
fi
#default ETMUX_SCRIPT_NAME
if [ -z "$ETMUX_SCRIPT_NAME" ]; then
ETMUX_SCRIPT_NAME=.etmux.sh
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
$verbose && echo "not in tmux, will attach"
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
# loop through each dir in path to see if we have a match
for x in $paths; do
$verbose && echo "looking in $x" >&2
if [ -d "$x/$session" ]; then
$verbose && echo "found directory: $x/$session" >&2
echo "$x/$session"
break
elif [ -x "$x/$session" ]; then
$verbose && echo "found script: $x/$session" >&2
echo "$x/$session"
break
elif [ -f "$x/$session" ]; then
echo "found \`$session\` in $x but it is not a directory or executable" >&2
exit 1
fi
done
IFS=$old_ifs
}
runSessionScript() {
$verbose && echo "running $1" >&2
if [ -d "$x/$session" ]; then
cd $1
fi
# 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'" >> $filename
fi
if [ -d "$1" -a -x "$1/$ETMUX_SCRIPT_NAME" ]; then
cat "$1/$ETMUX_SCRIPT_NAME" >> $filename
elif [ ! -d "$1" -a -x "$1" ]; then
cat "$1" >> $filename
else
echo "start && join" >> $filename
fi
$verbose && echo "created tmp script at $filename:" >&2
$verbose && echo "---------------------------------" >&2
$verbose && cat $filename >&2
$verbose && echo "---------------------------------" >&2
# run the custom script
env socket="$socket" 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
if [ -z "$has" ]; then
# session doesn't exist
$verbose && echo "creating session '$session'"
start_cmd="env TMUX= tmux $socket_flag new-session -d -s \"$session\""
sessionScript=`checkForSessionScript`
if [ "$?" -ne 0 ]; then
exit
fi
if [ -z "$sessionScript" ]; then
# no script, just start and join ourselves
$verbose && echo "did not find matching project, starting in current dir"
eval $start_cmd
eval $join_cmd
else
runSessionScript $sessionScript
fi
else
# session exists
$verbose && echo "joining session '$session'"
eval $join_cmd
fi