Do not restore processes within existing panes

Closes #23
This commit is contained in:
Bruno Sutic 2014-08-29 13:42:48 +02:00
parent 9a6e4a1a2c
commit f9ef86d604
No known key found for this signature in database
GPG Key ID: 66D96E4F2F7EF26C
3 changed files with 38 additions and 5 deletions

View File

@ -5,6 +5,8 @@
issues during the restore and is now fixed. issues during the restore and is now fixed.
- restoring sessions multiple times messes up the whole environment - new panes - restoring sessions multiple times messes up the whole environment - new panes
are all around. This is now fixed - pane restorations are now idempotent. 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 ### v0.2.0, 2014-08-29
- bugfix: with vim 'session' strategy, if the session file does not exist - make - bugfix: with vim 'session' strategy, if the session file does not exist - make

View File

@ -13,7 +13,7 @@ restore_pane_process() {
local window_number="$3" local window_number="$3"
local pane_index="$4" local pane_index="$4"
local dir="$5" 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 switch-client -t "${session_name}:${window_number}"
tmux select-pane -t "$pane_index" tmux select-pane -t "$pane_index"
@ -32,7 +32,14 @@ restore_pane_process() {
_process_should_be_restored() { _process_should_be_restored() {
local pane_full_command="$1" 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 return 0
elif _process_on_the_restore_list "$pane_full_command"; then elif _process_on_the_restore_list "$pane_full_command"; then
return 0 return 0

View File

@ -7,6 +7,12 @@ source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/process_restore_helpers.sh" source "$CURRENT_DIR/process_restore_helpers.sh"
source "$CURRENT_DIR/spinner_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() { is_line_type() {
local line_type="$1" local line_type="$1"
local line="$2" local line="$2"
@ -30,6 +36,23 @@ pane_exists() {
\grep -q "^$pane_index$" \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() { window_exists() {
local session_name="$1" local session_name="$1"
local window_number="$2" local window_number="$2"
@ -81,13 +104,14 @@ new_pane() {
restore_pane() { restore_pane() {
local pane="$1" 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 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")" dir="$(remove_first_char "$dir")"
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
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 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
@ -95,7 +119,7 @@ restore_pane() {
else else
new_session "$session_name" "$window_number" "$window_name" "$dir" new_session "$session_name" "$window_number" "$window_name" "$dir"
fi fi
done done < <(echo "$pane")
} }
restore_state() { restore_state() {