Skip to content

Commit

Permalink
🐛 Fixes bug #7 which allowed for multiple simultaneous pane monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rickstaa committed Sep 29, 2020
1 parent 860be5d commit 9316940
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 54 deletions.
31 changes: 22 additions & 9 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 )"

# Source helpers and variables
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/variables.sh"

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

# Cancel monitor process if active
{ # Try

# consult pid file for the pid
PID=$(cat "$PID_DIR/$PANEID".pid)
# Consult pid file for the pid
PID=$(cat "${PID_DIR}/${PANE_ID}.pid")

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

# job done - kill process and remove pid file
kill $PID
rm "$PID_DIR/$PANEID".pid
# Display success message
tmux display-message "Pane monitoring canceled..."

} || { # Catch
tmux display-message "Pane not monitored..."
exit 0
}
}
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 | sed -e 's/[^A-Za-z0-9._-]/_/g'
}
102 changes: 57 additions & 45 deletions scripts/notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,61 @@ 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

# write pid to file
echo "$$" > "$PID_DIR/$PANEID".pid

# Display tnotify start messsage
tmux display-message "Montoring pane..."

# 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
# 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//%}")

# Monitor pane if it is not already monitored
if [[ ! -f "${PID_DIR}/${PANE_ID}.pid" ]]; then # If pane not yet monitored

# job started - create pid-file
echo "$$" > "${PID_DIR}/${PANE_ID}.pid"

# 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

# 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
# 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

# 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
rm "${PID_DIR}/${PANE_ID}.pid"
# exit 0
else # If pane is already being monitored

# Display pane already monitored message
tmux display-message "Pane already monitored..."
exit 0
fi
3 changes: 3 additions & 0 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
## -- Add tmux plugin variables

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

## Tnotify tmux options

Expand Down

0 comments on commit 9316940

Please sign in to comment.