alert
alert copied to clipboard
Support for :replaces-id in styles that support it
Say a package shows an alert on unread mail, showing the unread mail count every time the count increases. If I use :style 'notifications and I idle a bit, it might "queue" up a bunch of these and on unidling, I end up with a bunch of "2 unread", "3 unread", "ℵ0 unread" etc.
It'd be nice if we could use the :replaces-id from notifications-notify to replace old ones.
I think a nice API would be simply (alert "2 unread" :id 'my-special-id) and then alert internally does
(puthash 'my-special-id
(notifications-notify :body "2 unread"
:replaces-id (gethash 'my-special-id alert-ids))
alert-ids)
or something like that.
This sounds like a great idea!
https://github.com/jwiegley/alert/pull/61
If the style notifier is https://github.com/julienXX/terminal-notifier, then it seems like it can take a -group ID (no need to keep a hashmap for that one); I don't have a Mac to test that on though.
If growlnotify is the same as http://www.growlforwindows.com/gfw/help/growlnotify.aspx then this one seems usable: /c:coalesingid The coalescing id used to update/replace existing notifications.
I don't know if any other styles would support replacing.
Would it also make sense to add this as the :remover of the style? Ie.:
diff --git a/alert.el b/alert.el
index 9b5ce12..c699e33 100644
--- a/alert.el
+++ b/alert.el
@@ -798,8 +798,18 @@ by the `notifications' style.")
(puthash (plist-get info :id) id alert-notifications-ids)))
(alert-message-notify info))
+(defun alert-notifications-remove (info)
+ "Remove the `notifications-notify' message based on INFO :id."
+ (message "remove %S" info)
+ (let ((id (and (plist-get info :id)
+ (gethash (plist-get info :id) alert-notifications-ids))))
+ (when id
+ (notifications-close-notification id)
+ (remhash (plist-get info :id) alert-notifications-ids))))
+
(alert-define-style 'notifications :title "Notify using notifications"
- :notifier #'alert-notifications-notify))
+ :notifier #'alert-notifications-notify
+ :remover #'alert-notifications-remove))
(defcustom alert-notifier-command (executable-find "terminal-notifier")
One minor issue is that this makes it a bit confusing to test, since simply doing (alert "test test" :id "new-mail") from eval-expression will get immediately removed by the post-command-hook alert-remove-on-command, unless you also provide some :buffer to it.
OTOH, adding a :remover probably makes the alert styles act more like each other wrt. never-persist and buffer changes and such.
@jwiegley I find the above (defun alert-notifications-remove handy, but I'm not sure if having it as :remover can mess up things. Should I make a PR with just the defun for now?
Sure, that's safe enough to merge right away.