2014-08-26 11:20:15 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
2014-08-28 22:17:02 +00:00
|
|
|
source "$CURRENT_DIR/variables.sh"
|
2014-08-26 11:20:15 +00:00
|
|
|
source "$CURRENT_DIR/helpers.sh"
|
2014-08-27 14:15:07 +00:00
|
|
|
source "$CURRENT_DIR/process_restore_helpers.sh"
|
2014-08-28 10:58:07 +00:00
|
|
|
source "$CURRENT_DIR/spinner_helpers.sh"
|
2014-08-26 11:20:15 +00:00
|
|
|
|
2015-02-09 15:34:24 +00:00
|
|
|
# delimiter
|
|
|
|
d=$'\t'
|
|
|
|
|
2014-08-29 11:42:48 +00:00
|
|
|
# Global variable.
|
2014-08-29 16:59:14 +00:00
|
|
|
# Used during the restore: if a pane already exists from before, it is
|
2014-08-29 11:42:48 +00:00
|
|
|
# saved in the array in this variable. Later, process running in existing pane
|
|
|
|
# is also not restored. That makes the restoration process more idempotent.
|
|
|
|
EXISTING_PANES_VAR=""
|
|
|
|
|
2015-02-10 14:09:23 +00:00
|
|
|
RESTORING_FROM_SCRATCH="false"
|
|
|
|
|
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() {
|
2014-08-29 16:59:14 +00:00
|
|
|
local resurrect_file="$(last_resurrect_file)"
|
|
|
|
if [ ! -f $resurrect_file ]; then
|
|
|
|
display_message "Tmux resurrect file not found!"
|
2014-08-28 10:45:01 +00:00
|
|
|
return 1
|
2014-08-26 13:52:07 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-08-29 10:49:06 +00:00
|
|
|
pane_exists() {
|
|
|
|
local session_name="$1"
|
|
|
|
local window_number="$2"
|
|
|
|
local pane_index="$3"
|
|
|
|
tmux list-panes -t "${session_name}:${window_number}" -F "#{pane_index}" 2>/dev/null |
|
|
|
|
\grep -q "^$pane_index$"
|
|
|
|
}
|
|
|
|
|
2014-08-29 11:42:48 +00:00
|
|
|
register_existing_pane() {
|
|
|
|
local session_name="$1"
|
|
|
|
local window_number="$2"
|
|
|
|
local pane_index="$3"
|
|
|
|
local pane_custom_id="${session_name}:${window_number}:${pane_index}"
|
|
|
|
local delimiter=$'\t'
|
|
|
|
EXISTING_PANES_VAR="${EXISTING_PANES_VAR}${delimiter}${pane_custom_id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
is_pane_registered_as_existing() {
|
|
|
|
local session_name="$1"
|
|
|
|
local window_number="$2"
|
|
|
|
local pane_index="$3"
|
|
|
|
local pane_custom_id="${session_name}:${window_number}:${pane_index}"
|
|
|
|
[[ "$EXISTING_PANES_VAR" =~ "$pane_custom_id" ]]
|
|
|
|
}
|
|
|
|
|
2015-02-10 14:09:23 +00:00
|
|
|
restore_from_scratch_true() {
|
|
|
|
RESTORING_FROM_SCRATCH="true"
|
|
|
|
}
|
|
|
|
|
|
|
|
is_restoring_from_scratch() {
|
|
|
|
[ "$RESTORING_FROM_SCRATCH" == "true" ]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-02 20:34:00 +00:00
|
|
|
tmux split-window -t "${session_name}:${window_number}" -c "$dir"
|
|
|
|
# minimize window so more panes can fit
|
|
|
|
tmux resize-pane -t "${session_name}:${window_number}" -U "999"
|
2014-08-26 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restore_pane() {
|
|
|
|
local pane="$1"
|
2015-02-09 15:34:24 +00:00
|
|
|
while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_full_command; do
|
2014-08-28 22:41:13 +00:00
|
|
|
dir="$(remove_first_char "$dir")"
|
2014-08-26 22:11:13 +00:00
|
|
|
window_name="$(remove_first_char "$window_name")"
|
|
|
|
pane_full_command="$(remove_first_char "$pane_full_command")"
|
2014-08-29 10:49:06 +00:00
|
|
|
if pane_exists "$session_name" "$window_number" "$pane_index"; then
|
2015-02-10 14:09:23 +00:00
|
|
|
if is_restoring_from_scratch; then
|
|
|
|
# overwrite the pane
|
|
|
|
# happens only for the first pane if it's the only registered pane for the whole tmux server
|
|
|
|
local pane_id="$(tmux display-message -p -F "#{pane_id}" -t "$session_name:$window_number")"
|
|
|
|
new_pane "$session_name" "$window_number" "$window_name" "$dir"
|
|
|
|
tmux kill-pane -t "$pane_id"
|
|
|
|
else
|
|
|
|
# Pane exists, no need to create it!
|
|
|
|
# Pane existence is registered. Later, its process also won't be restored.
|
|
|
|
register_existing_pane "$session_name" "$window_number" "$pane_index"
|
|
|
|
fi
|
2014-08-29 10:49:06 +00:00
|
|
|
elif window_exists "$session_name" "$window_number"; then
|
2014-08-26 11:20:15 +00:00
|
|
|
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
|
2014-08-29 11:42:48 +00:00
|
|
|
done < <(echo "$pane")
|
2014-08-26 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 15:23:20 +00:00
|
|
|
restore_state() {
|
|
|
|
local state="$1"
|
|
|
|
echo "$state" |
|
2015-02-09 15:34:24 +00:00
|
|
|
while IFS=$d read line_type client_session client_last_session; do
|
2014-08-26 15:23:20 +00:00
|
|
|
tmux switch-client -t "$client_last_session"
|
|
|
|
tmux switch-client -t "$client_session"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2015-02-10 12:44:14 +00:00
|
|
|
restore_grouped_session() {
|
|
|
|
local grouped_session="$1"
|
|
|
|
echo "$grouped_session" |
|
|
|
|
while IFS=$d read line_type grouped_session original_session alternate_window active_window; do
|
|
|
|
TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$grouped_session" -t "$original_session"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_active_and_alternate_windows_for_grouped_sessions() {
|
|
|
|
local grouped_session="$1"
|
|
|
|
echo "$grouped_session" |
|
|
|
|
while IFS=$d read line_type grouped_session original_session alternate_window_index active_window_index; do
|
|
|
|
alternate_window_index="$(remove_first_char "$alternate_window_index")"
|
|
|
|
active_window_index="$(remove_first_char "$active_window_index")"
|
|
|
|
if [ -n "$alternate_window_index" ]; then
|
|
|
|
tmux switch-client -t "${grouped_session}:${alternate_window_index}"
|
|
|
|
fi
|
|
|
|
if [ -n "$active_window_index" ]; then
|
|
|
|
tmux switch-client -t "${grouped_session}:${active_window_index}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2015-02-10 14:09:23 +00:00
|
|
|
never_ever_overwrite() {
|
|
|
|
local overwrite_option_value="$(get_tmux_option "$overwrite_option" "")"
|
|
|
|
[ -n "$overwrite_option_value" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
detect_if_restoring_from_scratch() {
|
|
|
|
if never_ever_overwrite; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
local total_number_of_panes="$(tmux list-panes -a | wc -l | sed 's/ //g')"
|
|
|
|
if [ "$total_number_of_panes" -eq 1 ]; then
|
|
|
|
restore_from_scratch_true
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-02-10 12:44:14 +00:00
|
|
|
# functions called from main (ordered)
|
|
|
|
|
2014-08-29 10:49:06 +00:00
|
|
|
restore_all_panes() {
|
2015-02-10 14:09:23 +00:00
|
|
|
detect_if_restoring_from_scratch
|
2014-08-26 11:20:15 +00:00
|
|
|
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
|
2014-08-29 16:59:14 +00:00
|
|
|
done < $(last_resurrect_file)
|
2014-08-26 16:54:39 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 12:44:14 +00:00
|
|
|
restore_pane_layout_for_each_window() {
|
|
|
|
\grep '^window' $(last_resurrect_file) |
|
|
|
|
while IFS=$d 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-10-12 22:47:44 +00:00
|
|
|
restore_shell_history() {
|
|
|
|
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $7, $10; }' $(last_resurrect_file) |
|
2015-02-09 15:34:24 +00:00
|
|
|
while IFS=$d read session_name window_number pane_index pane_command; do
|
2014-10-12 22:47:44 +00:00
|
|
|
if ! is_pane_registered_as_existing "$session_name" "$window_number" "$pane_index"; then
|
|
|
|
if [ "$pane_command" = "bash" ]; then
|
|
|
|
local pane_id="$session_name:$window_number.$pane_index"
|
|
|
|
# tmux send-keys has -R option that should reset the terminal.
|
|
|
|
# However, appending 'clear' to the command seems to work more reliably.
|
|
|
|
local read_command="history -r '$(resurrect_history_file "$pane_id")'; clear"
|
|
|
|
tmux send-keys -t "$pane_id" "$read_command" C-m
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
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
|
2014-08-29 16:59:14 +00:00
|
|
|
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $11 !~ "^:$" { print $2, $3, $7, $8, $11; }' $(last_resurrect_file) |
|
2015-02-09 15:34:24 +00:00
|
|
|
while IFS=$d read session_name window_number pane_index dir pane_full_command; do
|
2014-08-28 22:41:13 +00:00
|
|
|
dir="$(remove_first_char "$dir")"
|
2014-08-27 11:12:32 +00:00
|
|
|
pane_full_command="$(remove_first_char "$pane_full_command")"
|
2014-08-27 22:43:31 +00:00
|
|
|
restore_pane_process "$pane_full_command" "$session_name" "$window_number" "$pane_index" "$dir"
|
2014-08-27 11:12:32 +00:00
|
|
|
done
|
|
|
|
fi
|
2014-08-26 22:11:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 16:54:39 +00:00
|
|
|
restore_active_pane_for_each_window() {
|
2014-08-29 16:59:14 +00:00
|
|
|
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $9 == 1 { print $2, $3, $7; }' $(last_resurrect_file) |
|
2015-02-09 15:34:24 +00:00
|
|
|
while IFS=$d read session_name window_number active_pane; do
|
2014-08-26 16:54:39 +00:00
|
|
|
tmux switch-client -t "${session_name}:${window_number}"
|
|
|
|
tmux select-pane -t "$active_pane"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2015-02-12 12:29:49 +00:00
|
|
|
restore_zoomed_windows() {
|
|
|
|
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $6 ~ /Z/ && $9 == 1 { print $2, $3; }' $(last_resurrect_file) |
|
|
|
|
while IFS=$d read session_name window_number; do
|
|
|
|
tmux resize-pane -t "${session_name}:${window_number}" -Z
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2015-02-09 01:25:50 +00:00
|
|
|
restore_grouped_sessions() {
|
|
|
|
while read line; do
|
|
|
|
if is_line_type "grouped_session" "$line"; then
|
|
|
|
restore_grouped_session "$line"
|
2015-02-09 14:57:48 +00:00
|
|
|
restore_active_and_alternate_windows_for_grouped_sessions "$line"
|
2015-02-09 01:25:50 +00:00
|
|
|
fi
|
|
|
|
done < $(last_resurrect_file)
|
|
|
|
}
|
|
|
|
|
2014-08-26 17:16:51 +00:00
|
|
|
restore_active_and_alternate_windows() {
|
2014-08-29 16:59:14 +00:00
|
|
|
awk 'BEGIN { FS="\t"; OFS="\t" } /^window/ && $5 ~ /[*-]/ { print $2, $4, $3; }' $(last_resurrect_file) |
|
2014-08-26 17:16:51 +00:00
|
|
|
sort -u |
|
2015-02-09 15:34:24 +00:00
|
|
|
while IFS=$d read session_name active_window window_number; do
|
2014-08-26 17:16:51 +00:00
|
|
|
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-29 16:59:14 +00:00
|
|
|
done < $(last_resurrect_file)
|
2014-08-26 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2014-08-28 10:45:01 +00:00
|
|
|
if supported_tmux_version_ok && check_saved_session_exists; then
|
2014-08-29 17:51:47 +00:00
|
|
|
start_spinner "Restoring..." "Tmux restore complete!"
|
2014-08-29 10:49:06 +00:00
|
|
|
restore_all_panes
|
2014-08-26 22:11:13 +00:00
|
|
|
restore_pane_layout_for_each_window >/dev/null 2>&1
|
2014-10-17 20:33:29 +00:00
|
|
|
if save_bash_history_option_on; then
|
|
|
|
restore_shell_history
|
|
|
|
fi
|
2014-08-29 14:16:03 +00:00
|
|
|
restore_all_pane_processes
|
2014-08-26 22:11:13 +00:00
|
|
|
# below functions restore exact cursor positions
|
2014-08-26 16:54:39 +00:00
|
|
|
restore_active_pane_for_each_window
|
2014-08-29 15:04:00 +00:00
|
|
|
restore_zoomed_windows
|
2015-02-09 14:57:48 +00:00
|
|
|
restore_grouped_sessions # also restores active and alt windows for grouped sessions
|
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
|
2014-08-28 10:58:07 +00:00
|
|
|
stop_spinner
|
2014-08-29 16:59:14 +00:00
|
|
|
display_message "Tmux restore complete!"
|
2014-08-26 13:47:31 +00:00
|
|
|
fi
|
2014-08-26 11:20:15 +00:00
|
|
|
}
|
|
|
|
main
|