diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ed8cb3..1b3ae5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ issues during the restore and is now fixed. - restoring sessions multiple times messes up the whole environment - new panes are all around. This is now fixed - pane restorations are now idempotent. +- if pane exists from before session restore - do not restore the process within + it. This makes the restoration process even more idempotent. ### v0.2.0, 2014-08-29 - bugfix: with vim 'session' strategy, if the session file does not exist - make diff --git a/scripts/process_restore_helpers.sh b/scripts/process_restore_helpers.sh index c9e22ef..7681a0d 100644 --- a/scripts/process_restore_helpers.sh +++ b/scripts/process_restore_helpers.sh @@ -13,7 +13,7 @@ restore_pane_process() { local window_number="$3" local pane_index="$4" local dir="$5" - if _process_should_be_restored "$pane_full_command"; then + if _process_should_be_restored "$pane_full_command" "$session_name" "$window_number" "$pane_index"; then tmux switch-client -t "${session_name}:${window_number}" tmux select-pane -t "$pane_index" @@ -32,7 +32,14 @@ restore_pane_process() { _process_should_be_restored() { local pane_full_command="$1" - if _restore_all_processes; then + local session_name="$2" + local window_number="$3" + local pane_index="$4" + if is_pane_registered_as_existing "$session_name" "$window_number" "$pane_index"; then + # Scenario where pane existed before restoration, so we're not + # restoring the proces either. + return 1 + elif _restore_all_processes; then return 0 elif _process_on_the_restore_list "$pane_full_command"; then return 0 diff --git a/scripts/session_restorer.sh b/scripts/session_restorer.sh index 7984ef5..bc736e5 100755 --- a/scripts/session_restorer.sh +++ b/scripts/session_restorer.sh @@ -7,6 +7,12 @@ source "$CURRENT_DIR/helpers.sh" source "$CURRENT_DIR/process_restore_helpers.sh" source "$CURRENT_DIR/spinner_helpers.sh" +# Global variable. +# Used during the restoration: if a pane already exists from before, it is +# 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="" + is_line_type() { local line_type="$1" local line="$2" @@ -30,6 +36,23 @@ pane_exists() { \grep -q "^$pane_index$" } +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" ]] +} + window_exists() { local session_name="$1" local window_number="$2" @@ -81,13 +104,14 @@ new_pane() { restore_pane() { local pane="$1" - echo "$pane" | 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 dir="$(remove_first_char "$dir")" window_name="$(remove_first_char "$window_name")" pane_full_command="$(remove_first_char "$pane_full_command")" if pane_exists "$session_name" "$window_number" "$pane_index"; then - true # pane exists, no need to create it! + # Pane exists, no need to create it! + # Pane existence is registered. Later, it's process also isn't restored. + register_existing_pane "$session_name" "$window_number" "$pane_index" elif window_exists "$session_name" "$window_number"; then new_pane "$session_name" "$window_number" "$window_name" "$dir" elif session_exists "$session_name"; then @@ -95,7 +119,7 @@ restore_pane() { else new_session "$session_name" "$window_number" "$window_name" "$dir" fi - done + done < <(echo "$pane") } restore_state() {