From 74773bed62fc232f4574b73a2b5d0fca2e739029 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Tue, 7 Jul 2015 23:57:11 +0200 Subject: [PATCH] Save pane contents only if pane not blank --- CHANGELOG.md | 2 ++ scripts/helpers.sh | 5 +++++ scripts/restore.sh | 9 ++++++--- scripts/save.sh | 28 +++++++++++++++++++++++++--- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbf963c..fab2799 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ - bugfix for pane contents restoration - expand tilde char `~` if used with `@resurrect-dir` - do not save empty trailing lines when pane content is saved +- do not save pane contents if pane is empty (only for 'save pane contents' + feature) ### v2.4.0, 2015-02-23 - add "tmux-test" diff --git a/scripts/helpers.sh b/scripts/helpers.sh index b897d9d..146ab71 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -92,6 +92,11 @@ pane_contents_file() { echo "$(resurrect_dir)/pane_contents-${pane_id}" } +pane_contents_file_exists() { + local pane_id="$1" + [ -f "$(pane_contents_file "$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 a1f48bf..dceaa01 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -117,7 +117,8 @@ new_window() { local window_name="$3" local dir="$4" local pane_index="$5" - if is_restoring_pane_contents; then + local pane_id="${session_name}:${window_number}.${pane_index}" + if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")" tmux new-window -d -t "${session_name}:${window_number}" -n "$window_name" -c "$dir" "$pane_creation_command" else @@ -131,7 +132,8 @@ new_session() { local window_name="$3" local dir="$4" local pane_index="$5" - if is_restoring_pane_contents; then + local pane_id="${session_name}:${window_number}.${pane_index}" + if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")" TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -n "$window_name" -c "$dir" "$pane_creation_command" else @@ -150,7 +152,8 @@ new_pane() { local window_name="$3" local dir="$4" local pane_index="$5" - if is_restoring_pane_contents; then + local pane_id="${session_name}:${window_number}.${pane_index}" + if is_restoring_pane_contents && pane_contents_file_exists "$pane_id"; then local pane_creation_command="$(pane_creation_command "$session_name" "$window_number" "$pane_index")" tmux split-window -t "${session_name}:${window_number}" -c "$dir" "$pane_creation_command" else diff --git a/scripts/save.sh b/scripts/save.sh index b2a9d4f..fe84f1a 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -110,14 +110,36 @@ pane_full_command() { $strategy_file "$pane_pid" } +number_nonempty_lines_on_screen() { + local pane_id="$1" + tmux capture-pane -pJ -t "$pane_id" | + sed '/^$/d' | + wc -l | + sed 's/ //g' +} + +# tests if there was any command output in the current pane +pane_has_any_content() { + local pane_id="$1" + local history_size="$(tmux display -p -t "$pane_id" -F "#{history_size}")" + local cursor_y="$(tmux display -p -t "$pane_id" -F "#{cursor_y}")" + # doing "cheap" tests first + [ "$history_size" -gt 0 ] || # history has any content? + [ "$cursor_y" -gt 0 ] || # cursor not in first line? + [ "$(number_nonempty_lines_on_screen "$pane_id")" -gt 1 ] +} + capture_pane_contents() { local pane_id="$1" local start_line="-$2" local pane_contents_area="$3" - if [ "$pane_contents_area" = "visible" ]; then - start_line="0" + if pane_has_any_content "$pane_id"; then + if [ "$pane_contents_area" = "visible" ]; then + start_line="0" + fi + # the printf hack below removes *trailing* empty lines + printf '%s\n' "$(tmux capture-pane -epJ -S "$start_line" -t "$pane_id")" > "$(pane_contents_file "$pane_id")" fi - printf '%s\n' "$(tmux capture-pane -epJ -S "$start_line" -t "$pane_id")" > "$(pane_contents_file "$pane_id")" } save_shell_history() {