10 Commits

Author SHA1 Message Date
2edd3ff98d v0.0.4 2014-08-26 20:58:38 +02:00
bcad7cd1ea Bugfix: correct pane ordering in window 2014-08-26 20:57:53 +02:00
4d5c0a2a0d Improve active/alternate window restoring 2014-08-26 20:29:55 +02:00
8051fb9d36 Restore pane layout for each window
Close #2
2014-08-26 20:19:34 +02:00
a649614a20 v0.0.3 2014-08-26 19:18:24 +02:00
8166fa2602 Restore active and alternate window for each session
Closes #12
2014-08-26 19:16:51 +02:00
ecc42c5a56 Save and restore active pane
Active pane is restored for each window with multiple panes.

Closes #5
2014-08-26 18:54:39 +02:00
aa8f323b8b Improved handling of fields that can be empty 2014-08-26 17:51:56 +02:00
c78a38803a Bugfix: non-existing window names
Fixes #11
2014-08-26 17:28:40 +02:00
877780eb02 Save and restore current and alternate session
Closes #6
2014-08-26 17:27:46 +02:00
3 changed files with 125 additions and 9 deletions

View File

@ -2,6 +2,16 @@
### master
### v0.0.4, 2014-08-26
- restore pane layout for each window
- bugfix: correct pane ordering in a window
### v0.0.3, 2014-08-26
- save and restore current and alternate session
- fix a bug with non-existing window names
- restore active pane for each window that has multiple panes
- restore active and alternate window for each session
### v0.0.2, 2014-08-26
- saving a new session does not remove the previous one
- make the directory where sessions are stored configurable

View File

@ -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
@ -32,6 +39,10 @@ tmux_socket() {
echo $TMUX | cut -d',' -f1
}
remove_first_char() {
echo "$1" | cut -c2-
}
new_window() {
local session_name="$1"
local window_number="$2"
@ -58,13 +69,14 @@ new_pane() {
local window_number="$2"
local window_name="$3"
local dir="$4"
tmux split-window -d -t "${session_name}:${window_number}" -c "$dir"
tmux split-window -t "${session_name}:${window_number}" -c "$dir"
}
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 window_active window_flags pane_index dir pane_active; do
window_name="$(remove_first_char $window_name)"
if window_exists "$session_name" "$window_number"; then
new_pane "$session_name" "$window_number" "$window_name" "$dir"
elif session_exists "$session_name"; then
@ -75,17 +87,63 @@ 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"
fi
done < $(last_session_path)
}
restore_pane_layout_for_each_window() {
\grep '^window' $(last_session_path) |
while IFS=$'\t' read line_type session_name window_number window_active window_flags window_layout; do
tmux select-layout -t "${session_name}:${window_number}" "$window_layout"
done
}
restore_active_pane_for_each_window() {
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $7 != 0 && $9 == 1 { print $2, $3, $7; }' $(last_session_path) |
while IFS=$'\t' read session_name window_number active_pane; do
tmux switch-client -t "${session_name}:${window_number}"
tmux select-pane -t "$active_pane"
done
}
restore_active_and_alternate_windows() {
awk 'BEGIN { FS="\t"; OFS="\t" } /^window/ && $5 ~ /[*-]/ { print $2, $4, $3; }' $(last_session_path) |
sort -u |
while IFS=$'\t' read session_name active_window window_number; do
tmux switch-client -t "${session_name}:${window_number}"
done
}
restore_active_and_alternate_sessions() {
while read line; do
if is_line_type "state" "$line"; then
restore_state "$line"
fi
done < $(last_session_path)
display_message "Restored all Tmux sessions!"
}
main() {
if supported_tmux_version_ok; then
check_saved_session_exists
restore_all_sessions
restore_pane_layout_for_each_window
restore_active_pane_for_each_window
restore_active_and_alternate_windows
restore_active_and_alternate_sessions
display_message "Restored all Tmux sessions!"
fi
}
main

View File

@ -4,27 +4,75 @@ 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}"
format+="${delimiter}"
format+="#{window_name}"
format+=":#{window_name}"
format+="${delimiter}"
format+="#{window_active}"
format+="${delimiter}"
format+=":#{window_flags}"
format+="${delimiter}"
format+="#{pane_index}"
format+="${delimiter}"
format+="#{pane_current_path}"
format+="${delimiter}"
format+="#{pane_active}"
echo "$format"
}
dump() {
tmux list-panes -a -F "$(dump_format)"
window_format() {
local delimiter=$'\t'
local format
format+="window"
format+="${delimiter}"
format+="#{session_name}"
format+="${delimiter}"
format+="#{window_index}"
format+="${delimiter}"
format+="#{window_active}"
format+="${delimiter}"
format+=":#{window_flags}"
format+="${delimiter}"
format+="#{window_layout}"
echo "$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_windows() {
tmux list-windows -a -F "$(window_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_windows >> $session_path
dump_state >> $session_path
ln -fs "$session_path" "$(last_session_path)"
display_message "Saved all Tmux sessions!"
}