mirror of
				https://github.com/tmux-plugins/tmux-resurrect.git
				synced 2025-11-04 08:56:03 +00:00 
			
		
		
		
	@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
		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!"
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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!"
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user