From 61ff39584a6ac889d4b91cab2d74a79c3e26e9d3 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Tue, 17 Feb 2015 17:13:21 +0100 Subject: [PATCH] Enable easy 'tmux auto start' configuration --- CHANGELOG.md | 1 + README.md | 26 ++++++++++++--- resurrect_auto.tmux | 6 ++++ scripts/handle_tmux_automatic_start.sh | 28 ++++++++++++++++ .../osx_disable.sh | 10 ++++++ .../handle_tmux_automatic_start/osx_enable.sh | 33 +++++++++++++++++++ .../osx_iterm_start_tmux.sh | 4 +-- .../osx_terminal_start_tmux.sh | 4 +-- scripts/variables.sh | 7 ++++ 9 files changed, 110 insertions(+), 9 deletions(-) create mode 100755 scripts/handle_tmux_automatic_start.sh create mode 100755 scripts/handle_tmux_automatic_start/osx_disable.sh create mode 100755 scripts/handle_tmux_automatic_start/osx_enable.sh rename {system_startup_scripts => scripts/handle_tmux_automatic_start}/osx_iterm_start_tmux.sh (88%) rename {system_startup_scripts => scripts/handle_tmux_automatic_start}/osx_terminal_start_tmux.sh (86%) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca0ab42..982c780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ### master +- enable "tmux auto start" for OS X ### v2.0.0, 2015-02-15 - enable automatic environment restore when tmux is started diff --git a/README.md b/README.md index 707ee0d..ea8c95e 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,32 @@ # tmux-resurrect-auto -Continuous saving of tmux environment. Automatic restore when tmux is started. +Features: + +- continuous saving of tmux environment +- automatic tmux start when computer/server is on +- automatic restore when tmux is started + +These features enable uninterrupted tmux usage. No matter the computer or server +restarts, if the machine is on, tmux will be there how you left it off the last +time it was used. #### Continuous saving -After this plugin is installed, `tmux-resurrect` will save environment at the -interval of 15 minutes. All the saving happens in the background without the -impact to your workflow. +Tmux environment will be saved at the interval of 15 minutes. All the saving +happens in the background without the impact to your workflow. + +This action starts automatically when the plugin is installed. + +#### Automatic tmux start + +Tmux is automatically started after the computer/server is turned on. + +Put `set -g @resurrect-auto-tmux-start 'on'` in `tmux.conf` to enable this. #### Automatic restore -Last saved environment is automatically restored when tmux server is started. +Last saved environment is automatically restored when tmux is started. + Put `set -g @resurrect-auto-restore 'on'` in `tmux.conf` to enable this. Note: automatic restore happens **exclusively** on tmux server start. No other diff --git a/resurrect_auto.tmux b/resurrect_auto.tmux index 9a8cb8f..9f508e3 100755 --- a/resurrect_auto.tmux +++ b/resurrect_auto.tmux @@ -12,6 +12,10 @@ supported_tmux_version_ok() { $CURRENT_DIR/scripts/check_tmux_version.sh "$SUPPORTED_VERSION" } +handle_tmux_automatic_start() { + $CURRENT_DIR/scripts/handle_tmux_automatic_start.sh +} + add_resurrect_save_interpolation() { local status_right_value="$(get_tmux_option "status-right" "")" local new_value="${save_command_interpolation}${status_right_value}" @@ -31,6 +35,8 @@ start_auto_restore_in_background() { main() { if supported_tmux_version_ok; then + handle_tmux_automatic_start + # Don't start saving right after tmux is started. # We wanna give user a chance to restore previous session. set_last_save_timestamp diff --git a/scripts/handle_tmux_automatic_start.sh b/scripts/handle_tmux_automatic_start.sh new file mode 100755 index 0000000..5697aa5 --- /dev/null +++ b/scripts/handle_tmux_automatic_start.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/helpers.sh" +source "$CURRENT_DIR/variables.sh" + +is_tmux_automatic_start_enabled() { + local auto_start_value="$(get_tmux_option "$auto_start_option" "$auto_start_default")" + [ "$auto_start_value" == "on" ] +} + +is_osx() { + [ $(uname) == "Darwin" ] +} + +main() { + if is_tmux_automatic_start_enabled; then + if is_osx; then + $CURRENT_DIR/handle_tmux_automatic_start/osx_enable.sh + fi + else + if is_osx; then + $CURRENT_DIR/handle_tmux_automatic_start/osx_disable.sh + fi + fi +} +main diff --git a/scripts/handle_tmux_automatic_start/osx_disable.sh b/scripts/handle_tmux_automatic_start/osx_disable.sh new file mode 100755 index 0000000..19f9af1 --- /dev/null +++ b/scripts/handle_tmux_automatic_start/osx_disable.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/../variables.sh" + +main() { + rm "$osx_auto_start_file_path" > /dev/null 2>&1 +} +main diff --git a/scripts/handle_tmux_automatic_start/osx_enable.sh b/scripts/handle_tmux_automatic_start/osx_enable.sh new file mode 100755 index 0000000..370ccfe --- /dev/null +++ b/scripts/handle_tmux_automatic_start/osx_enable.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/../variables.sh" + +template() { + local tmux_start_script="$1" + local content + read -r -d '' content <<-EOF + + + + + Label + ${osx_auto_start_file_name} + ProgramArguments + + ${tmux_start_script} + + RunAtLoad + + + + EOF + echo "$content" +} + +main() { + local launchd_plist_file_content="$(template "$CURRENT_DIR/osx_terminal_start_tmux.sh")" + echo "$launchd_plist_file_content" > "$osx_auto_start_file_path" +} +main diff --git a/system_startup_scripts/osx_iterm_start_tmux.sh b/scripts/handle_tmux_automatic_start/osx_iterm_start_tmux.sh similarity index 88% rename from system_startup_scripts/osx_iterm_start_tmux.sh rename to scripts/handle_tmux_automatic_start/osx_iterm_start_tmux.sh index 56bf719..5d3f110 100755 --- a/system_startup_scripts/osx_iterm_start_tmux.sh +++ b/scripts/handle_tmux_automatic_start/osx_iterm_start_tmux.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# for "true full screen" call the script with "true" as the first argument +# for "true full screen" call the script with "fullscreen" as the first argument TRUE_FULL_SCREEN="$1" start_iterm_and_run_tmux() { @@ -52,7 +52,7 @@ resize_to_true_full_screen() { main() { start_iterm_and_run_tmux - if [ "$TRUE_FULL_SCREEN" == "true" ]; then + if [ "$TRUE_FULL_SCREEN" == "fullscreen" ]; then resize_to_true_full_screen else resize_window_to_full_screen diff --git a/system_startup_scripts/osx_terminal_start_tmux.sh b/scripts/handle_tmux_automatic_start/osx_terminal_start_tmux.sh similarity index 86% rename from system_startup_scripts/osx_terminal_start_tmux.sh rename to scripts/handle_tmux_automatic_start/osx_terminal_start_tmux.sh index ba9ab46..8e1da71 100755 --- a/system_startup_scripts/osx_terminal_start_tmux.sh +++ b/scripts/handle_tmux_automatic_start/osx_terminal_start_tmux.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# for "true full screen" call the script with "true" as the first argument +# for "true full screen" call the script with "fullscreen" as the first argument TRUE_FULL_SCREEN="$1" start_terminal_and_run_tmux() { @@ -39,7 +39,7 @@ resize_to_true_full_screen() { main() { start_terminal_and_run_tmux - if [ "$TRUE_FULL_SCREEN" == "true" ]; then + if [ "$TRUE_FULL_SCREEN" == "fullscreen" ]; then resize_to_true_full_screen else resize_window_to_full_screen diff --git a/scripts/variables.sh b/scripts/variables.sh index e88f872..59d8a91 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -14,3 +14,10 @@ auto_restore_option="@resurrect-auto-restore" auto_restore_default="off" auto_restore_halt_file="${HOME}/tmux_no_auto_restore" + +# tmux auto start options +auto_start_option="@resurrect-auto-tmux-start" +auto_start_default="off" + +osx_auto_start_file_name="Tmux.Start.plist" +osx_auto_start_file_path="${HOME}/Library/LaunchAgents/${osx_auto_start_file_name}"