Skip to content

Commit

Permalink
Fix tmux-plugins#7 tmux-plugins#28 tmux-plugins#31 Propose an option …
Browse files Browse the repository at this point in the history
…to automatically turn on logging for new panes
  • Loading branch information
tchinchow committed Jan 7, 2025
1 parent b5c5f7b commit c490af7
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 84 deletions.
7 changes: 6 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Custom configuration

The default logging path is `$HOME` To change that, add `set -g @logging-path "path"` to `.tmux.conf` file
The default logging path is `$HOME`
To change that, add `set -g @logging-path "path"` to `.tmux.conf` file.

The default behaviour is to not start logging with any new session.
You can control this behaviour by adding
`set -g @toggle-logging-at-startup "true"` to `.tmux.conf` file.
12 changes: 12 additions & 0 deletions logging.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/variables.sh"
source "$CURRENT_DIR/scripts/shared.sh"

is_toggle_logging_at_startup() {
local toggle_logging_at_startup="$(get_tmux_option "@toggle-logging-at-startup" "false")"
if [ "$toggle_logging_at_startup" == "true" ]; then
return 0
else
return 1
fi
}

main() {
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"

if is_toggle_logging_at_startup; then
tmux set-hook -g after-new-session "run-shell '$CURRENT_DIR/scripts/start_logging.sh'"
fi
}

main
80 changes: 80 additions & 0 deletions scripts/shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,83 @@ expand_tmux_format_path() {
mkdir -p "${full_directory_path}"
echo "${full_path}"
}

ansifilter_installed() {
type ansifilter >/dev/null 2>&1 || return 1
}

system_osx() {
[ $(uname) == "Darwin" ]
}

pipe_pane_ansifilter() {
local file=$1
tmux pipe-pane "exec cat - | ansifilter >> '${file}'"
}

pipe_pane_sed_osx() {
local file=$1
# Warning, very complex regex ahead.
# Some characters below might not be visible from github web view.
local ansi_codes_osx="(\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]||]0;[^]+|[[:space:]]+$)"
tmux pipe-pane "exec cat - | sed -E \"s/$ansi_codes_osx//g\" >> '${file}'"
}

pipe_pane_sed() {
local file=$1
local ansi_codes="(\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]|)"
tmux pipe-pane "exec cat - | sed -r 's/$ansi_codes//g' >> '${file}'"
}

start_pipe_pane() {
local file=$(expand_tmux_format_path "${logging_full_filename}")

if ansifilter_installed; then
pipe_pane_ansifilter "${file}"
elif system_osx; then
# OSX uses sed '-E' flag and a slightly different regex
pipe_pane_sed_osx "${file}"
else
pipe_pane_sed "${file}"
fi
set_logging_variable "logging"
display_message "Started logging to ${logging_full_filename}"
}

stop_pipe_pane() {
tmux pipe-pane
set_logging_variable "not logging"
display_message "Ended logging to $logging_full_filename"
}

# returns a string unique to current pane
pane_unique_id() {
tmux display-message -p "#{session_name}_#{window_index}_#{pane_index}"
}

# saving 'logging' 'not logging' status in a variable unique to pane
set_logging_variable() {
local value="$1"
local pane_unique_id="$(pane_unique_id)"
tmux set-option -gq "@${pane_unique_id}" "$value"
}

# this function checks if logging is happening for the current pane
is_logging() {
local pane_unique_id="$(pane_unique_id)"
local current_pane_logging="$(get_tmux_option "@${pane_unique_id}" "not logging")"
if [ "$current_pane_logging" == "logging" ]; then
return 0
else
return 1
fi
}

# starts/stop logging
toggle_pipe_pane() {
if is_logging; then
stop_pipe_pane
else
start_pipe_pane
fi
}
Expand Down
43 changes: 6 additions & 37 deletions scripts/start_logging.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,13 @@
#!/usr/bin/env bash

# path to log file - global variable
FILE="$1"
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

ansifilter_installed() {
type ansifilter >/dev/null 2>&1 || return 1
}

system_osx() {
[ $(uname) == "Darwin" ]
}

pipe_pane_ansifilter() {
tmux pipe-pane "exec cat - | ansifilter >> $FILE"
}

pipe_pane_sed_osx() {
# Warning, very complex regex ahead.
# Some characters below might not be visible from github web view.
local ansi_codes_osx="(\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]||]0;[^]+|[[:space:]]+$)"
tmux pipe-pane "exec cat - | sed -E \"s/$ansi_codes_osx//g\" >> $FILE"
}

pipe_pane_sed() {
local ansi_codes="(\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]|)"
tmux pipe-pane "exec cat - | sed -r 's/$ansi_codes//g' >> $FILE"
}

start_pipe_pane() {
if ansifilter_installed; then
pipe_pane_ansifilter
elif system_osx; then
# OSX uses sed '-E' flag and a slightly different regex
pipe_pane_sed_osx
else
pipe_pane_sed
fi
}
source "$CURRENT_DIR/variables.sh"
source "$CURRENT_DIR/shared.sh"

main() {
start_pipe_pane
if supported_tmux_version_ok; then
start_pipe_pane
fi
}
main
46 changes: 0 additions & 46 deletions scripts/toggle_logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,6 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/variables.sh"
source "$CURRENT_DIR/shared.sh"


start_pipe_pane() {
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 $logging_full_filename"
}

# returns a string unique to current pane
pane_unique_id() {
tmux display-message -p "#{session_name}_#{window_index}_#{pane_index}"
}

# saving 'logging' 'not logging' status in a variable unique to pane
set_logging_variable() {
local value="$1"
local pane_unique_id="$(pane_unique_id)"
tmux set-option -gq "@${pane_unique_id}" "$value"
}

# this function checks if logging is happening for the current pane
is_logging() {
local pane_unique_id="$(pane_unique_id)"
local current_pane_logging="$(get_tmux_option "@${pane_unique_id}" "not logging")"
if [ "$current_pane_logging" == "logging" ]; then
return 0
else
return 1
fi
}

# starts/stop logging
toggle_pipe_pane() {
if is_logging; then
set_logging_variable "not logging"
stop_pipe_pane
else
set_logging_variable "logging"
start_pipe_pane
fi
}

main() {
if supported_tmux_version_ok; then
toggle_pipe_pane
Expand Down
4 changes: 4 additions & 0 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ logging_filename=${logging_filename:-$default_logging_filename}

logging_full_filename="${logging_path}/${logging_filename}"

default_toggle_logging_at_startup="$HOME"
toggle_logging_at_startup=$(tmux show-option -gqv "@toggle-logging-at-startup")
toggle_logging_at_startup=${toggle_logging_at_startup:-$default_toggle_logging_at_startup}

# Screen capture options
default_screen_capture_path="$HOME"
screen_capture_path=$(tmux show-option -gqv "@screen-capture-path")
Expand Down

0 comments on commit c490af7

Please sign in to comment.