From 42ca2d44543421744f572c8ef98e559b2a21344a Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Wed, 11 Feb 2015 15:43:37 +0100 Subject: [PATCH] First working version --- CHANGELOG.md | 4 ++++ CONTRIBUTING.md | 15 +++++++++++++ LICENSE.md | 19 ++++++++++++++++ README.md | 0 resurrect_auto.tmux | 18 +++++++++++++++ scripts/check_tmux_version.sh | 33 +++++++++++++++++++++++++++ scripts/helpers.sh | 16 +++++++++++++ scripts/resurrect_auto_save.sh | 41 ++++++++++++++++++++++++++++++++++ scripts/variables.sh | 11 +++++++++ 9 files changed, 157 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100755 resurrect_auto.tmux create mode 100755 scripts/check_tmux_version.sh create mode 100644 scripts/helpers.sh create mode 100755 scripts/resurrect_auto_save.sh create mode 100644 scripts/variables.sh diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..92f5d9d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +# Changelog + +### master +- first working version diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..21c18fd --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,15 @@ +### Contributing + +Code contributions are welcome! + +If you wanna contribute a bigger feature, please open a github issue so we can +discuss it together first. + +### Reporting a bug + +If you find a bug please report it in the issues. When reporting a bug please +attach: +- a file symlinked to `~/.tmux/resurrect/last`. +- your `.tmux.conf` +- if you're getting an error paste it to a [gist](https://gist.github.com/) and + link it in the issue diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e6e7350 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,19 @@ +Copyright (C) Bruno Sutic + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/resurrect_auto.tmux b/resurrect_auto.tmux new file mode 100755 index 0000000..fb181e6 --- /dev/null +++ b/resurrect_auto.tmux @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/scripts/helpers.sh" + +save_command_interpolation="#($CURRENT_DIR/scripts/resurrect_auto_save.sh)" + +add_resurrect_save_interpolation() { + local status_right_value="$(get_tmux_option "status-right" "")" + local new_value="${save_command_interpolation}${status_right_value}" + set_tmux_option "status-right" "$new_value" +} + +main() { + add_resurrect_save_interpolation +} +main diff --git a/scripts/check_tmux_version.sh b/scripts/check_tmux_version.sh new file mode 100755 index 0000000..f4a887a --- /dev/null +++ b/scripts/check_tmux_version.sh @@ -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 diff --git a/scripts/helpers.sh b/scripts/helpers.sh new file mode 100644 index 0000000..206eac1 --- /dev/null +++ b/scripts/helpers.sh @@ -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" +} diff --git a/scripts/resurrect_auto_save.sh b/scripts/resurrect_auto_save.sh new file mode 100755 index 0000000..f20938f --- /dev/null +++ b/scripts/resurrect_auto_save.sh @@ -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 diff --git a/scripts/variables.sh b/scripts/variables.sh new file mode 100644 index 0000000..28840c3 --- /dev/null +++ b/scripts/variables.sh @@ -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"