Skip to content

Commit c490af7

Browse files
committed
Fix tmux-plugins#7 tmux-plugins#28 tmux-plugins#31 Propose an option to automatically turn on logging for new panes
1 parent b5c5f7b commit c490af7

File tree

6 files changed

+108
-84
lines changed

6 files changed

+108
-84
lines changed

docs/configuration.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
# Custom configuration
22

3-
The default logging path is `$HOME` To change that, add `set -g @logging-path "path"` to `.tmux.conf` file
3+
The default logging path is `$HOME`
4+
To change that, add `set -g @logging-path "path"` to `.tmux.conf` file.
5+
6+
The default behaviour is to not start logging with any new session.
7+
You can control this behaviour by adding
8+
`set -g @toggle-logging-at-startup "true"` to `.tmux.conf` file.

logging.tmux

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55
source "$CURRENT_DIR/scripts/variables.sh"
66
source "$CURRENT_DIR/scripts/shared.sh"
77

8+
is_toggle_logging_at_startup() {
9+
local toggle_logging_at_startup="$(get_tmux_option "@toggle-logging-at-startup" "false")"
10+
if [ "$toggle_logging_at_startup" == "true" ]; then
11+
return 0
12+
else
13+
return 1
14+
fi
15+
}
816

917
main() {
1018
tmux bind-key "$logging_key" run-shell "$CURRENT_DIR/scripts/toggle_logging.sh"
1119
tmux bind-key "$pane_screen_capture_key" run-shell "$CURRENT_DIR/scripts/screen_capture.sh"
1220
tmux bind-key "$save_complete_history_key" run-shell "$CURRENT_DIR/scripts/save_complete_history.sh"
1321
tmux bind-key "$clear_history_key" run-shell "$CURRENT_DIR/scripts/clear_history.sh"
22+
23+
if is_toggle_logging_at_startup; then
24+
tmux set-hook -g after-new-session "run-shell '$CURRENT_DIR/scripts/start_logging.sh'"
25+
fi
1426
}
1527

1628
main

scripts/shared.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,83 @@ expand_tmux_format_path() {
5353
mkdir -p "${full_directory_path}"
5454
echo "${full_path}"
5555
}
56+
57+
ansifilter_installed() {
58+
type ansifilter >/dev/null 2>&1 || return 1
59+
}
60+
61+
system_osx() {
62+
[ $(uname) == "Darwin" ]
63+
}
64+
65+
pipe_pane_ansifilter() {
66+
local file=$1
67+
tmux pipe-pane "exec cat - | ansifilter >> '${file}'"
68+
}
69+
70+
pipe_pane_sed_osx() {
71+
local file=$1
72+
# Warning, very complex regex ahead.
73+
# Some characters below might not be visible from github web view.
74+
local ansi_codes_osx="(\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]||]0;[^]+|[[:space:]]+$)"
75+
tmux pipe-pane "exec cat - | sed -E \"s/$ansi_codes_osx//g\" >> '${file}'"
76+
}
77+
78+
pipe_pane_sed() {
79+
local file=$1
80+
local ansi_codes="(\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]|)"
81+
tmux pipe-pane "exec cat - | sed -r 's/$ansi_codes//g' >> '${file}'"
82+
}
83+
84+
start_pipe_pane() {
85+
local file=$(expand_tmux_format_path "${logging_full_filename}")
86+
87+
if ansifilter_installed; then
88+
pipe_pane_ansifilter "${file}"
89+
elif system_osx; then
90+
# OSX uses sed '-E' flag and a slightly different regex
91+
pipe_pane_sed_osx "${file}"
92+
else
93+
pipe_pane_sed "${file}"
94+
fi
95+
set_logging_variable "logging"
96+
display_message "Started logging to ${logging_full_filename}"
97+
}
98+
99+
stop_pipe_pane() {
100+
tmux pipe-pane
101+
set_logging_variable "not logging"
102+
display_message "Ended logging to $logging_full_filename"
103+
}
104+
105+
# returns a string unique to current pane
106+
pane_unique_id() {
107+
tmux display-message -p "#{session_name}_#{window_index}_#{pane_index}"
108+
}
109+
110+
# saving 'logging' 'not logging' status in a variable unique to pane
111+
set_logging_variable() {
112+
local value="$1"
113+
local pane_unique_id="$(pane_unique_id)"
114+
tmux set-option -gq "@${pane_unique_id}" "$value"
115+
}
116+
117+
# this function checks if logging is happening for the current pane
118+
is_logging() {
119+
local pane_unique_id="$(pane_unique_id)"
120+
local current_pane_logging="$(get_tmux_option "@${pane_unique_id}" "not logging")"
121+
if [ "$current_pane_logging" == "logging" ]; then
122+
return 0
123+
else
124+
return 1
125+
fi
126+
}
127+
128+
# starts/stop logging
129+
toggle_pipe_pane() {
130+
if is_logging; then
131+
stop_pipe_pane
132+
else
133+
start_pipe_pane
134+
fi
135+
}

scripts/start_logging.sh

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,13 @@
11
#!/usr/bin/env bash
22

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

6-
ansifilter_installed() {
7-
type ansifilter >/dev/null 2>&1 || return 1
8-
}
9-
10-
system_osx() {
11-
[ $(uname) == "Darwin" ]
12-
}
13-
14-
pipe_pane_ansifilter() {
15-
tmux pipe-pane "exec cat - | ansifilter >> $FILE"
16-
}
17-
18-
pipe_pane_sed_osx() {
19-
# Warning, very complex regex ahead.
20-
# Some characters below might not be visible from github web view.
21-
local ansi_codes_osx="(\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]||]0;[^]+|[[:space:]]+$)"
22-
tmux pipe-pane "exec cat - | sed -E \"s/$ansi_codes_osx//g\" >> $FILE"
23-
}
24-
25-
pipe_pane_sed() {
26-
local ansi_codes="(\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]|)"
27-
tmux pipe-pane "exec cat - | sed -r 's/$ansi_codes//g' >> $FILE"
28-
}
29-
30-
start_pipe_pane() {
31-
if ansifilter_installed; then
32-
pipe_pane_ansifilter
33-
elif system_osx; then
34-
# OSX uses sed '-E' flag and a slightly different regex
35-
pipe_pane_sed_osx
36-
else
37-
pipe_pane_sed
38-
fi
39-
}
5+
source "$CURRENT_DIR/variables.sh"
6+
source "$CURRENT_DIR/shared.sh"
407

418
main() {
42-
start_pipe_pane
9+
if supported_tmux_version_ok; then
10+
start_pipe_pane
11+
fi
4312
}
4413
main

scripts/toggle_logging.sh

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,6 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55
source "$CURRENT_DIR/variables.sh"
66
source "$CURRENT_DIR/shared.sh"
77

8-
9-
start_pipe_pane() {
10-
local file=$(expand_tmux_format_path "${logging_full_filename}")
11-
"$CURRENT_DIR/start_logging.sh" "${file}"
12-
display_message "Started logging to ${logging_full_filename}"
13-
}
14-
15-
stop_pipe_pane() {
16-
tmux pipe-pane
17-
display_message "Ended logging to $logging_full_filename"
18-
}
19-
20-
# returns a string unique to current pane
21-
pane_unique_id() {
22-
tmux display-message -p "#{session_name}_#{window_index}_#{pane_index}"
23-
}
24-
25-
# saving 'logging' 'not logging' status in a variable unique to pane
26-
set_logging_variable() {
27-
local value="$1"
28-
local pane_unique_id="$(pane_unique_id)"
29-
tmux set-option -gq "@${pane_unique_id}" "$value"
30-
}
31-
32-
# this function checks if logging is happening for the current pane
33-
is_logging() {
34-
local pane_unique_id="$(pane_unique_id)"
35-
local current_pane_logging="$(get_tmux_option "@${pane_unique_id}" "not logging")"
36-
if [ "$current_pane_logging" == "logging" ]; then
37-
return 0
38-
else
39-
return 1
40-
fi
41-
}
42-
43-
# starts/stop logging
44-
toggle_pipe_pane() {
45-
if is_logging; then
46-
set_logging_variable "not logging"
47-
stop_pipe_pane
48-
else
49-
set_logging_variable "logging"
50-
start_pipe_pane
51-
fi
52-
}
53-
548
main() {
559
if supported_tmux_version_ok; then
5610
toggle_pipe_pane

scripts/variables.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ logging_filename=${logging_filename:-$default_logging_filename}
3232

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

35+
default_toggle_logging_at_startup="$HOME"
36+
toggle_logging_at_startup=$(tmux show-option -gqv "@toggle-logging-at-startup")
37+
toggle_logging_at_startup=${toggle_logging_at_startup:-$default_toggle_logging_at_startup}
38+
3539
# Screen capture options
3640
default_screen_capture_path="$HOME"
3741
screen_capture_path=$(tmux show-option -gqv "@screen-capture-path")

0 commit comments

Comments
 (0)