Skip to content

Commit

Permalink
Merge pull request #21 from selfuryon/master
Browse files Browse the repository at this point in the history
Auto-creating folders in logging path #2
  • Loading branch information
bruno- authored Mar 2, 2018
2 parents b270611 + 3059d52 commit f79e5ed
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 91 deletions.
28 changes: 5 additions & 23 deletions logging.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,12 @@ 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"
}

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"
}

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"
}

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"
}

main() {
setup_logging_key_binding
setup_screen_capture_key_binding
setup_save_complete_history_key_binding
setup_clear_history_key_binding
tmux bind-key "$logging_key" run-shell "$CURRENT_DIR/scripts/toggle_logging.sh"
tmux bind-key "$pane_screen_capture_key" run-shell "$CURRENT_DIR/scripts/screen_capture.sh"
tmux bind-key "$save_complete_history_key" run-shell "$CURRENT_DIR/scripts/save_complete_history.sh"
tmux bind-key "$clear_history_key" run-shell "$CURRENT_DIR/scripts/clear_history.sh"
}

main
31 changes: 0 additions & 31 deletions scripts/capture_pane_helpers.sh

This file was deleted.

14 changes: 5 additions & 9 deletions scripts/save_complete_history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +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"

# 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"
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
13 changes: 4 additions & 9 deletions scripts/screen_capture.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +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"

# 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"
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
10 changes: 10 additions & 0 deletions scripts/shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
13 changes: 4 additions & 9 deletions scripts/toggle_logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@ 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=$(expand_tmux_format_path "${logging_full_filename}")
"$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
Expand Down
36 changes: 26 additions & 10 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

0 comments on commit f79e5ed

Please sign in to comment.