Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
2edd3ff98d | |||
bcad7cd1ea | |||
4d5c0a2a0d | |||
8051fb9d36 | |||
a649614a20 | |||
8166fa2602 | |||
ecc42c5a56 | |||
aa8f323b8b | |||
c78a38803a | |||
877780eb02 |
10
CHANGELOG.md
10
CHANGELOG.md
@ -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
|
||||
|
@ -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
|
||||
|
@ -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!"
|
||||
}
|
||||
|
Reference in New Issue
Block a user