From fb641881c2cc82c71a24d63bdbb8fa032340d0e4 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Fri, 20 Feb 2015 02:05:58 +0100 Subject: [PATCH] Don't start auto-saving if another tmux server is running --- CHANGELOG.md | 2 ++ resurrect_auto.tmux | 20 +++++++++++++++++--- scripts/helpers.sh | 30 ++++++++++++++++++++++++++++++ scripts/resurrect_auto_restore.sh | 31 +++---------------------------- 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7161ba..c2c786b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - bugfixes for 'tmux auto start' OS X Terminal.app and iTerm scripts - prevent saving for the first 15 minutes only when plugin is sourced the first time (not on subsequent sources or tmux.conf reloads) +- do not start auto-saving if there's another tmux server running (we don't want + for save files from various tmux environments to override each other) ### v2.1.0, 2015-02-18 - enable "tmux auto start" for OS X diff --git a/resurrect_auto.tmux b/resurrect_auto.tmux index 514ea3c..1535bff 100755 --- a/resurrect_auto.tmux +++ b/resurrect_auto.tmux @@ -16,6 +16,15 @@ handle_tmux_automatic_start() { $CURRENT_DIR/scripts/handle_tmux_automatic_start.sh } +another_tmux_server_running() { + if just_started_tmux_server; then + another_tmux_server_running_on_startup + else + # script loaded after tmux server start can have multiple clients attached + [ "$(number_tmux_processes_except_current_server)" -gt "$(number_current_server_client_processes)" ] + fi +} + delay_saving_environment_on_first_plugin_load() { if [ -z "$(get_tmux_option "$last_auto_save_option" "")" ]; then # last save option not set, this is first time plugin load @@ -44,9 +53,14 @@ main() { if supported_tmux_version_ok; then handle_tmux_automatic_start - # give user a chance to restore previously saved session - delay_saving_environment_on_first_plugin_load - add_resurrect_save_interpolation + # Advanced edge case handling: start auto-saving only if this is the + # only tmux server. We don't want saved files from more environments to + # overwrite each other. + if ! another_tmux_server_running; then + # give user a chance to restore previously saved session + delay_saving_environment_on_first_plugin_load + add_resurrect_save_interpolation + fi if just_started_tmux_server; then start_auto_restore_in_background diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 206eac1..97c0e4f 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -14,3 +14,33 @@ set_tmux_option() { local value="$2" tmux set-option -gq "$option" "$value" } + +# multiple tmux server detection helpers + +current_tmux_server_pid() { + echo "$TMUX" | + cut -f2 -d"," +} + +all_tmux_processes() { + ps -Ao "command pid" | + \grep "^tmux" +} + +number_tmux_processes_except_current_server() { + all_tmux_processes | + \grep -v " $(current_tmux_server_pid)$" | + wc -l | + sed "s/ //g" +} + +number_current_server_client_processes() { + tmux list-clients | + wc -l | + sed "s/ //g" +} + +another_tmux_server_running_on_startup() { + # there are 2 tmux processes (current tmux server + 1) on tmux startup + [ "$(number_tmux_processes_except_current_server)" -gt 1 ] +} diff --git a/scripts/resurrect_auto_restore.sh b/scripts/resurrect_auto_restore.sh index 65eba23..27afbc4 100755 --- a/scripts/resurrect_auto_restore.sh +++ b/scripts/resurrect_auto_restore.sh @@ -10,33 +10,6 @@ auto_restore_enabled() { [ "$auto_restore_value" == "on" ] && [ ! -f "$auto_restore_halt_file" ] } -current_tmux_server_pid() { - echo "$TMUX" | - cut -f2 -d"," -} - -all_tmux_processes() { - ps -Ao "command pid" | - \grep "^tmux" -} - -tmux_processes_except_current_server() { - all_tmux_processes | - \grep -v " $(current_tmux_server_pid)$" -} - -number_tmux_processes_except_current_server() { - all_tmux_processes | - \grep -v " $(current_tmux_server_pid)$" | - wc -l | - sed "s/ //g" -} - -another_tmux_server_running() { - # there are 2 tmux processes (current tmux server + 1) on tmux startup - [ "$(number_tmux_processes_except_current_server)" -gt 1 ] -} - fetch_and_run_tmux_resurrect_restore_script() { # give tmux some time to start and source all the plugins sleep 1 @@ -47,7 +20,9 @@ fetch_and_run_tmux_resurrect_restore_script() { } main() { - if auto_restore_enabled && ! another_tmux_server_running; then + # Advanced edge case handling: auto restore only if this is the only tmux + # server. If another tmux server exists, it is assumed auto-restore is not wanted. + if auto_restore_enabled && ! another_tmux_server_running_on_startup; then fetch_and_run_tmux_resurrect_restore_script fi }