Estimated reading time: 5 minutes

Simple notifications in awesome

On seeing my window notifications in awesome from a screencast for a new project at work Matt Cooper asks:

Is there a library that does those fancy notifications or do you have to hack it up yourself?

notifications in awesome

There is a library to make notification popups in awesome, and it is called naughty. With it notifications are as simple as calling naughty.notify, for example:

Note

I use moonscript for all my lua needs, as it provides a nicer syntax and fixes some of the warts(such as global-by-default). The examples below are all written in moonscript.

require "naughty"

naughty.notify text: "my little popup", position: "bottom_left"

I do however define a few functions in my configuration file to simplify the normal notifications I use:

-- Generic info/warn/error notifications
debug_messages = false
notify =
    -- debug_notify: Display notification when debug_messages is true
    debug: (text) ->
        if debug_messages
            naughty.notify text: "<span color='#ff00ff'>Debug</span>: #{awful.util.escape text}",
                timeout: 10, width:350,

    _gnotify: (ntype, text) ->
        colour = switch ntype
            when "info"
                "#00ff00"
            when "warn"
                "#ffff00"
            when "error"
                "#ff0000"
            else
                beautiful.fg_normal
        naughty.notify text: "<span color='#{colour}'>●</span> #{awful.util.escape text}",

    start: (text) ->
        notify._gnotify "info", text,
    stop: (text) ->
        notify._gnotify "error", text,
    warn: (text) ->
        notify._gnotify "warn", text,

Note

If you’re willing to install lua-functional, the above code snippet can be made much nicer by using its partial application support to define the final functions.

This group of functions, all namespaced under notify, provide simple wrappers for the everyday notifications I use. notify.debug is a nice way to be able to litter your configuration file with visible pointers as to what is going on, set debug_messages = true in your rc.moon to enable all the debug information and switch it back to false to stop it being displayed.

The notify.{start,stop,warn} functions prepend the text you pass it with a coloured Unicode bullet. awesome makes use of pango meaning you can easily use pretty much any character you wish, or more specifically any character your font can display. From time to time I’ve experimented with using and , but most of the fonts I prefer to use don’t display them correctly(if you’re seeing two boxes your fonts don’t either).

Window creation notifications

One of the notifications I like to have is for when new windows are opened, this way I don’t miss windows opening on tags I’m not currently viewing.

client.connect_signal "manage", (using nil) =>
    -- Display the window’s name, or just Application if it isn’t set
    notify.start "#{@name or 'Application'} started"

Network notifications

I also have the following gears timer set to toggle my network monitor between lo and ppp0 depending on whether my remote network interface is up. Having the little popups to show when the network has gone down or come up is quite nice, and definitely more noticeable than just changing the text in the wibox. The code below changes the interface name in the wibox, and switches the network graph widget to use the appropriate input too.

netiface = "lo"
gears.timer 3, ->
    iface, state = if netiface == "lo" and io.open "/var/lock/LCK..ttyUSB0"
        "ppp0", "up"
    elseif netiface == "ppp0" and not io.open "/var/lock/LCK..ttyUSB0"
        "lo", "down"

    nettext_widget.text = " #{iface}:"
    wicked.register netbar_widget, "net", "${#{iface} up_b}", 3, "upload"
    wicked.register netbar_widget, "net", "${#{iface} down_b}", 3,
        "download"
    notify.start "PPP0 interface has come #{state}"

Authenticate this page by pasting this signature into Keybase.

Have a suggestion or see a typo? Edit this page