Skip to content

Commit

Permalink
🔀 Merge pull request #14 from rickstaa/fix_multi_call_#7
Browse files Browse the repository at this point in the history
Fixes bug #7 which allowed for multiple simultaneous pane monitoring
  • Loading branch information
rickstaa authored Nov 23, 2020
2 parents 860be5d + 5ec24f9 commit 941ac00
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 69 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# tmux-notify
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/ab27b2b704364bdd993f2c829473cebf)](https://www.codacy.com/app/rickstaa/tmux-notify?utm_source=github.com&utm_medium=referral&utm_content=rickstaa/tmux-notify&utm_campaign=Badge_Grade)

[![Maintained](https://img.shields.io/badge/Maintained%3F-yes-green)](https://github.com/ChanderG/tmux-notify/pulse)
[![Contributions](https://img.shields.io/badge/contributions-welcome-orange.svg)](contributing.md)
[![Tmux version](https://img.shields.io/badge/tmux-%3D%3E1.9-blue)](https://github.com/tmux/tmux/wiki)
Expand Down Expand Up @@ -65,12 +65,13 @@ Put `set -g @tnotify-sleep-duration 'desired duration'` in `.tmux.conf` to chang

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

## How does it work?
## How does it work

Pretty naive approach actually. Checks if pane content ends in $ every 10 seconds.
Will add other prompt end characters as needed.

## Contributing

Contributions to this repository are welcome. See the [contribution guidelines](contributing.md) for more information.

## License
Expand Down
37 changes: 25 additions & 12 deletions scripts/cancel.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
#!/usr/bin/env bash
## -- Cancel monitoring script
# Used to cancel the current pane monitor job

# get id of the current active pane
PANEID=$(tmux list-panes | grep "active" | awk -F\] '{print $3}' | awk '{print $1}')
PID_DIR=~/.tmux/notify
# Get current directory
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Cancel monitor process if active
{ # Try
# Source helpers and variables
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/variables.sh"

# consult pid file for the pid
PID=$(cat "$PID_DIR/$PANEID".pid)
# Get pane id
SESSION_NR=$(tmux list-sessions | grep "(attached)" | awk '{print $1}' | tr -d :)
WINDOW_NR=$(tmux list-windows | grep "(active)" | awk '{print $1}' | tr -d :)
PANE_NR=$(tmux list-panes | grep "active" | awk -F\] '{print $3}' | awk '{print $1}' | tr -d %)
PANE_ID=$(detox_file_name "s_${SESSION_NR}_w${WINDOW_NR}_p${PANE_NR}")
PID_FILE_PATH="${PID_DIR}/${PANE_ID}.pid"

# job done - kill process and remove pid file
kill $PID
rm "$PID_DIR/$PANEID".pid
# Cancel pane monitoring if active
if [[ -f "$PID_FILE_PATH" ]]; then

} || { # Catch
# Retrieve monitor process PID
PID=$(cat "$PID_FILE_PATH")

# Kill process and remove pid file
kill "$PID"
rm "${PID_DIR}/${PANE_ID}.pid"

# Display success message
tmux display-message "Pane monitoring canceled..."
else
tmux display-message "Pane not monitored..."
exit 0
}
fi
8 changes: 8 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash
## -- Helper functions
# Additional functions that are used in the main scripts.

# Get tmux option
get_tmux_option() {
Expand All @@ -19,3 +20,10 @@ set_tmux_option() {
local value="$2"
tmux set-option -gq "$option" "$value"
}

## Detox file names
# Makes sure invalid chars are removed from a filename
detox_file_name(){
local file_name="$1"
echo "${file_name//[^A-Za-z0-9._-]/_}"
}
109 changes: 65 additions & 44 deletions scripts/notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ source "$CURRENT_DIR/variables.sh"

## Functions

# if user cancels
on_cancel()
{
tmux display-message "Cancelling monitoring..."
# Wait a bit for all pane monitors to complete
sleep "$monitor_sleep_duration_value"

# Preform cleanup operation is monitoring was canceled
if [[ -f "$PID_FILE_PATH" ]]; then
kill "$PID"
rm "${PID_DIR}/${PANE_ID}.pid"
fi
exit 0
}
trap 'on_cancel' TERM
Expand All @@ -26,49 +32,64 @@ verbose_enabled() {

## Main script

# get id of the current active pane
PANEID=$(tmux list-panes | grep "active" | awk -F\] '{print $3}' | awk '{print $1}')
PID_DIR=~/.tmux/notify
# Get pane id
SESSION_NR=$(tmux list-sessions | grep "(attached)" | awk '{print $1}' | tr -d :)
WINDOW_NR=$(tmux list-windows | grep "(active)" | awk '{print $1}' | tr -d :)
PANE_NR=$(tmux list-panes | grep "active" | awk -F\] '{print $3}' | awk '{print $1}' | tr -d %)
PANE_ID=$(detox_file_name "s_${SESSION_NR}_w${WINDOW_NR}_p${PANE_NR//%}")
PID_FILE_PATH="${PID_DIR}/${PANE_ID}.pid"

# write pid to file
echo "$$" > "$PID_DIR/$PANEID".pid
# Monitor pane if it is not already monitored
if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored

# Display tnotify start messsage
tmux display-message "Montoring pane..."
# job started - create pid-file
echo "$$" > "$PID_FILE_PATH"

# Construct finish message
if verbose_enabled; then # If @tnotify-verbose is disabled
complete_message="Tmux pane task completed!"
else # If @tnotify-verbose is enabled
verbose_msg_value="$(get_tmux_option "$verbose_msg_option" "$verbose_msg_default")"
complete_message=$(tmux display-message -p "$verbose_msg_value")
fi
# Display tnotify start messsage
tmux display-message "Montoring pane..."

# Construct tnotify finish message
if verbose_enabled; then # If @tnotify-verbose is disabled
complete_message="Tmux pane task completed!"
else # If @tnotify-verbose is enabled
verbose_msg_value="$(get_tmux_option "$verbose_msg_option" "$verbose_msg_default")"
complete_message=$(tmux display-message -p "$verbose_msg_value")
fi

# Check process status every 10 seconds to see if has is finished
while true; do

# capture pane output
output=$(tmux capture-pane -pt "$PANE_NR")

# run tests to determine if work is done
# if so, break and notify
lc=$(echo "$output" | tail -c2)
case $lc in
"$" | "#" )
# notify-send does not always work due to changing dbus params
# see https://superuser.com/questions/1118878/using-notify-send-in-a-tmux-session-shows-error-no-notification#1118896
notify-send "$complete_message"
# trigger visual bell
# your terminal emulator can be setup to set URGENT bit on visual bell
# for eg, Xresources -> URxvt.urgentOnBell: true
tmux split-window "echo -e \"\a\" && exit"
break
esac

# Check process status every 10 seconds
while true; do

# capture pane output
output=$(tmux capture-pane -pt $PANEID)

# run tests to determine if work is done
# if so, break and notify
lc=$(echo $output | tail -c2)
case $lc in
"$" | "#" )
# notify-send does not always work due to changing dbus params
# see https://superuser.com/questions/1118878/using-notify-send-in-a-tmux-session-shows-error-no-notification#1118896
notify-send "$complete_message"
# trigger visual bell
# your terminal emulator can be setup to set URGENT bit on visual bell
# for eg, Xresources -> URxvt.urgentOnBell: true
tmux split-window "echo -e \"\a\" && exit"
break
esac

# Sleep for a given time
monitor_sleep_duration_value=$(get_tmux_option "$monitor_sleep_duration" "$monitor_sleep_duration_default")
sleep $monitor_sleep_duration_value
done

# job done - remove pid file
rm "$PID_DIR/$PANEID".pid
# Sleep for a given time
monitor_sleep_duration_value=$(get_tmux_option "$monitor_sleep_duration" "$monitor_sleep_duration_default")
sleep "$monitor_sleep_duration_value"
done

# job done - remove pid file and return
if [[ -f "$PID_FILE_PATH" ]]; then
rm "$PID_FILE_PATH"
fi
exit 0
else # If pane is already being monitored

# Display pane already monitored message
tmux display-message "Pane already monitored..."
exit 0
fi
17 changes: 10 additions & 7 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env bash
## -- Add tmux plugin variables
SUPPORTED_VERSION="1.9"

## Main variables
export SUPPORTED_VERSION="1.9"
export PID_DIR=~/.tmux/notify

## Tnotify tmux options

# Notification verbosity settings
verbose_option="@tnotify-verbose"
verbose_default="off"
verbose_msg_option="@tnotify-verbose-msg"
verbose_msg_default="(#S, #I:#P) Tmux pane task completed!"
export verbose_option="@tnotify-verbose"
export verbose_default="off"
export verbose_msg_option="@tnotify-verbose-msg"
export verbose_msg_default="(#S, #I:#P) Tmux pane task completed!"

# Monitor checker interval
monitor_sleep_duration="@tnotify-sleep-duration"
monitor_sleep_duration_default=10
export monitor_sleep_duration="@tnotify-sleep-duration"
export monitor_sleep_duration_default=10
8 changes: 4 additions & 4 deletions tnotify.tmux
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env bash

# Initialize variables
source "$CURRENT_DIR/scripts/helpers.sh"
source "$CURRENT_DIR/scripts/variables.sh"

# Get current directory
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PID_DIR=~/.tmux/notify

# Initialize variables
source "$CURRENT_DIR/scripts/helpers.sh"
source "$CURRENT_DIR/scripts/variables.sh"

# prepare pid file directory
if [[ ! -d $PID_DIR ]]; then
mkdir $PID_DIR
Expand Down

0 comments on commit 941ac00

Please sign in to comment.