Skip to content

Commit

Permalink
Workaround bug in terminal-notifier by adding configurable timeout
Browse files Browse the repository at this point in the history
- Previously, the notifications would only work once.
- This seems to be related to `terminal-notifier` not exiting automatically after displaying the notification.
- The issue is related to a bug where `terminal-notifier` is waiting for the user to close or otherwise give input to the notification, as if it was an alert with actions.
- The bug has not been fixed upstream since it was first reported several months ago.
- One workaround is to add a timeout, so the notification "closes itself".
- Made the timeout configurable through `configNotificationTimeout`.

See

- julienXX/terminal-notifier#223
  • Loading branch information
joelpurra committed Oct 22, 2017
1 parent aed236c commit e247423
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
7 changes: 7 additions & 0 deletions USAGE.md
Expand Up @@ -233,3 +233,10 @@ Settings are read from `~/.np/config.sh`. The format is one `setting=value` per

- Default is "false".
- Follow sound file symlinks before passing the path to the external player.


### `configNotificationTimeout`

- Default is "3".
- Set to a positive integer, or "0" to manually close the notification.
- Time before the sound notifications close automatically.
6 changes: 6 additions & 0 deletions src/help/np-help.txt
Expand Up @@ -64,4 +64,10 @@ Configuration:
Values: "true" or "false"
Default: "false"

configNotificationTimeout
Time before the sound notifications close automatically.

Values: a positive integer, or "0" to disable the timeout.
Default: "3"

See 'np help <command>' for more information on a specific command.
3 changes: 3 additions & 0 deletions src/shared/functionality.sh
Expand Up @@ -72,6 +72,9 @@ configOrder="$configDefaultOrder"
configDefaultFollowSymlinks="false"
configFollowSymlinks="$configDefaultFollowSymlinks"

configDefaultNotificationTimeout="3"
configNotificationTimeout="$configDefaultNotificationTimeout"


# Allow $configConfigFile to override above configuration.
readConfig
4 changes: 3 additions & 1 deletion src/shared/functions/cross-platform.sh
Expand Up @@ -60,7 +60,9 @@ waitForFileChange() {
# https://github.com/emcrisostomo/fswatch
# Should cover all systems
[[ -z "$fswatchExec" ]] && die "could not find 'fswatch'"


debug "Waiting for change in '$@': '$watched'"

local watched=$("$fswatchExec" --one-event "$@")

debug "Detected change in '$@': '$watched'"
Expand Down
34 changes: 32 additions & 2 deletions src/shared/functions/notify.sh
Expand Up @@ -41,11 +41,41 @@ notify() {

case "$notifyExec" in
'terminal-notifier')
terminal-notifier -group "np" -title "$title" -open "https://github.com/joelpurra/npshell" -subtitle "$subtitle" -message "$message" >/dev/null
# Currently not awaiting exit/output as it depends on the configurable timeout.
(
# NOTE: forcefully close any previous notification, in case it wasn't already gone.
terminal-notifier \
-remove "np" \
-json >/dev/null

# TODO: use the JSON output if the user clicks close/show/custom action?
# The closeLabel and alert actions are only shown if the user allows it to.
# - Open System Preferences > Notification.
# - Select "terminal-notifier" (or perhaps "np") in the sidebar.
# - Select the "Alerts" alert style.
# https://github.com/julienXX/terminal-notifier/raw/master/assets/System_prefs.png
#
# NOTE: Setting timeout due to bug in terminal-notifier.
# https://github.com/julienXX/terminal-notifier/issues/223
terminal-notifier \
-group "np" \
-title "$title" \
-open "https://github.com/joelpurra/npshell" \
-subtitle "$subtitle" \
-message "$message" \
-timeout "$configNotificationTimeout" \
-json >/dev/null
) &
;;
'growlnotify')
# http://growl.info/downloads
# Untested.
growlnotify --noteName "${title} - ${subtitle}" --identifier "np" --url "https://github.com/joelpurra/npshell" --message "$message" >/dev/null
# Does not seem to support a timeout argument.
growlnotify \
--noteName "${title} - ${subtitle}" \
--identifier "np" \
--url "https://github.com/joelpurra/npshell" \
--message "$message" >/dev/null
;;
*)
die "no notifier executable found."
Expand Down
9 changes: 9 additions & 0 deletions src/shared/man/man1/np.1
Expand Up @@ -233,3 +233,12 @@ Default is "false".
.IP \(bu 2
Follow sound file symlinks before passing the path to the external player.
.RE
.SS \fB\fCconfigNotificationTimeout\fR
.RS
.IP \(bu 2
Default is "3".
.IP \(bu 2
Set to a positive integer, or "0" to manually close the notification.
.IP \(bu 2
Time before the sound notifications close automatically.
.RE

0 comments on commit e247423

Please sign in to comment.