2014-08-26 11:20:15 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
|
|
|
source "$CURRENT_DIR/helpers.sh"
|
2014-08-27 14:15:07 +00:00
|
|
|
source "$CURRENT_DIR/process_restore_helpers.sh"
|
2014-08-26 11:20:15 +00:00
|
|
|
|
2014-08-26 15:23:20 +00:00
|
|
|
is_line_type() {
|
|
|
|
local line_type="$1"
|
|
|
|
local line="$2"
|
|
|
|
echo "$line" |
|
|
|
|
\grep -q "^$line_type"
|
|
|
|
}
|
|
|
|
|
2014-08-26 13:52:07 +00:00
|
|
|
check_saved_session_exists() {
|
|
|
|
local saved_session="$(last_session_path)"
|
|
|
|
if [ ! -f $saved_session ]; then
|
|
|
|
display_message "Saved session not found!"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-08-26 11:20:15 +00:00
|
|
|
window_exists() {
|
|
|
|
local session_name="$1"
|
|
|
|
local window_number="$2"
|
|
|
|
tmux list-windows -t "$session_name" -F "#{window_index}" 2>/dev/null |
|
|
|
|
\grep -q "^$window_number$"
|
|
|
|
}
|
|
|
|
|
|
|
|
session_exists() {
|
|
|
|
local session_name="$1"
|
|
|
|
tmux has-session -t "$session_name" 2>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
first_window_num() {
|
|
|
|
tmux show -gv base-index
|
|
|
|
}
|
|
|
|
|
|
|
|
tmux_socket() {
|
|
|
|
echo $TMUX | cut -d',' -f1
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:51:56 +00:00
|
|
|
remove_first_char() {
|
|
|
|
echo "$1" | cut -c2-
|
|
|
|
}
|
|
|
|
|
2014-08-26 11:20:15 +00:00
|
|
|
new_window() {
|
|
|
|
local session_name="$1"
|
|
|
|
local window_number="$2"
|
|
|
|
local window_name="$3"
|
|
|
|
local dir="$4"
|
|
|
|
tmux new-window -d -t "${session_name}:${window_number}" -n "$window_name" -c "$dir"
|
|
|
|
}
|
|
|
|
|
|
|
|
new_session() {
|
|
|
|
local session_name="$1"
|
|
|
|
local window_number="$2"
|
|
|
|
local window_name="$3"
|
|
|
|
local dir="$4"
|
|
|
|
TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -n "$window_name" -c "$dir"
|
|
|
|
# change first window number if necessary
|
|
|
|
local created_window_num="$(first_window_num)"
|
|
|
|
if [ $created_window_num -ne $window_number ]; then
|
|
|
|
tmux move-window -s "${session_name}:${created_window_num}" -t "${session_name}:${window_number}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
new_pane() {
|
|
|
|
local session_name="$1"
|
|
|
|
local window_number="$2"
|
|
|
|
local window_name="$3"
|
|
|
|
local dir="$4"
|
2014-08-26 18:56:40 +00:00
|
|
|
tmux split-window -t "${session_name}:${window_number}" -c "$dir"
|
2014-08-26 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restore_pane() {
|
|
|
|
local pane="$1"
|
|
|
|
echo "$pane" |
|
2014-08-26 22:11:13 +00:00
|
|
|
while IFS=$'\t' read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_full_command; do
|
|
|
|
window_name="$(remove_first_char "$window_name")"
|
|
|
|
pane_full_command="$(remove_first_char "$pane_full_command")"
|
2014-08-26 11:20:15 +00:00
|
|
|
if window_exists "$session_name" "$window_number"; then
|
|
|
|
new_pane "$session_name" "$window_number" "$window_name" "$dir"
|
|
|
|
elif session_exists "$session_name"; then
|
|
|
|
new_window "$session_name" "$window_number" "$window_name" "$dir"
|
|
|
|
else
|
|
|
|
new_session "$session_name" "$window_number" "$window_name" "$dir"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:23:20 +00:00
|
|
|
restore_state() {
|
|
|
|
local state="$1"
|
|
|
|
echo "$state" |
|
|
|
|
while IFS=$'\t' read line_type client_session client_last_session; do
|
|
|
|
tmux switch-client -t "$client_last_session"
|
|
|
|
tmux switch-client -t "$client_session"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-08-26 11:20:15 +00:00
|
|
|
restore_all_sessions() {
|
|
|
|
while read line; do
|
2014-08-26 15:23:20 +00:00
|
|
|
if is_line_type "pane" "$line"; then
|
|
|
|
restore_pane "$line"
|
2014-08-26 16:54:39 +00:00
|
|
|
fi
|
|
|
|
done < $(last_session_path)
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:11:13 +00:00
|
|
|
restore_all_pane_processes() {
|
2014-08-27 11:12:32 +00:00
|
|
|
if restore_pane_processes_enabled; then
|
|
|
|
local pane_full_command
|
|
|
|
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $11 !~ "^:$" { print $2, $3, $7, $11; }' $(last_session_path) |
|
|
|
|
while IFS=$'\t' read session_name window_number pane_index pane_full_command; do
|
|
|
|
pane_full_command="$(remove_first_char "$pane_full_command")"
|
|
|
|
restore_pane_process "$pane_full_command" "$session_name" "$window_number" "$pane_index"
|
|
|
|
done
|
|
|
|
fi
|
2014-08-26 22:11:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 18:19:34 +00:00
|
|
|
restore_pane_layout_for_each_window() {
|
|
|
|
\grep '^window' $(last_session_path) |
|
|
|
|
while IFS=$'\t' read line_type session_name window_number window_active window_flags window_layout; do
|
|
|
|
tmux select-layout -t "${session_name}:${window_number}" "$window_layout"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-08-26 16:54:39 +00:00
|
|
|
restore_active_pane_for_each_window() {
|
|
|
|
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $7 != 0 && $9 == 1 { print $2, $3, $7; }' $(last_session_path) |
|
|
|
|
while IFS=$'\t' read session_name window_number active_pane; do
|
|
|
|
tmux switch-client -t "${session_name}:${window_number}"
|
|
|
|
tmux select-pane -t "$active_pane"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-08-26 17:16:51 +00:00
|
|
|
restore_active_and_alternate_windows() {
|
2014-08-26 18:29:55 +00:00
|
|
|
awk 'BEGIN { FS="\t"; OFS="\t" } /^window/ && $5 ~ /[*-]/ { print $2, $4, $3; }' $(last_session_path) |
|
2014-08-26 17:16:51 +00:00
|
|
|
sort -u |
|
|
|
|
while IFS=$'\t' read session_name active_window window_number; do
|
|
|
|
tmux switch-client -t "${session_name}:${window_number}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-08-26 16:54:39 +00:00
|
|
|
restore_active_and_alternate_sessions() {
|
|
|
|
while read line; do
|
|
|
|
if is_line_type "state" "$line"; then
|
2014-08-26 15:23:20 +00:00
|
|
|
restore_state "$line"
|
|
|
|
fi
|
2014-08-26 13:31:47 +00:00
|
|
|
done < $(last_session_path)
|
2014-08-26 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2014-08-26 13:47:31 +00:00
|
|
|
if supported_tmux_version_ok; then
|
2014-08-26 13:52:07 +00:00
|
|
|
check_saved_session_exists
|
2014-08-26 13:47:31 +00:00
|
|
|
restore_all_sessions
|
2014-08-26 22:11:13 +00:00
|
|
|
restore_all_pane_processes
|
|
|
|
restore_pane_layout_for_each_window >/dev/null 2>&1
|
|
|
|
# below functions restore exact cursor positions
|
2014-08-26 16:54:39 +00:00
|
|
|
restore_active_pane_for_each_window
|
2014-08-26 17:16:51 +00:00
|
|
|
restore_active_and_alternate_windows
|
2014-08-26 16:54:39 +00:00
|
|
|
restore_active_and_alternate_sessions
|
|
|
|
display_message "Restored all Tmux sessions!"
|
2014-08-26 13:47:31 +00:00
|
|
|
fi
|
2014-08-26 11:20:15 +00:00
|
|
|
}
|
|
|
|
main
|