First working version

This commit is contained in:
Bruno Sutic
2015-02-11 15:43:37 +01:00
commit 42ca2d4454
9 changed files with 157 additions and 0 deletions

33
scripts/check_tmux_version.sh Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env bash
VERSION="$1"
UNSUPPORTED_MSG="$2"
# this is used to get "clean" integer version number. Examples:
# `tmux 1.9` => `19`
# `1.9a` => `19`
get_digits_from_string() {
local string="$1"
local only_digits="$(echo "$string" | tr -dC '[:digit:]')"
echo "$only_digits"
}
tmux_version_int() {
local tmux_version_string=$(tmux -V)
echo "$(get_digits_from_string "$tmux_version_string")"
}
exit_if_unsupported_version() {
local current_version="$1"
local supported_version="$2"
if [ "$current_version" -lt "$supported_version" ]; then
exit 1
fi
}
main() {
local supported_version_int="$(get_digits_from_string "$VERSION")"
local current_version_int="$(tmux_version_int)"
exit_if_unsupported_version "$current_version_int" "$supported_version_int"
}
main

16
scripts/helpers.sh Normal file
View File

@ -0,0 +1,16 @@
get_tmux_option() {
local option="$1"
local default_value="$2"
local option_value=$(tmux show-option -gqv "$option")
if [ -z "$option_value" ]; then
echo "$default_value"
else
echo "$option_value"
fi
}
set_tmux_option() {
local option="$1"
local value="$2"
tmux set-option -gq "$option" "$value"
}

41
scripts/resurrect_auto_save.sh Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/variables.sh"
supported_tmux_version_ok() {
$CURRENT_DIR/check_tmux_version.sh "$SUPPORTED_VERSION"
}
current_timestamp() {
echo "$(date +%s)"
}
set_last_save_timestamp() {
set_tmux_option "$last_auto_save_option" "$(current_timestamp)"
}
enough_time_since_last_run_passed() {
local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "0")"
local interval_minutes="$(get_tmux_option "$auto_save_interval_option" "$auto_save_interval_default")"
local interval_seconds="$((interval_minutes * 60))"
local next_run="$((last_saved_timestamp + $interval_seconds))"
[ "$(current_timestamp)" -ge "$next_run" ]
}
fetch_and_run_tmux_resurrect_save_script() {
local resurrect_save_script_path="$(get_tmux_option "$resurrect_save_path_option" "")"
if [ -n "$resurrect_save_script_path" ]; then
$resurrect_save_script_path "no-spinner"
set_last_save_timestamp
fi
}
main() {
if supported_tmux_version_ok && enough_time_since_last_run_passed; then
fetch_and_run_tmux_resurrect_save_script
fi
}
main

11
scripts/variables.sh Normal file
View File

@ -0,0 +1,11 @@
SUPPORTED_VERSION="1.9"
# these tmux options contain paths to tmux resurrect save and restore scripts
resurrect_save_path_option="@resurrect-save-script-path"
resurrect_restore_path_option="@resurrect-restore-script-path"
auto_save_interval_default="15"
auto_save_interval_option="@resurrect-auto-save-interval"
# time when the tmux environment was last saved (unix timestamp)
last_auto_save_option="@resurrect-auto-save-last-timestamp"