-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmux-sessions.sh
executable file
·357 lines (319 loc) · 9.37 KB
/
tmux-sessions.sh
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash -e
########################################################################
#** Version: 1.0
#* This script stores and restores tmux sessions.
#
########################################################################
# note: the frame for this script was auto-created with
# *https://github.com/inofix/admin-toolbox/blob/master/makebashscript.sh*
# author/copyright: <[email protected]>
# license: gladly sharing with the universe and any lifeform,
# be it naturally born or constructed, alien or kin..
# USE AT YOUR OWN RISK.
########################################################################
[ "$1" == "debug" ] && shift && set -x
## variables ##
### you may copy the following variables into this file for having your own
### local config ...
conffile=~/.tmux-sessions.sh
### {{{
confdir=~/.tmux-sessions/
nogroup="nogroup"
rm_interactive=0
verbose=0
# not used yet: favorite_term="/usr/bin/urxvt -fn 6x13 +sb -tr -bg black -fg white -sh 60 -sl 10000"
### }}}
# The system tools we gladly use. Thank you!
declare -A sys_tools
sys_tools=(
["_cut"]="/usr/bin/cut"
["_find"]="/usr/bin/find"
["_grep"]="/bin/grep"
["_head"]="/usr/bin/head"
["_mkdir"]="/bin/mkdir"
["_rm"]="/bin/rm"
["_rmdir"]="/bin/rmdir"
["_sed"]="/bin/sed"
["_tmux"]="/usr/bin/tmux"
)
danger_tools=( "_mkdir" "_rm" "_rmdir" )
## functions ##
print_usage()
{
echo "usage: $0 [options] action [sessionname]..."
}
print_help()
{
print_usage
$_grep "^#\* " $0 | $_sed 's;^#\*;;'
}
print_version()
{
$_grep "^#\*\* " $0 | $_sed 's;^#\*\*;;'
}
die()
{
[ $verbose -eq 0 ] && echo "$@"
exit 1
}
error()
{
[ $verbose -eq 0 ] && print_usage
[ $verbose -eq 0 ] && echo ""
die "Error: $@"
}
list()
{
declare -A sessions
stored_sessions=( $($_find $confdir -type f -printf '%P\n') )
for s in ${stored_sessions[@]} ; do
ifs=$IFS
IFS="/"
vs=( $s )
IFS=$ifs
has_session=""
sgroup="${vs[0]}"
session="${vs[1]%.*}"
client=$($_tmux list-clients 2> /dev/null | \
$_grep " $session " | $_cut -d":" -f1)
store="$s"
if ! $_tmux has-session -t "$session" 2> /dev/null ; then
sessions["$session"]="$has_session;;$sgroup;$client;$store"
fi
done
active_sessions=( $($_tmux list-sessions -F '#{session_group};#S') ) || true
for s in ${active_sessions[@]} ; do
ifs=$IFS
IFS=";"
vs=( $s )
IFS=$ifs
group="${vs[0]}"
[ -z "$group" ] && group=$nogroup
session="${vs[1]}"
client=$($_tmux list-clients | $_grep " $session " | $_cut -d":" -f1)
store=$($_find $confdir -type f -name "$session.new")
[ -z "$store" ] &&
store=$($_find $confdir -type f -name "$session.copy")
store=${store##$confdir}
sgroup=${store%/*}
sessions["$session"]="y;$group;$sgroup;$client;$store"
done
printf "[\e[1mgroup#:name]%6s[session\e[0m]%10s[\e[1mclient\e[0m]%6s[\e[1mstorage file\e[0m]\n"
for session in ${!sessions[@]} ; do
ifs=$IFS
IFS=";"
vs=( ${sessions[$session]} )
IFS=$ifs
has_session="${vs[0]}"
group="${vs[1]}"
sgroup="${vs[2]}"
client="${vs[3]}"
store="${vs[4]}"
out="["
s=1
if [ -z "$group" ] || [ "$group" == "$nogroup" ] ; then
out="$out\e[0;31m.\e[0;39m:"
(( s++ ))
else
out="$out\e[0;32m$group\e[0;39m:"
(( s += ${#group} ))
fi
if [ -z "$sgroup" ] ; then
out="$out\e[0;31m.\e[0;39m]"
(( s++ ))
elif [ "$sgroup" == "$nogroup" ] ; then
out="$out\e[0;33m$sgroup\e[0;39m]"
(( s += ${#sgroup} ))
else
out="$out\e[0;32m$sgroup\e[0;39m]"
(( s += ${#sgroup} ))
fi
(( s = 17 - s ))
out="$out%${s}s"
if [ -n "$client" ] ; then
out="$out[\e[1;32m$session\e[0;39m]"
elif [ -n "$has_session" ] ; then
out="$out[\e[1;33m$session\e[0;39m]"
else
out="$out[\e[1;31m$session\e[0;39m]"
fi
(( s = 17 - ${#session} ))
out="$out%${s}s"
if [ -n "$client" ] ; then
out="$out[\e[0;32m$client\e[0;39m]"
(( s = 12 - ${#client} ))
else
out="$out[\e[0;31m...\e[0;39m]"
s=9
fi
out="$out%${s}s"
if [ -n "$store" ] ; then
out="$out[\e[0;32m$store\e[0;39m]"
else
out="$out[\e[0;31m...\e[0;39m]"
fi
printf "$out\n"
done
}
store()
{
requested=" $@ "
sessions=( $($_tmux list-sessions -F '#{session_group};#S') )
for s in ${sessions[@]} ; do
ifs=$IFS
IFS=";"
vs=( $s )
IFS=$ifs
group="${vs[0]}"
session="${vs[1]}"
stored=$($_find $confdir -type f -name "${session}.*" -printf '%P\n')
if [ -n "$stored" ] ; then
group=${stored%/*}
fi
if [ ${#@} -gt 0 ] ; then
[ -n "${requested/*$session*/}" ] && continue
fi
[ $verbose -eq 0 ] && echo "processing: $group:$session"
if [ -z "$group" ] ; then
group="$nogroup"
elif [ -d "$confdir/$group" ] ; then
orig="$($_find $confdir/$group -type f -name "*.new" \
-printf '%f\n' | $_head -1)"
if [ -n "$orig" ] && [ "$session" != "${orig%.new}" ] ; then
echo "$_tmux new-session -d -t ${orig%.new} -s $session"\
> $confdir/$group/${session}.copy
continue
fi
fi
$_mkdir -p $confdir/$group
workpaths=( $($_tmux list-panes -t $session -F '#{pane_current_path}') )
echo "cd ${workpaths[0]}" > $confdir/$group/${session}.new
windows=( $($_tmux list-windows -t $session -F '#W') )
echo "$_tmux new -d -s $session -n ${windows[0]} " \
"-c ${workpaths[0]}" \
> $confdir/$group/${session}.new
for (( i=1; i<${#windows[@]}; i++ )) ; do
echo "$_tmux new-window -t $session -n ${windows[i]}" \
>> $confdir/$group/${session}.new
done
echo "$_tmux attach -t $session" >> $confdir/$group/${session}.new
done
}
init()
{
[ $verbose -eq 0 ] && echo "Creating config directory: $confdir"
$_mkdir -p $confdir
}
attach()
{
echo "please implement me first.."
exit 0
}
cleanup()
{
[ -d "$confdir" ] && $_rm $rm_options $confdir/*/*
}
restore()
{
requested=" $@ "
sessions=( $($_find $confdir -type f -name "*.new") \
$($_find $confdir -type f -name "*.copy") )
for f in ${sessions[@]} ; do
s="${f##*/}"
session="${s%.*}"
if [ ${#@} -gt 0 ] ; then
[ -n "${requested/*$session*/}" ] && continue
fi
if ! $_tmux has-session -t "$session" 2> /dev/null ; then
. "$f"
[ $verbose -eq 0 ] && echo "Session $session restored."
fi
done
}
## logic ##
## first set the system tools
for t in ${!sys_tools[@]} ; do
if [ -x "${sys_tools[$t]%% *}" ] ; then
export ${t}="${sys_tools[$t]}"
else
error "Missing system tool: ${sys_tools[$t]##* } must be installed."
fi
done
[ -r "$conffile" ] && . $conffile
#* options:
while true ; do
case "$1" in
#* -f |--force do not ask
-f|--force)
rm_interactive=1
;;
#* -h |--help print this help
-h|--help)
print_help
exit 0
;;
#* -i |--interactive ask before removing
-i|--interactive)
rm_interactive=0
;;
#* -q |--quiet be as silent as reasonable
-q|--quiet)
verbose=1
;;
#* -v |--verbose give some feedback
-v|--verbose)
verbose=0
;;
#* -V |--version print version info and exit
-V|--version)
print_version
exit
;;
--*|-*)
error "option $1 not supported"
;;
*)
break
;;
esac
shift
done
rm_options=""
[ $verbose -eq 0 ] && rm_options="-v"
[ $rm_interactive -eq 0 ] && rm_options="$rm_options -i"
#* actions:
case $1 in
##* attach attach all deattached sessions
# a*)
# restore
# attach
# ;;
#* store save the current sessions setup
s*)
shift
init
store $@
;;
#* cleanup remove the stored session setup
cle*)
cleanup
;;
#* restore create sessions from stored setup
re*)
shift
restore $@
;;
#* list list active and stored sessions
l*)
list
;;
*)
print_usage
;;
esac
#* TODO:
#* Missing Features:
#* * Implement the separate attach action
#* * somehow respect history (at least ssh sessions would be nice)
#* Known Bugs:
#* * does not check if session exists etc