diff --git a/README.md b/README.md index 45bc902..91545d6 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/scripts/cancel.sh b/scripts/cancel.sh index 848fdec..4c72324 100755 --- a/scripts/cancel.sh +++ b/scripts/cancel.sh @@ -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 -} \ No newline at end of file +fi diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 44c78ff..67e8169 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -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() { @@ -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._-]/_}" +} diff --git a/scripts/notify.sh b/scripts/notify.sh index ad563e6..6cb3682 100755 --- a/scripts/notify.sh +++ b/scripts/notify.sh @@ -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 @@ -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 diff --git a/scripts/variables.sh b/scripts/variables.sh index a14b36c..bd49930 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -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 diff --git a/tnotify.tmux b/tnotify.tmux index 1ed0644..b677173 100755 --- a/tnotify.tmux +++ b/tnotify.tmux @@ -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