Don't start auto-saving if another tmux server is running

pull/7/head
Bruno Sutic 2015-02-20 02:05:58 +01:00
parent 297e5b6993
commit fb641881c2
No known key found for this signature in database
GPG Key ID: 66D96E4F2F7EF26C
4 changed files with 52 additions and 31 deletions

View File

@ -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

View File

@ -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

View File

@ -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 ]
}

View File

@ -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
}