-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmuxsession.sh
executable file
·155 lines (139 loc) · 4.37 KB
/
tmuxsession.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
#! /bin/bash
# collection of functions to manage tmux sessions with dmenu
function new_session()
{
local i=1
if [[ ! -z $1 ]]; then
tmux new-session -s $1
return 0
fi
local sname=$(new_session_name)
tmux new-session -s $sname
}
# building a new session name
function new_session_name()
{
local i=1
declare -a slist=( $(tmux ls -F '#{session_name}') )
declare -i len=${#slist[@]:-2}
if ! tmux has-session -t 0x0$i 2>/dev/null; then
echo "0x0$i"
return
fi
for session in ${slist[@]}; do
if [[ $session == 0x?? ]]; then
let i=i+1
if ! tmux has-session -t 0x0$i 2>/dev/null; then
echo "0x0$i"
return
fi
fi
done
}
# dumping sate of a tmux session
# dump contains currently running application and current working directory
function dump_session()
{
local session_name=$1
local delim=$':'
if tmux has-session -t $session_name; then
tmux list-windows -F "#W${delim}#{pane_current_path}" -t ${session_name}
fi
}
# write state of a given tmux session in a sav file
function save_session()
{
local session_name=${1:?"${FUNCNAME[0]} session name is required"}
local session_dir=${2:?"${FUNCNAME[0]} directory where sessions saved is required"}
if [[ -d $session_dir ]]; then
(dump_session $session_name)> "$session_dir/${session_name}.sav"
else
echo "${FUNCNAME[0]} No such directory $session_dir"
fi
}
# reload session state from file, build tmux session and attach them
# session cannot be reload if they is already active
# if the session is active they should be attached instead of reloaded
function reload_session()
{
local session_name=${1:?"${FUNCNAME[0]} session name required"}
local session_dir=${2:?"${FUNCNAME[0]} directory where sessions saved required"}
local OLDIFS=${IFS}
if tmux has-session -t $session_name 2>/dev/null; then
attach_session $session_name $session_dir
return
fi
if [[ -d $session_dir ]]; then
while IFS=$':' read application path; do
if ! tmux has-session -t $session_name 2>/dev/null; then
tmux new-session -d -s $session_name
fi
tmux new-window -a -c $path "$application"
done < "$session_dir/${session_name}.sav"
else
echo "${FUNCNAME[0]} No such directory $session_dir"
fi
tmux attach -t $session_name
IFS=${OLDIFS}
}
function attach_session()
{
local session_name=${1:?"${FUNCNAME[0]} session name required"}
local savdir=${2:?"${FUNCNAME[0]} directory of saved session required"}
local newsession=$3
OLDIFS=${IFS}
IFS=$'\x0a'
if tmux has-session -t $session_name 2>/dev/null; then
is_attached=$(is_session_attached $session_name)
if [[ $is_attached -eq 1 ]]; then
attach_attached_session $session_name $newsession
else
tmux attach-session -t $session_name
fi
else
reload_session $session_name $savdir
fi
IFS=${OLDIFS}
}
# check if given session is already anywhere attached or not
function is_session_attached()
{
local session_name=${1:?"${FUNCNAME[0]} no session name given"}
local delim=$":"
tmux ls -F "#{session_name}${delim}#{session_attached}" | grep "^$session_name" | cut -d"$delim" -f2
}
# getting all names of sessions for that exists an .sav file
function get_saved_sessions()
{
local savdir=$1
declare -a savedsessions=( $( find $savdir -name "*.sav" ) )
savedsessions=( ${savedsessions[@]##*/} )
echo ${savedsessions[@]%.sav}
}
# function to attach one session from multiple clients
function attach_attached_session()
{
local basesession=$1
local newsession=${2:-$(new_session_name)}
tmux new-session -d -t $basesession -s $newsession
tmux new-window
tmux attach-session -t $newsession \; set-option destroy-unattached
}
# create complete session list, containing saved sessions and activ ones
function get_selection_list()
{
echo `get_saved_sessions` | tr ' ' '\n' && tmux ls
}
# choosing a session from a dmenu selection to attach them
function choose_session()
{
local savdir=$1
IFS=$'\n'
local target=$( get_selection_list |
dmenu -nb '#000' -nf '#ff7c00' -sf '#000' -sb '#3cff00' | awk -F':' '{print $1}')
attach_session $target $savdir
}
function list_sessions()
{
tmux ls
}