Skip to content

Commit b40b4d0

Browse files
authored
Merge pull request #24 from ChanderG/add_prompt_suffix_var
feat: add '@tnotify-prompt-suffixes' variable
2 parents c81b500 + 1ccd7e2 commit b40b4d0

File tree

6 files changed

+75
-54
lines changed

6 files changed

+75
-54
lines changed

README.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
<a href="https://github.com/ChanderG/tmux-notify"><img src="resources/tmux-notify-logo.svg" alt="tmux notify logo" width="567" height="135"/></a>
88

9-
Tmux plugin to notify you when processes complete.
9+
Tmux plugin to notify you when processes are complete.
1010

1111
Notification is via libnotify and visual bell raised in the tmux window. Visual bells can be mapped (in the terminal level) to X11 urgency bit and handled by your window manager.
1212

1313
## Use cases
1414

15-
- When you have already started a process in a pane and wish to be notified; that is can't use a manual trigger
16-
- Working in different containers (Docker) -> can't choose the shell -> and can't use a shell level feature
17-
- Working over ssh but your tmux is on client side
15+
- When you have already started a process in a pane and wish to be notified, that is when you can't use a manual trigger.
16+
- Working in different containers (Docker) -> can't choose the shell -> and can't use a shell level feature
17+
- Working over ssh, but your tmux is on the client-side
1818

1919
## Install
2020

@@ -41,41 +41,46 @@ Use `prefix + I` to install.
4141
- `notify-send` or `osascript`.
4242

4343
> **Note**
44-
> Works on Linux and macOS (note: only actively tested on Linux).
44+
> Works on Linux and macOS (note: only actively tested on Linux).
4545
4646
## Configuration
4747

4848
### Enable verbose notification
4949

50-
By default, the notification text is set to `Tmux pane task completed!`. We have also included a verbose output option. When enabled information about the pane, window and session in which the task has completed is given.
50+
By default, the notification text is set to `Tmux pane task completed!`. We have also included a verbose output option. When enabled, information about the pane, window, and session the task has completed is given.
5151

52-
Put `set -g @tnotify-verbose 'on'` in `.tmux.conf` to enable this.
52+
Put `set -g @tnotify-verbose 'on'` in the `.tmux.conf` config file to enable this.
5353

5454
#### Change the verbose notification message
5555

56-
To change the verbose notification text put `set -g @tnotify-verbose-msg 'put your notification text here'` in `.tmux.conf`. You can use all the tmux variables in your notification text. Some useful tmux aliases are:
56+
To change the verbose notification text, put `set -g @tnotify-verbose-msg 'put your notification text here'` in the `.tmux.conf` config file. You can use all the tmux variables in your notification text. Some useful tmux aliases are:
5757

58-
- `#D`: Pane id
59-
- `#P`: Pane index
60-
- `#T`: Pane title
61-
- `#S`: Session name
62-
- `#I`: Window index
63-
- `#W`: Window name
58+
- `#D`: Pane id
59+
- `#P`: Pane index
60+
- `#T`: Pane title
61+
- `#S`: Session name
62+
- `#I`: Window index
63+
- `#W`: Window name
6464

65-
For the full list of aliases and variables you are referred to the `FORMATS` section of the [tmux manual](http://man7.org/linux/man-pages/man1/tmux.1.html).
65+
For the full list of aliases and variables you are referred to the `FORMATS` section of the [tmux manual](http://man7.org/linux/man-pages/man1/tmux.1.html).
6666

6767
### Change monitor update period
6868

6969
By default, the monitor sleep period is set to 10 seconds. This means that tmux-notify checks the pane activity every 10 seconds.
7070

71-
Put `set -g @tnotify-sleep-duration 'desired duration'` in `.tmux.conf` to change this duration.
71+
Put `set -g @tnotify-sleep-duration 'desired duration" in the`.tmux.conf\` file to change this duration.
7272

7373
**NOTE:** Keep in mind that there is a trade-off between notification speed (short sleep duration) and the amount of memory this tool needs.
7474

75+
### Add additional shell suffixes
76+
77+
The tmux notify script uses your shell prompt suffix to check whether a command has finished. It looks for the `$`, `#` and `%` suffixes by default. If you customise your shell to use different shell suffixes, you can add them by putting `set -g @tnotify-prompt-suffixes 'put your comma-separated bash suffix list here'` in the`.tmux.conf\` file.
78+
79+
Feel free to open [a pull](https://github.com/ChanderG/tmux-notify/pulls) request or [issue](https://github.com/ChanderG/tmux-notify/issues) if you think your shell prompt suffix should be included by default.
80+
7581
## How does it work
7682

77-
Pretty naive approach actually. Checks if pane content ends in $ every 10 seconds.
78-
Will add other prompt end characters as needed.
83+
The pretty naive approach. Checks if pane content ends with the bash prompt suffixes mentioned above every 10 seconds.
7984

8085
## Contributing
8186

scripts/cancel.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
77

88
# Source helpers and variables
9-
source "$CURRENT_DIR/helpers.sh"
10-
source "$CURRENT_DIR/variables.sh"
9+
source "${CURRENT_DIR}/helpers.sh"
10+
source "${CURRENT_DIR}/variables.sh"
1111

1212
# Cancel pane monitoring if active
1313
if [[ -f "$PID_FILE_PATH" ]]; then
14-
14+
1515
# Retrieve monitor process PID
1616
PID=$(cat "$PID_FILE_PATH")
17-
17+
1818
# Kill process and remove pid file
1919
kill "$PID"
2020
rm "${PID_DIR}/${PANE_ID}.pid"
21-
21+
2222
# Display success message
2323
tmux display-message "Pane monitoring canceled..."
2424
else

scripts/helpers.sh

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
44

55
# Get tmux option
66
get_tmux_option() {
7-
local option="$1"
8-
local default_value="$2"
9-
local option_value=$(tmux show-option -gqv "$option")
10-
if [ -z "$option_value" ]; then
11-
echo "$default_value"
12-
else
13-
echo "$option_value"
14-
fi
7+
local option="$1"
8+
local default_value="$2"
9+
local option_value=$(tmux show-option -gqv "$option")
10+
if [ -z "$option_value" ]; then
11+
echo "$default_value"
12+
else
13+
echo "$option_value"
14+
fi
1515
}
1616

1717
# Set tmux option
1818
set_tmux_option() {
19-
local option="$1"
20-
local value="$2"
21-
tmux set-option -gq "$option" "$value"
19+
local option="$1"
20+
local value="$2"
21+
tmux set-option -gq "$option" "$value"
22+
}
23+
24+
# Escape globbing charaters
25+
escape_glob_chars() {
26+
echo "$1" | sed 's/[.[\*^$()+?{|]/\\&/g'
2227
}

scripts/notify.sh

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
66

77
# Source helpers and variables
8-
source "$CURRENT_DIR/helpers.sh"
9-
source "$CURRENT_DIR/variables.sh"
8+
source "${CURRENT_DIR}/helpers.sh"
9+
source "${CURRENT_DIR}/variables.sh"
1010

1111
## Functions
1212

@@ -20,7 +20,7 @@ notify() {
2020
# see https://superuser.com/questions/1118878/using-notify-send-in-a-tmux-session-shows-error-no-notification#1118896
2121
notify-send "$1"
2222
fi
23-
23+
2424
# trigger visual bell
2525
# your terminal emulator can be setup to set URGENT bit on visual bell
2626
# for eg, Xresources -> URxvt.urgentOnBell: true
@@ -32,7 +32,7 @@ on_cancel()
3232
{
3333
# Wait a bit for all pane monitors to complete
3434
sleep "$monitor_sleep_duration_value"
35-
35+
3636
# Preform cleanup operation is monitoring was canceled
3737
if [[ -f "$PID_FILE_PATH" ]]; then
3838
kill "$PID"
@@ -44,38 +44,47 @@ trap 'on_cancel' TERM
4444

4545
# Check if verbose option is enabled
4646
verbose_enabled() {
47-
local verbose_value="$(get_tmux_option "$verbose_option" "$verbose_default")"
48-
[ "$verbose_value" != "on" ]
47+
local verbose_value="$(get_tmux_option "$verbose_option" "$verbose_default")"
48+
[ "$verbose_value" != "on" ]
4949
}
5050

5151
## Main script
5252

5353
# Monitor pane if it is not already monitored
5454
if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored
55-
55+
5656
# job started - create pid-file
5757
echo "$$" > "$PID_FILE_PATH"
58-
58+
5959
# Display tnotify start messsage
6060
tmux display-message "Monitoring pane..."
61-
61+
6262
# Construct tnotify finish message
6363
if verbose_enabled; then # If @tnotify-verbose is disabled
6464
complete_message="Tmux pane task completed!"
6565
else # If @tnotify-verbose is enabled
6666
verbose_msg_value="$(get_tmux_option "$verbose_msg_option" "$verbose_msg_default")"
6767
complete_message=$(tmux display-message -p "$verbose_msg_value")
6868
fi
69-
69+
70+
# Create bash suffix list
71+
# NOTE: Looks complicated but uses shell parameter expansion
72+
# see https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
73+
prompt_suffixes="$(get_tmux_option "$prompt_suffixes" "$prompt_suffixes_default")"
74+
prompt_suffixes=${prompt_suffixes// /} # Remove whitespace
75+
prompt_suffixes=${prompt_suffixes//,/|} # Replace comma with or operator
76+
prompt_suffixes=$(escape_glob_chars "$prompt_suffixes")
77+
prompt_suffixes="\(${prompt_suffixes}\)$"
78+
7079
# Check process status every 10 seconds to see if has is finished
7180
while true; do
72-
81+
7382
# capture pane output
7483
output=$(tmux capture-pane -pt %"$PANE_ID")
75-
84+
7685
# run tests to determine if work is done
7786
# if so, break and notify
78-
if echo "$output" | tail -n2 | grep -e '\$\|#\|%' &> /dev/null; then
87+
if echo "$output" | tail -n2 | grep -e $prompt_suffixes &> /dev/null; then
7988
# tmux display-message "$@"
8089
if [[ "$1" == "refocus" ]]; then
8190
tmux switch -t \$"$SESSION_ID"
@@ -85,19 +94,19 @@ if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored
8594
notify "$complete_message"
8695
break
8796
fi
88-
97+
8998
# Sleep for a given time
9099
monitor_sleep_duration_value=$(get_tmux_option "$monitor_sleep_duration" "$monitor_sleep_duration_default")
91100
sleep "$monitor_sleep_duration_value"
92101
done
93-
102+
94103
# job done - remove pid file and return
95104
if [[ -f "$PID_FILE_PATH" ]]; then
96105
rm "$PID_FILE_PATH"
97106
fi
98107
exit 0
99108
else # If pane is already being monitored
100-
109+
101110
# Display pane already monitored message
102111
tmux display-message "Pane already monitored..."
103112
exit 0

scripts/variables.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
## Main variables
55
if [[ -z $XDG_CACHE_HOME ]]; then
6-
export PID_DIR=~/.tmux/notify
6+
export PID_DIR=~/.tmux/notify
77
else
8-
export PID_DIR="$XDG_CACHE_HOME/tmux/tmux-notify"
8+
export PID_DIR="$XDG_CACHE_HOME/tmux/tmux-notify"
99
fi
1010

1111
# Get ID's
@@ -15,6 +15,8 @@ export PANE_ID=$(tmux display-message -p '#{pane_id}' | tr -d %)
1515
export PID_FILE_PATH="${PID_DIR}/${PANE_ID}.pid"
1616

1717
## Tnotify tmux options
18+
export prompt_suffixes="@tnotify-prompt-suffixes"
19+
export prompt_suffixes_default="$,#,%"
1820

1921
# Notification verbosity settings
2022
export verbose_option="@tnotify-verbose"

tnotify.tmux

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55

66
# Set PID_DIR
77
if [[ -z $XDG_CACHE_HOME ]]; then
8-
export PID_DIR=~/.tmux/notify
8+
export PID_DIR=~/.tmux/notify
99
else
10-
export PID_DIR="$XDG_CACHE_HOME/tmux/tmux-notify"
10+
export PID_DIR="$XDG_CACHE_HOME/tmux/tmux-notify"
1111
fi
1212

1313
# Initialize variables

0 commit comments

Comments
 (0)