Enable 'full restore' by overwriting a single pane

This commit is contained in:
Bruno Sutic 2015-02-10 15:09:23 +01:00
parent ddf9c5ef87
commit c4375bf642
No known key found for this signature in database
GPG Key ID: 66D96E4F2F7EF26C
4 changed files with 46 additions and 12 deletions

View File

@ -1,6 +1,8 @@
# Changelog # Changelog
### master ### master
- if restore is started when there's only **1 pane in the whole tmux server**,
assume the users wants the "full restore" and overrwrite that pane.
### v2.0.0, 2015-02-10 ### v2.0.0, 2015-02-10
- add link to the wiki page for "first pane/window issue" to the README as well - add link to the wiki page for "first pane/window issue" to the README as well

View File

@ -51,15 +51,10 @@ This plugin goes to great lengths to save and restore all the details from your
Requirements / dependencies: `tmux 1.9` or higher, `bash`. Requirements / dependencies: `tmux 1.9` or higher, `bash`.
`tmux-resurrect` is idempotent! It will not try to restore panes or windows that `tmux-resurrect` is idempotent! It will not try to restore panes or windows that
already exist. already exist.<br/>
The single exception to this is when tmux is started with only 1 pane in order
### FAQ to restore previous tmux env. In this case only will this single pane be
overwritten.
> I have a problem: first pane/window is not restoring!
Check out
[this wiki page](https://github.com/tmux-plugins/tmux-resurrect/wiki/Help:-issues-with-the-first-window)
for the explanation and problem solution.
### Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended) ### Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended)

View File

@ -16,6 +16,8 @@ d=$'\t'
# is also not restored. That makes the restoration process more idempotent. # is also not restored. That makes the restoration process more idempotent.
EXISTING_PANES_VAR="" EXISTING_PANES_VAR=""
RESTORING_FROM_SCRATCH="false"
is_line_type() { is_line_type() {
local line_type="$1" local line_type="$1"
local line="$2" local line="$2"
@ -56,6 +58,14 @@ is_pane_registered_as_existing() {
[[ "$EXISTING_PANES_VAR" =~ "$pane_custom_id" ]] [[ "$EXISTING_PANES_VAR" =~ "$pane_custom_id" ]]
} }
restore_from_scratch_true() {
RESTORING_FROM_SCRATCH="true"
}
is_restoring_from_scratch() {
[ "$RESTORING_FROM_SCRATCH" == "true" ]
}
window_exists() { window_exists() {
local session_name="$1" local session_name="$1"
local window_number="$2" local window_number="$2"
@ -114,9 +124,17 @@ restore_pane() {
window_name="$(remove_first_char "$window_name")" window_name="$(remove_first_char "$window_name")"
pane_full_command="$(remove_first_char "$pane_full_command")" pane_full_command="$(remove_first_char "$pane_full_command")"
if pane_exists "$session_name" "$window_number" "$pane_index"; then if pane_exists "$session_name" "$window_number" "$pane_index"; then
# Pane exists, no need to create it! if is_restoring_from_scratch; then
# Pane existence is registered. Later, it's process also isn't restored. # overwrite the pane
register_existing_pane "$session_name" "$window_number" "$pane_index" # 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
elif window_exists "$session_name" "$window_number"; then elif window_exists "$session_name" "$window_number"; then
new_pane "$session_name" "$window_number" "$window_name" "$dir" new_pane "$session_name" "$window_number" "$window_name" "$dir"
elif session_exists "$session_name"; then elif session_exists "$session_name"; then
@ -159,9 +177,25 @@ restore_active_and_alternate_windows_for_grouped_sessions() {
done done
} }
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
}
# functions called from main (ordered) # functions called from main (ordered)
restore_all_panes() { restore_all_panes() {
detect_if_restoring_from_scratch
while read line; do while read line; do
if is_line_type "pane" "$line"; then if is_line_type "pane" "$line"; then
restore_pane "$line" restore_pane "$line"

View File

@ -28,3 +28,6 @@ save_command_strategy_option="@resurrect-save-command-strategy"
default_save_command_strategy="ps" default_save_command_strategy="ps"
bash_history_option="@resurrect-save-bash-history" bash_history_option="@resurrect-save-bash-history"
# set to 'on' to ensure panes are never ever overwritten
overwrite_option="@resurrect-never-overwrite"