ENOSUCHBLOG

Programming, philosophy, pedaling.


Introducing notify.cr

Jun 18, 2018     Tags: crystal, devblog, programming    

This post is at least a year old.

This is just a quick announcement for notify.cr, my latest Crystal library (“shard”).

notify.cr is a library for Desktop Notifications, the standard used by most Linux desktops to provide the user with notifications. It connects to the user’s notification daemon via DBus, and should work with any complaint daemon (e.g., dunst).

Installation and usage:

The easiest way to install notify.cr is via Crystal’s shards dependency manager. You can find the relevant steps in the repository README.

In use, notify.cr has only five methods: #capabilities, #capability?, #information, #notify, and #close.

#capabilities and #capability? are used to perform feature checking. #capabilities returns the underlying feature list, while #capability? checks for the availability of any particular feature.

1
2
3
4
5
6
require "notify"

notifier = Notify.new

notifier.capabilities # => ["body", "body-markup", "body-hyperlinks"]
notifier.capability? "body" # => true

#information returns a list of strings, indicating vendor information:

1
notifier.information # => ["mynotifier", "authorname", "1.0"]

Finally, #notify, and #close do the main work of displaying and hiding notifications from the user. #notify returns a UInt32 that identifies the displayed notification, allowing it to be closed via #close or overwritten by a subsequent #notify call:

1
2
3
4
5
6
7
id = notifier.notify "hello", body: "world!", expire_timeout: 5000

# replace the previous notification with this one *without* creating a new ID
notifier.notify "look ma", body: "no flicker!", replaces_id: id

# finally, close the notification if it's still on the screen
notifier.close id

expire_timeout and replaces_id are just two of several parameters that #notify can take. Check out the API documentation for more.

Thanks for reading!


Discussions: Reddit