From f5ed7640c83c44a881f3f5da365af09afbe3fa33 Mon Sep 17 00:00:00 2001 From: Sergey Yakovlev Date: Tue, 13 Feb 2018 02:12:49 +0500 Subject: [PATCH 1/2] Changing variable working way. Reducing 'get_tmux_option' uses. Adding auto-creating folders in logging path --- logging.tmux | 13 +++++------- scripts/capture_pane_helpers.sh | 23 ++++++-------------- scripts/save_complete_history.sh | 7 ------- scripts/screen_capture.sh | 7 ------- scripts/toggle_logging.sh | 15 ++++++------- scripts/variables.sh | 36 +++++++++++++++++++++++--------- 6 files changed, 43 insertions(+), 58 deletions(-) diff --git a/logging.tmux b/logging.tmux index 8f54877..b9459ba 100755 --- a/logging.tmux +++ b/logging.tmux @@ -5,24 +5,21 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/scripts/variables.sh" source "$CURRENT_DIR/scripts/shared.sh" + setup_logging_key_binding() { - local key="$(get_tmux_option "$logging_key_option" "$default_logging_key")" - tmux bind-key "$key" run-shell "$CURRENT_DIR/scripts/toggle_logging.sh" + tmux bind-key "$logging_key" run-shell "$CURRENT_DIR/scripts/toggle_logging.sh" } setup_screen_capture_key_binding() { - local key="$(get_tmux_option "$pane_screen_capture_key_option" "$default_pane_screen_capture_key")" - tmux bind-key "$key" run-shell "$CURRENT_DIR/scripts/screen_capture.sh" + tmux bind-key "$pane_screen_capture_key" run-shell "$CURRENT_DIR/scripts/screen_capture.sh" } setup_save_complete_history_key_binding() { - local key="$(get_tmux_option "$save_complete_history_key_option" "$default_save_complete_history_key")" - tmux bind-key "$key" run-shell "$CURRENT_DIR/scripts/save_complete_history.sh" + tmux bind-key "$save_complete_history_key" run-shell "$CURRENT_DIR/scripts/save_complete_history.sh" } setup_clear_history_key_binding() { - local key="$(get_tmux_option "$clear_history_key_option" "$default_clear_history_key")" - tmux bind-key "$key" run-shell "$CURRENT_DIR/scripts/clear_history.sh" + tmux bind-key "$clear_history_key" run-shell "$CURRENT_DIR/scripts/clear_history.sh" } main() { diff --git a/scripts/capture_pane_helpers.sh b/scripts/capture_pane_helpers.sh index f8594e2..d3830cb 100644 --- a/scripts/capture_pane_helpers.sh +++ b/scripts/capture_pane_helpers.sh @@ -1,26 +1,15 @@ -# Variables in this helper should be set in the file sourcing the helper. -# Required variables: -# - path_option & default_path -# - name_option & default_name - -get_path() { - get_tmux_option "$path_option" "$default_path" -} - -# `tmux save-buffer` command does not perform interpolation, so we're doing it -# "manually" with `display-message` -get_filename() { - local name_template=$(get_tmux_option "$name_option" "$default_name") - tmux display-message -p "$name_template" -} - capture_pane() { - local file="$(get_path)/$(get_filename)" local capture_scope=$1 if [ $capture_scope == "History" ]; then + local file=$(tmux display-message -p "$save_complete_history_full_filename") + local path=$(tmux display-message -p "$save_complete_history_path") + mkdir -p "$path" local history_limit="$(tmux display-message -p -F "#{history_limit}")" tmux capture-pane -J -S "-${history_limit}" -p > "$file" elif [[ $capture_scope == "Screen capture" ]]; then + local file=$(tmux display-message -p "$screen_capture_full_filename") + local path=$(tmux display-message -p "$screen_capture_path") + mkdir -p "$path" tmux capture-pane -J -p > "$file" else # error diff --git a/scripts/save_complete_history.sh b/scripts/save_complete_history.sh index 3040d4d..445134c 100755 --- a/scripts/save_complete_history.sh +++ b/scripts/save_complete_history.sh @@ -6,13 +6,6 @@ source "$CURRENT_DIR/variables.sh" source "$CURRENT_DIR/shared.sh" source "$CURRENT_DIR/capture_pane_helpers.sh" -# Functions defined in capture_pane_helpers are customized via below variables. -default_path="$default_save_complete_history_path" -default_name="$default_save_complete_history_filename" - -path_option="$save_complete_history_path_option" -name_option="$save_complete_history_filename_option" - main() { if supported_tmux_version_ok; then capture_pane "History" diff --git a/scripts/screen_capture.sh b/scripts/screen_capture.sh index 57819c0..9002172 100755 --- a/scripts/screen_capture.sh +++ b/scripts/screen_capture.sh @@ -6,13 +6,6 @@ source "$CURRENT_DIR/variables.sh" source "$CURRENT_DIR/shared.sh" source "$CURRENT_DIR/capture_pane_helpers.sh" -# Functions defined in capture_pane_helpers are customized via below variables. -default_path="$default_screen_capture_path" -default_name="$default_screen_capture_filename" - -path_option="$screen_capture_path_option" -name_option="$screen_capture_filename_option" - main() { if supported_tmux_version_ok; then capture_pane "Screen capture" diff --git a/scripts/toggle_logging.sh b/scripts/toggle_logging.sh index 9299973..bc63d90 100755 --- a/scripts/toggle_logging.sh +++ b/scripts/toggle_logging.sh @@ -5,21 +5,18 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/variables.sh" source "$CURRENT_DIR/shared.sh" -get_filename() { - local logging_path="$(get_tmux_option "$logging_path_option" "$default_logging_path")" - local logging_filename="$(get_tmux_option "$logging_filename_option" "$default_logging_filename")" - echo "${logging_path}/${logging_filename}" -} start_pipe_pane() { - local filename="$(get_filename)" - "$CURRENT_DIR/start_logging.sh" "$filename" - display_message "Started logging to $filename" + local file=$(tmux display-message -p "$logging_full_filename") + local path=$(tmux display-message -p "$logging_path") + mkdir -p "$path" + "$CURRENT_DIR/start_logging.sh" "$file" + display_message "Started logging to $logging_full_filename" } stop_pipe_pane() { tmux pipe-pane - display_message "Ended logging to $(get_filename)" + display_message "Ended logging to $logging_full_filename" } # returns a string unique to current pane diff --git a/scripts/variables.sh b/scripts/variables.sh index 3017db2..2893233 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -2,38 +2,54 @@ SUPPORTED_VERSION="1.9" # Key binding options and defaults -logging_key_option="@logging-key" default_logging_key="P" # Shift-p +logging_key=$(tmux show-option -gqv "@logging_key") +logging_key=${logging_key:-$default_logging_key} -pane_screen_capture_key_option="@screen-capture-key" default_pane_screen_capture_key="M-p" # Alt-p +pane_screen_capture_key=$(tmux show-option -gqv "@screen-capture-key") +pane_screen_capture_key=${pane_screen_capture_key:-$default_pane_screen_capture_key} -save_complete_history_key_option="@save-complete-history-key" default_save_complete_history_key="M-P" # Alt-Shift-p +save_complete_history_key=$(tmux show-option -gqv "@save-complete-history-key") +save_complete_history_key=${save_complete_history_key:-$default_save_complete_history_key} -clear_history_key_option="@clear-history-key" default_clear_history_key="M-c" # Alt-c +clear_history_key=$(tmux show-option -gqv "@clear-history-key") +clear_history_key=${clear_history_key:-$default_clear_history_key} # General options filename_suffix="#{session_name}-#{window_index}-#{pane_index}-%Y%m%dT%H%M%S.log" # Logging options -logging_path_option="@logging-path" default_logging_path="$HOME" +logging_path=$(tmux show-option -gqv "@logging-path") +logging_path=${logging_path:-$default_clear_history_key} -logging_filename_option="@logging-filename" default_logging_filename="tmux-${filename_suffix}" +logging_filename=$(tmux show-option -gqv "@logging-filename") +logging_filename=${logging_filename:-$default_logging_filename} + +logging_full_filename="${logging_path}/${logging_filename}" # Screen capture options -screen_capture_path_option="@screen-capture-path" default_screen_capture_path="$HOME" +screen_capture_path=$(tmux show-option -gqv "@screen-capture-path") +screen_capture_path=${screen_capture_path:-$default_screen_capture_path} -screen_capture_filename_option="@screen-capture-filename" default_screen_capture_filename="tmux-screen-capture-${filename_suffix}" +screen_capture_filename=$(tmux show-option -gqv "@screen-capture-filename") +screen_capture_filename=${screen_capture_filename:-$default_screen_capture_filename} + +screen_capture_full_filename="${screen_capture_path}/${screen_capture_filename}" # Save complete history options -save_complete_history_path_option="@save-complete-history-path" default_save_complete_history_path="$HOME" +save_complete_history_path=$(tmux show-option -gqv "@save-complete-history-path") +save_complete_history_path=${save_complete_history_path:-$default_save_complete_history_path} -save_complete_history_filename_option="@save-complete-history-filename" default_save_complete_history_filename="tmux-history-${filename_suffix}" +save_complete_history_filename=$(tmux show-option -gqv "@save-complete-history-filename") +save_complete_history_filename=${save_complete_history_filename:-$default_save_complete_history_filename} + +save_complete_history_full_filename="${save_complete_history_path}/${save_complete_history_filename}" From 3059d52cc8b409a6298ae653d469660100cc3367 Mon Sep 17 00:00:00 2001 From: Sergey Yakovlev Date: Fri, 2 Mar 2018 16:37:33 +0500 Subject: [PATCH 2/2] Refactoring code based on #21 PR comments --- logging.tmux | 17 +---------------- scripts/capture_pane_helpers.sh | 20 -------------------- scripts/save_complete_history.sh | 7 +++++-- scripts/screen_capture.sh | 6 ++++-- scripts/shared.sh | 10 ++++++++++ scripts/toggle_logging.sh | 8 +++----- 6 files changed, 23 insertions(+), 45 deletions(-) delete mode 100644 scripts/capture_pane_helpers.sh diff --git a/logging.tmux b/logging.tmux index b9459ba..c65856e 100755 --- a/logging.tmux +++ b/logging.tmux @@ -6,26 +6,11 @@ source "$CURRENT_DIR/scripts/variables.sh" source "$CURRENT_DIR/scripts/shared.sh" -setup_logging_key_binding() { +main() { tmux bind-key "$logging_key" run-shell "$CURRENT_DIR/scripts/toggle_logging.sh" -} - -setup_screen_capture_key_binding() { tmux bind-key "$pane_screen_capture_key" run-shell "$CURRENT_DIR/scripts/screen_capture.sh" -} - -setup_save_complete_history_key_binding() { tmux bind-key "$save_complete_history_key" run-shell "$CURRENT_DIR/scripts/save_complete_history.sh" -} - -setup_clear_history_key_binding() { tmux bind-key "$clear_history_key" run-shell "$CURRENT_DIR/scripts/clear_history.sh" } -main() { - setup_logging_key_binding - setup_screen_capture_key_binding - setup_save_complete_history_key_binding - setup_clear_history_key_binding -} main diff --git a/scripts/capture_pane_helpers.sh b/scripts/capture_pane_helpers.sh deleted file mode 100644 index d3830cb..0000000 --- a/scripts/capture_pane_helpers.sh +++ /dev/null @@ -1,20 +0,0 @@ -capture_pane() { - local capture_scope=$1 - if [ $capture_scope == "History" ]; then - local file=$(tmux display-message -p "$save_complete_history_full_filename") - local path=$(tmux display-message -p "$save_complete_history_path") - mkdir -p "$path" - local history_limit="$(tmux display-message -p -F "#{history_limit}")" - tmux capture-pane -J -S "-${history_limit}" -p > "$file" - elif [[ $capture_scope == "Screen capture" ]]; then - local file=$(tmux display-message -p "$screen_capture_full_filename") - local path=$(tmux display-message -p "$screen_capture_path") - mkdir -p "$path" - tmux capture-pane -J -p > "$file" - else - # error - exit 1 - fi - remove_empty_lines_from_end_of_file "$file" - display_message "$capture_scope saved to $file" -} diff --git a/scripts/save_complete_history.sh b/scripts/save_complete_history.sh index 445134c..569b56b 100755 --- a/scripts/save_complete_history.sh +++ b/scripts/save_complete_history.sh @@ -4,11 +4,14 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/variables.sh" source "$CURRENT_DIR/shared.sh" -source "$CURRENT_DIR/capture_pane_helpers.sh" main() { if supported_tmux_version_ok; then - capture_pane "History" + local file=$(expand_tmux_format_path "${save_complete_history_full_filename}") + local history_limit="$(tmux display-message -p -F "#{history_limit}")" + tmux capture-pane -J -S "-${history_limit}" -p > "${file}" + remove_empty_lines_from_end_of_file "${file}" + display_message "History saved to ${file}" fi } main diff --git a/scripts/screen_capture.sh b/scripts/screen_capture.sh index 9002172..9397cd3 100755 --- a/scripts/screen_capture.sh +++ b/scripts/screen_capture.sh @@ -4,11 +4,13 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/variables.sh" source "$CURRENT_DIR/shared.sh" -source "$CURRENT_DIR/capture_pane_helpers.sh" main() { if supported_tmux_version_ok; then - capture_pane "Screen capture" + local file=$(expand_tmux_format_path "${screen_capture_full_filename}") + tmux capture-pane -J -p > "${file}" + remove_empty_lines_from_end_of_file "${file}" + display_message "Screen capture saved to ${file}" fi } main diff --git a/scripts/shared.sh b/scripts/shared.sh index 90247a8..17f2867 100644 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -43,3 +43,13 @@ remove_empty_lines_from_end_of_file() { supported_tmux_version_ok() { $CURRENT_DIR/check_tmux_version.sh "$SUPPORTED_VERSION" } + +# Checking full path to logfile and expanding tmux format in normal path +# As example: expand %Y-%m-%d to current date +expand_tmux_format_path() { + local tmux_format_path=$1 + local full_path=$(tmux display-message -p "${tmux_format_path}") + local full_directory_path=${full_path%/*} + mkdir -p "${full_directory_path}" + echo "${full_path}" +} diff --git a/scripts/toggle_logging.sh b/scripts/toggle_logging.sh index bc63d90..e240d8c 100755 --- a/scripts/toggle_logging.sh +++ b/scripts/toggle_logging.sh @@ -7,11 +7,9 @@ source "$CURRENT_DIR/shared.sh" start_pipe_pane() { - local file=$(tmux display-message -p "$logging_full_filename") - local path=$(tmux display-message -p "$logging_path") - mkdir -p "$path" - "$CURRENT_DIR/start_logging.sh" "$file" - display_message "Started logging to $logging_full_filename" + local file=$(expand_tmux_format_path "${logging_full_filename}") + "$CURRENT_DIR/start_logging.sh" "${file}" + display_message "Started logging to ${logging_full_filename}" } stop_pipe_pane() {