Skip to content

Commit

Permalink
[Feature] Optional notification title (#41)
Browse files Browse the repository at this point in the history
* Added optional notification title

* refactor: small code improvements

---------

Co-authored-by: rickstaa <[email protected]>
  • Loading branch information
TrimVis and rickstaa committed Aug 15, 2023
1 parent 977b244 commit 411f535
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ To change the verbose notification text, put `set -g @tnotify-verbose-msg 'put y
* `#I`: Window index
* `#W`: Window name

For the complete 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).
For the complete 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). You can also add a notification title using `set -g @tnotify-verbose-title`. Doing so will move the verbose notification text into the notification body.

### Change monitor update period

Expand Down
1 change: 0 additions & 1 deletion scripts/cancel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ source "${CURRENT_DIR}/variables.sh"

# Cancel pane monitoring if active
if [[ -f "$PID_FILE_PATH" ]]; then

# Retrieve monitor process PID
PID=$(cat "$PID_FILE_PATH")

Expand Down
18 changes: 13 additions & 5 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,28 @@ send_telegram_message() {
}

# Send notification
# Usage: notify <message> <send_telegram>
# Usage: notify <message> <title> <send_telegram>
notify() {
# Switch notification method based on OS
if [[ "$OSTYPE" =~ ^darwin ]]; then # If macOS
osascript -e 'display notification "'"$1"'" with title "tmux-notify"'
if [ -n "$2" ]; then
osascript -e 'display notification "'"$1"'" with title "'"$2"'"'
else
osascript -e 'display notification "'"$1"'" with title "tmux-notify"'
fi
else
# 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 "$1"
if [ -n "$2" ]; then
notify-send "$2" "$1"
else
notify-send "$1"
fi
fi

# Send telegram message if telegram variables are set, and telegram alert all is
# enabled or if the $2 argument is set to true
if telegram_available && (telegram_all_enabled || [ "$2" == "true" ]); then
# enabled or if the $3 argument is set to true
if telegram_available && (telegram_all_enabled || [ "$3" == "true" ]); then
telegram_bot_id="$(get_tmux_option "$tmux_notify_telegram_bot_id" "$tmux_notify_telegram_bot_id_default")"
telegram_chat_id="$(get_tmux_option "$tmux_notify_telegram_channel_id" "$tmux_notify_telegram_channel_id_default")"
send_telegram_message $telegram_bot_id $telegram_chat_id "$1"
Expand Down
6 changes: 3 additions & 3 deletions scripts/notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ trap 'on_cancel' TERM

# Monitor pane if it is not already monitored
if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored

# job started - create pid-file
echo "$$" > "$PID_FILE_PATH"

Expand All @@ -41,6 +40,8 @@ if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored
if verbose_enabled; then # 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")
verbose_msg_title="$(get_tmux_option "$verbose_title_option" "$verbose_title_default")"
complete_title=$(tmux display-message -p "$verbose_msg_title")
else # If @tnotify-verbose is disabled
complete_message="Tmux pane task completed!"
fi
Expand All @@ -56,7 +57,6 @@ if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored

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

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

Expand All @@ -69,7 +69,7 @@ if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored
tmux select-window -t @"$WINDOW_ID"
tmux select-pane -t %"$PANE_ID"
fi
notify "$complete_message" $2
notify "$complete_message" "$complete_title" $2
break
fi

Expand Down
2 changes: 2 additions & 0 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ 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!"
export verbose_title_option="@tnotify-verbose-title"
export verbose_title_default=""

# Monitor checker interval
export monitor_sleep_duration="@tnotify-sleep-duration"
Expand Down

0 comments on commit 411f535

Please sign in to comment.