mirror of
				https://github.com/tmux-plugins/tmux-resurrect.git
				synced 2025-11-04 08:56:03 +00:00 
			
		
		
		
	Merge pull request #79 from laomaiweng/resurrect-pane-contents
Save and restore tmux pane contents
This commit is contained in:
		@@ -51,6 +51,11 @@ remove_first_char() {
 | 
				
			|||||||
	echo "$1" | cut -c2-
 | 
						echo "$1" | cut -c2-
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					capture_pane_contents_option_on() {
 | 
				
			||||||
 | 
						local option="$(get_tmux_option "$pane_contents_option" "off")"
 | 
				
			||||||
 | 
						[ "$option" == "on" ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
save_bash_history_option_on() {
 | 
					save_bash_history_option_on() {
 | 
				
			||||||
	local option="$(get_tmux_option "$bash_history_option" "off")"
 | 
						local option="$(get_tmux_option "$bash_history_option" "off")"
 | 
				
			||||||
	[ "$option" == "on" ]
 | 
						[ "$option" == "on" ]
 | 
				
			||||||
@@ -81,6 +86,11 @@ last_resurrect_file() {
 | 
				
			|||||||
	echo "$(resurrect_dir)/last"
 | 
						echo "$(resurrect_dir)/last"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					resurrect_pane_file() {
 | 
				
			||||||
 | 
						local pane_id="$1"
 | 
				
			||||||
 | 
						echo "$(resurrect_dir)/pane_contents-${pane_id}"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
resurrect_history_file() {
 | 
					resurrect_history_file() {
 | 
				
			||||||
	local pane_id="$1"
 | 
						local pane_id="$1"
 | 
				
			||||||
	echo "$(resurrect_dir)/bash_history-${pane_id}"
 | 
						echo "$(resurrect_dir)/bash_history-${pane_id}"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -210,6 +210,16 @@ restore_pane_layout_for_each_window() {
 | 
				
			|||||||
		done
 | 
							done
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					restore_pane_contents() {
 | 
				
			||||||
 | 
						awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $7, $10; }' $(last_resurrect_file) |
 | 
				
			||||||
 | 
							while IFS=$d read session_name window_number pane_index pane_command; do
 | 
				
			||||||
 | 
								if ! is_pane_registered_as_existing "$session_name" "$window_number" "$pane_index"; then
 | 
				
			||||||
 | 
									local pane_id="$session_name:$window_number.$pane_index"
 | 
				
			||||||
 | 
									local read_command=" cat '$(resurrect_pane_file "$pane_id")'"
 | 
				
			||||||
 | 
									tmux send-keys -t "$pane_id" "$read_command" C-m
 | 
				
			||||||
 | 
								fi
 | 
				
			||||||
 | 
							done
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
restore_shell_history() {
 | 
					restore_shell_history() {
 | 
				
			||||||
	awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $7, $10; }' $(last_resurrect_file) |
 | 
						awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $7, $10; }' $(last_resurrect_file) |
 | 
				
			||||||
		while IFS=$d read session_name window_number pane_index pane_command; do
 | 
							while IFS=$d read session_name window_number pane_index pane_command; do
 | 
				
			||||||
@@ -285,6 +295,9 @@ main() {
 | 
				
			|||||||
		if save_bash_history_option_on; then
 | 
							if save_bash_history_option_on; then
 | 
				
			||||||
			restore_shell_history
 | 
								restore_shell_history
 | 
				
			||||||
		fi
 | 
							fi
 | 
				
			||||||
 | 
							if capture_pane_contents_option_on; then
 | 
				
			||||||
 | 
								restore_pane_contents
 | 
				
			||||||
 | 
							fi
 | 
				
			||||||
		restore_all_pane_processes
 | 
							restore_all_pane_processes
 | 
				
			||||||
		# below functions restore exact cursor positions
 | 
							# below functions restore exact cursor positions
 | 
				
			||||||
		restore_active_pane_for_each_window
 | 
							restore_active_pane_for_each_window
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -78,7 +78,8 @@ state_format() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
dump_panes_raw() {
 | 
					dump_panes_raw() {
 | 
				
			||||||
	tmux list-panes -a -F "$(pane_format)"
 | 
						local format="${1:-$(pane_format)}"
 | 
				
			||||||
 | 
						tmux list-panes -a -F "$format"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
dump_windows_raw(){
 | 
					dump_windows_raw(){
 | 
				
			||||||
@@ -108,6 +109,13 @@ pane_full_command() {
 | 
				
			|||||||
	$strategy_file "$pane_pid"
 | 
						$strategy_file "$pane_pid"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					capture_pane_contents() {
 | 
				
			||||||
 | 
						local pane_id="$1"
 | 
				
			||||||
 | 
						local start_line="-$2"
 | 
				
			||||||
 | 
						[[ "$(get_tmux_option "$pane_contents_area_option" "full")" == "visible" ]] && start_line="0"
 | 
				
			||||||
 | 
						tmux capture-pane -epJ -S "$start_line" -t "$pane_id" > "$(resurrect_pane_file "$pane_id")"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
save_shell_history() {
 | 
					save_shell_history() {
 | 
				
			||||||
	local pane_id="$1"
 | 
						local pane_id="$1"
 | 
				
			||||||
	local pane_command="$2"
 | 
						local pane_command="$2"
 | 
				
			||||||
@@ -201,6 +209,13 @@ dump_state() {
 | 
				
			|||||||
	tmux display-message -p "$(state_format)"
 | 
						tmux display-message -p "$(state_format)"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					dump_pane_contents() {
 | 
				
			||||||
 | 
						paste -d"$d" <(dump_panes) <(dump_panes_raw "#{history_size}") |
 | 
				
			||||||
 | 
							while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command full_command history_size; do
 | 
				
			||||||
 | 
								capture_pane_contents "$session_name:$window_number.$pane_index" "$history_size"
 | 
				
			||||||
 | 
							done
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
dump_bash_history() {
 | 
					dump_bash_history() {
 | 
				
			||||||
	dump_panes |
 | 
						dump_panes |
 | 
				
			||||||
		while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command full_command; do
 | 
							while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command full_command; do
 | 
				
			||||||
@@ -216,6 +231,9 @@ save_all() {
 | 
				
			|||||||
	dump_windows >> "$resurrect_file_path"
 | 
						dump_windows >> "$resurrect_file_path"
 | 
				
			||||||
	dump_state   >> "$resurrect_file_path"
 | 
						dump_state   >> "$resurrect_file_path"
 | 
				
			||||||
	ln -fs "$(basename "$resurrect_file_path")" "$(last_resurrect_file)"
 | 
						ln -fs "$(basename "$resurrect_file_path")" "$(last_resurrect_file)"
 | 
				
			||||||
 | 
						if capture_pane_contents_option_on; then
 | 
				
			||||||
 | 
							dump_pane_contents
 | 
				
			||||||
 | 
						fi
 | 
				
			||||||
	if save_bash_history_option_on; then
 | 
						if save_bash_history_option_on; then
 | 
				
			||||||
		dump_bash_history
 | 
							dump_bash_history
 | 
				
			||||||
	fi
 | 
						fi
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,6 +29,13 @@ inline_strategy_token="->"
 | 
				
			|||||||
save_command_strategy_option="@resurrect-save-command-strategy"
 | 
					save_command_strategy_option="@resurrect-save-command-strategy"
 | 
				
			||||||
default_save_command_strategy="ps"
 | 
					default_save_command_strategy="ps"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Pane contents capture options.
 | 
				
			||||||
 | 
					# @resurrect-pane-contents-area option can be:
 | 
				
			||||||
 | 
					#   'visible' - capture only the visible pane area (default)
 | 
				
			||||||
 | 
					#   'full'    - capture the full pane contents
 | 
				
			||||||
 | 
					pane_contents_option="@resurrect-capture-pane-contents"
 | 
				
			||||||
 | 
					pane_contents_area_option="@resurrect-pane-contents-area"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bash_history_option="@resurrect-save-bash-history"
 | 
					bash_history_option="@resurrect-save-bash-history"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# set to 'on' to ensure panes are never ever overwritten
 | 
					# set to 'on' to ensure panes are never ever overwritten
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user