From 877780eb02b9020d7df393918dc0c565beb0a62f Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Tue, 26 Aug 2014 17:23:20 +0200 Subject: [PATCH] Save and restore current and alternate session Closes #6 --- CHANGELOG.md | 1 + scripts/session_restorer.sh | 24 ++++++++++++++++++++++-- scripts/session_saver.sh | 26 ++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 553f8ed..a2a1a5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ### master +- save and restore current and alternate session ### v0.0.2, 2014-08-26 - saving a new session does not remove the previous one diff --git a/scripts/session_restorer.sh b/scripts/session_restorer.sh index 9ab6bb9..bda9db0 100755 --- a/scripts/session_restorer.sh +++ b/scripts/session_restorer.sh @@ -4,6 +4,13 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" +is_line_type() { + local line_type="$1" + local line="$2" + echo "$line" | + \grep -q "^$line_type" +} + check_saved_session_exists() { local saved_session="$(last_session_path)" if [ ! -f $saved_session ]; then @@ -64,7 +71,7 @@ new_pane() { restore_pane() { local pane="$1" echo "$pane" | - while IFS=$'\t' read session_name window_number window_name dir; do + while IFS=$'\t' read line_type session_name window_number window_name dir; do if window_exists "$session_name" "$window_number"; then new_pane "$session_name" "$window_number" "$window_name" "$dir" elif session_exists "$session_name"; then @@ -75,9 +82,22 @@ restore_pane() { done } +restore_state() { + local state="$1" + echo "$state" | + while IFS=$'\t' read line_type client_session client_last_session; do + tmux switch-client -t "$client_last_session" + tmux switch-client -t "$client_session" + done +} + restore_all_sessions() { while read line; do - restore_pane "$line" + if is_line_type "pane" "$line"; then + restore_pane "$line" + elif is_line_type "state" "$line"; then + restore_state "$line" + fi done < $(last_session_path) display_message "Restored all Tmux sessions!" } diff --git a/scripts/session_saver.sh b/scripts/session_saver.sh index c3fa865..37563fe 100755 --- a/scripts/session_saver.sh +++ b/scripts/session_saver.sh @@ -4,9 +4,11 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" -dump_format() { +pane_format() { local delimiter=$'\t' local format + format+="pane" + format+="${delimiter}" format+="#{session_name}" format+="${delimiter}" format+="#{window_index}" @@ -17,14 +19,30 @@ dump_format() { echo "$format" } -dump() { - tmux list-panes -a -F "$(dump_format)" +state_format() { + local delimiter=$'\t' + local format + format+="state" + format+="${delimiter}" + format+="#{client_session}" + format+="${delimiter}" + format+="#{client_last_session}" + echo "$format" +} + +dump_panes() { + tmux list-panes -a -F "$(pane_format)" +} + +dump_state() { + tmux display-message -p "$(state_format)" } save_all_sessions() { local session_path="$(session_path)" mkdir -p "$(sessions_dir)" - dump > $session_path + dump_panes > $session_path + dump_state >> $session_path ln -fs "$session_path" "$(last_session_path)" display_message "Saved all Tmux sessions!" }