From b22b2a720379972d7ff8a85b76d76566e4a3a3d6 Mon Sep 17 00:00:00 2001 From: quentin Date: Tue, 17 Mar 2015 03:17:37 +0100 Subject: [PATCH 1/4] Save and restore tmux pane contents This feature is controlled by the '@resurrect-capture-pane-contents' option. Currently only the visible area of each pane is saved and restored. --- scripts/helpers.sh | 10 ++++++++++ scripts/restore.sh | 15 ++++++++++++++- scripts/save.sh | 15 +++++++++++++++ scripts/variables.sh | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 6b654af..64e45a9 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -51,6 +51,11 @@ remove_first_char() { 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() { local option="$(get_tmux_option "$bash_history_option" "off")" [ "$option" == "on" ] @@ -81,6 +86,11 @@ last_resurrect_file() { echo "$(resurrect_dir)/last" } +resurrect_pane_file() { + local pane_id="$1" + echo "$(resurrect_dir)/pane_contents-${pane_id}" +} + resurrect_history_file() { local pane_id="$1" echo "$(resurrect_dir)/bash_history-${pane_id}" diff --git a/scripts/restore.sh b/scripts/restore.sh index 495e286..30466dc 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -210,10 +210,20 @@ restore_pane_layout_for_each_window() { 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() { 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 + if ! is_pane_registered_as_existing "$session_name" "$window_number" "$pane_index"; then if [ "$pane_command" = "bash" ]; then local pane_id="$session_name:$window_number.$pane_index" # tmux send-keys has -R option that should reset the terminal. @@ -285,6 +295,9 @@ main() { if save_bash_history_option_on; then restore_shell_history fi + if capture_pane_contents_option_on; then + restore_pane_contents + fi restore_all_pane_processes # below functions restore exact cursor positions restore_active_pane_for_each_window diff --git a/scripts/save.sh b/scripts/save.sh index 45ec621..d33f9f3 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -108,6 +108,11 @@ pane_full_command() { $strategy_file "$pane_pid" } +capture_pane_contents() { + local pane_id="$1" + tmux capture-pane -ep -t "$pane_id" > "$(resurrect_pane_file "$pane_id")" +} + save_shell_history() { local pane_id="$1" local pane_command="$2" @@ -201,6 +206,13 @@ dump_state() { tmux display-message -p "$(state_format)" } +dump_pane_contents() { + 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 + capture_pane_contents "$session_name:$window_number.$pane_index" + done +} + dump_bash_history() { 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 @@ -216,6 +228,9 @@ save_all() { dump_windows >> "$resurrect_file_path" dump_state >> "$resurrect_file_path" 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 dump_bash_history fi diff --git a/scripts/variables.sh b/scripts/variables.sh index ee047f2..c1e15ef 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -29,6 +29,7 @@ inline_strategy_token="->" save_command_strategy_option="@resurrect-save-command-strategy" default_save_command_strategy="ps" +pane_contents_option="@resurrect-capture-pane-contents" bash_history_option="@resurrect-save-bash-history" # set to 'on' to ensure panes are never ever overwritten From 4f685d5c3df2547a4b1e4b2f633a562e8ae258b8 Mon Sep 17 00:00:00 2001 From: quentin Date: Tue, 17 Mar 2015 04:13:54 +0100 Subject: [PATCH 2/4] Add an option to save the full pane contents By default only the visible pane area is captured and restored. The @resurrect-pane-contents-area option lets the full pane area be captured instead. --- scripts/save.sh | 13 ++++++++----- scripts/variables.sh | 6 ++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/scripts/save.sh b/scripts/save.sh index d33f9f3..327a352 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -78,7 +78,8 @@ state_format() { } dump_panes_raw() { - tmux list-panes -a -F "$(pane_format)" + local format="${1:-$(pane_format)}" + tmux list-panes -a -F "$format" } dump_windows_raw(){ @@ -110,7 +111,9 @@ pane_full_command() { capture_pane_contents() { local pane_id="$1" - tmux capture-pane -ep -t "$pane_id" > "$(resurrect_pane_file "$pane_id")" + local start_line="0" + [[ "$(get_tmux_option "$pane_contents_area_option" "visible")" == "full" ]] && start_line="-$2" + tmux capture-pane -ep -S "$start_line" -t "$pane_id" > "$(resurrect_pane_file "$pane_id")" } save_shell_history() { @@ -207,9 +210,9 @@ dump_state() { } dump_pane_contents() { - 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 - capture_pane_contents "$session_name:$window_number.$pane_index" + 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 } diff --git a/scripts/variables.sh b/scripts/variables.sh index c1e15ef..f5fabb9 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -29,7 +29,13 @@ inline_strategy_token="->" save_command_strategy_option="@resurrect-save-command-strategy" 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" # set to 'on' to ensure panes are never ever overwritten From a1e3d37461db2211ad38cbe87f7a23041095b1a9 Mon Sep 17 00:00:00 2001 From: quentin Date: Wed, 18 Mar 2015 10:14:20 +0100 Subject: [PATCH 3/4] Correctly capture wrapped lines in the pane contents Add the -J options to `capture-pane` to handle wrapped lines correctly. This way wrapped lines will be joined upon capture and once restored, will re-wrap upon pane size changes. --- scripts/save.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/save.sh b/scripts/save.sh index 327a352..fef80cf 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -113,7 +113,7 @@ capture_pane_contents() { local pane_id="$1" local start_line="0" [[ "$(get_tmux_option "$pane_contents_area_option" "visible")" == "full" ]] && start_line="-$2" - tmux capture-pane -ep -S "$start_line" -t "$pane_id" > "$(resurrect_pane_file "$pane_id")" + tmux capture-pane -epJ -S "$start_line" -t "$pane_id" > "$(resurrect_pane_file "$pane_id")" } save_shell_history() { From 737568922be14a95d05359f488655dd454f46c14 Mon Sep 17 00:00:00 2001 From: quentin Date: Wed, 18 Mar 2015 22:38:38 +0100 Subject: [PATCH 4/4] Make "full" pane contents saving the default --- scripts/save.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/save.sh b/scripts/save.sh index fef80cf..5ca7e60 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -111,8 +111,8 @@ pane_full_command() { capture_pane_contents() { local pane_id="$1" - local start_line="0" - [[ "$(get_tmux_option "$pane_contents_area_option" "visible")" == "full" ]] && start_line="-$2" + 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")" }