Save and restore current and alternate session

Closes #6
This commit is contained in:
Bruno Sutic 2014-08-26 17:23:20 +02:00
parent 8ba7d6d873
commit 877780eb02
No known key found for this signature in database
GPG Key ID: 66D96E4F2F7EF26C
3 changed files with 45 additions and 6 deletions

View File

@ -1,6 +1,7 @@
# Changelog # Changelog
### master ### master
- save and restore current and alternate session
### v0.0.2, 2014-08-26 ### v0.0.2, 2014-08-26
- saving a new session does not remove the previous one - saving a new session does not remove the previous one

View File

@ -4,6 +4,13 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh" 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() { check_saved_session_exists() {
local saved_session="$(last_session_path)" local saved_session="$(last_session_path)"
if [ ! -f $saved_session ]; then if [ ! -f $saved_session ]; then
@ -64,7 +71,7 @@ new_pane() {
restore_pane() { restore_pane() {
local pane="$1" local pane="$1"
echo "$pane" | 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 if window_exists "$session_name" "$window_number"; then
new_pane "$session_name" "$window_number" "$window_name" "$dir" new_pane "$session_name" "$window_number" "$window_name" "$dir"
elif session_exists "$session_name"; then elif session_exists "$session_name"; then
@ -75,9 +82,22 @@ restore_pane() {
done 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() { restore_all_sessions() {
while read line; do while read line; do
if is_line_type "pane" "$line"; then
restore_pane "$line" restore_pane "$line"
elif is_line_type "state" "$line"; then
restore_state "$line"
fi
done < $(last_session_path) done < $(last_session_path)
display_message "Restored all Tmux sessions!" display_message "Restored all Tmux sessions!"
} }

View File

@ -4,9 +4,11 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh" source "$CURRENT_DIR/helpers.sh"
dump_format() { pane_format() {
local delimiter=$'\t' local delimiter=$'\t'
local format local format
format+="pane"
format+="${delimiter}"
format+="#{session_name}" format+="#{session_name}"
format+="${delimiter}" format+="${delimiter}"
format+="#{window_index}" format+="#{window_index}"
@ -17,14 +19,30 @@ dump_format() {
echo "$format" echo "$format"
} }
dump() { state_format() {
tmux list-panes -a -F "$(dump_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() { save_all_sessions() {
local session_path="$(session_path)" local session_path="$(session_path)"
mkdir -p "$(sessions_dir)" mkdir -p "$(sessions_dir)"
dump > $session_path dump_panes > $session_path
dump_state >> $session_path
ln -fs "$session_path" "$(last_session_path)" ln -fs "$session_path" "$(last_session_path)"
display_message "Saved all Tmux sessions!" display_message "Saved all Tmux sessions!"
} }