tmux-continuum/continuum.tmux

93 lines
2.6 KiB
Plaintext
Raw Normal View History

2015-02-11 14:43:37 +00:00
#!/usr/bin/env bash
set -x
2015-02-11 14:43:37 +00:00
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2015-02-12 14:45:16 +00:00
source "$CURRENT_DIR/scripts/helpers.sh"
source "$CURRENT_DIR/scripts/variables.sh"
source "$CURRENT_DIR/scripts/shared.sh"
2015-02-11 14:43:37 +00:00
2015-02-20 12:38:09 +00:00
save_command_interpolation="#($CURRENT_DIR/scripts/continuum_save.sh)"
2015-02-11 14:43:37 +00:00
2015-02-12 14:52:10 +00:00
supported_tmux_version_ok() {
2015-02-20 14:48:36 +00:00
"$CURRENT_DIR/scripts/check_tmux_version.sh" "$SUPPORTED_VERSION"
2015-02-12 14:52:10 +00:00
}
handle_tmux_automatic_start() {
2015-02-20 14:48:36 +00:00
"$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
}
2015-02-19 23:53:42 +00:00
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
set_last_save_timestamp
fi
}
2015-02-11 14:43:37 +00:00
add_resurrect_save_interpolation() {
local status_right_value="$(get_tmux_option "status-right" "")"
# check interpolation not already added
if ! [[ "$status_right_value" == *"$save_command_interpolation"* ]]; then
local new_value="${save_command_interpolation}${status_right_value}"
set_tmux_option "status-right" "$new_value"
fi
2015-02-11 14:43:37 +00:00
}
number_of_sessions() {
tmux list-sessions |
wc -l |
sed "s/ //g"
}
# when tmux server is first started, number of sessions is 0
2015-02-15 13:36:06 +00:00
just_started_tmux_server() {
[ "$(number_of_sessions)" -eq 0 ]
2015-02-15 13:36:06 +00:00
}
start_auto_restore_in_background() {
2015-02-20 14:48:36 +00:00
"$CURRENT_DIR/scripts/continuum_restore.sh" &
2015-02-15 13:36:06 +00:00
}
2015-11-29 17:28:51 +00:00
update_tmux_option() {
local option="$1"
local option_value="$(get_tmux_option "$option")"
# replace interpolation string with a script to execute
local new_option_value="${option_value/$status_interpolation_string/$status_script}"
set_tmux_option "$option" "$new_option_value"
}
2015-02-11 14:43:37 +00:00
main() {
2015-02-12 02:37:34 +00:00
if supported_tmux_version_ok; then
handle_tmux_automatic_start
# 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
2015-02-15 13:36:06 +00:00
if just_started_tmux_server; then
start_auto_restore_in_background
fi
2015-11-29 17:28:51 +00:00
# Put "#{continuum_status}" interpolation in status-right or
# status-left tmux option to get current tmux continuum status.
update_tmux_option "status-right"
update_tmux_option "status-left"
2015-02-12 02:37:34 +00:00
fi
2015-02-11 14:43:37 +00:00
}
main