-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore_session.sh
executable file
·73 lines (70 loc) · 2.37 KB
/
restore_session.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
#!/usr/bin/env bash
cd "$(dirname "${BASH_SOURCE[0]}")" || exit
source common_utils.sh
get_all_sessions() {
local -r all_files="$(ls "$SAVE_DIR")"
for file in $all_files; do
if [[ "$file" =~ _last$ ]] && [[ "${file%%_last}" != "$CURRENT_SESSION" ]]; then
echo "${file%%_last}"
fi
done
local -r all_sessions="$(tmux list-sessions -F "#{session_name}")"
for session in $all_sessions; do
if [[ "$session" != "$CURRENT_SESSION" ]]; then
echo "$session"
fi
done
}
select_session() {
local -r sessions=$(get_all_sessions | sort | uniq)
if command -v fzf 1>/dev/null; then
echo "$sessions" | fzf
else
PS3="Select session or 0 to cancel: "
select session in $sessions; do
if (( REPLY == 0 )); then
exit
elif (( REPLY > 0 && REPLY <= $(echo "$sessions" | wc -w) )); then
echo "$session"
break
fi
done
fi
}
session_name="$(select_session)"
if [[ -z "$session_name" ]]; then
exit 0
elif ! tmux has-session -t "$session_name" 2> /dev/null; then
start_spinner "Restoring session $session_name"
tmux new-session -ds "$session_name" -c "$HOME"
while read -r line; do
if grep -q "^window" <<< "$line"; then
IFS=$SEPARATOR read -r _ window_index window_name _ _ <<< "$line"
tmux new-window -k -t "$session_name:$window_index" -n "$window_name"
elif grep -q "^pane" <<< "$line"; then
IFS=$SEPARATOR read -r _ pane_index pane_current_path pane_active window_index command <<< "$line"
if [[ "$pane_index" == "$(get_tmux_option base-index 0)" ]]; then
tmux send-keys -t "$session_name:$window_index" "cd \"$pane_current_path\"" Enter "clear" Enter
else
tmux split-window -d -t "$session_name:$window_index" -c "$pane_current_path"
fi
if [[ "$pane_active" == "1" ]]; then
tmux select-pane -t "$session_name:$window_index.$pane_index"
fi
if [[ -n "$command" ]]; then
tmux send-keys -t "$session_name:$window_index.$pane_index" "$command" Enter
fi
fi
done < "${SAVE_DIR}/${session_name}_last"
while read -r line; do
if grep -q "^window" <<< "$line"; then
IFS=$SEPARATOR read -r _ window_index _ window_layout window_active <<< "$line"
tmux select-layout -t "$session_name:$window_index" "$window_layout"
if [[ "$window_active" == "1" ]]; then
tmux select-window -t "$session_name:$window_index"
fi
fi
done < "${SAVE_DIR}/${session_name}_last"
stop_spinner "Session restored"
fi
tmux switch-client -t "$session_name"