Skip to content

Instantly share code, notes, and snippets.

@CodingWithAnxiety
Last active January 23, 2025 07:09
Show Gist options
  • Save CodingWithAnxiety/a582c1d504ee279092010aac10c31f94 to your computer and use it in GitHub Desktop.
Save CodingWithAnxiety/a582c1d504ee279092010aac10c31f94 to your computer and use it in GitHub Desktop.
Libnotify Wezterm
--- A notification module for WezTerm using notify-send.
-- This module provides functionality to send notifications using the 'notify-send' command,
-- which is available on most Linux distributions that implement the org.freedesktop.Notifications specification.
local wezterm = require 'wezterm'
local M = {}
--- Check if a value exists in a table.
-- @local
-- @param tab table The table to search
-- @param val any The value to search for
-- @return boolean True if the value is found, false otherwise
local function has_value(tab, val)
for _, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
--- Send a notification using 'notify-send'.
-- @param summary string The summary ('title') of the notification
-- @param body string|nil The body of the notification
-- @param expire number|nil The duration, in milliseconds, for the notification to appear on screen. Some implementations may ignore this.
-- @param options table|nil A table containing additional options. View notify-send manpage for more details.
-- @usage
-- local notify = require('notify')
-- notify.send("Hello", "World", 5000, {urgency = "normal"})
function M.send(summary, body, expire, options)
-- Default values for optional fields
options = options or {}
-- Retrieve specific options from the 'options' table, or set to nil/default if not provided
local app_name = options.app_name
local category = options.category
local icon = options.icon
local urgency = options.urgency or "normal" -- Default urgency is 'normal'
local hint = options.hint
-- Allowed urgency levels for validation
local allowed_urgency = { "low", "normal", "critical" }
-- If the provided urgency is not one of the allowed values, default to 'normal'
if not has_value(allowed_urgency, urgency) then
urgency = "normal"
end
-- Start building the command as a table of strings (to execute via wezterm)
local cmd = { "notify-send", summary }
-- Append the body if provided (body is optional)
if body then
table.insert(cmd, body)
end
-- Append the expire time if provided (expire_time is optional)
if expire then
table.insert(cmd, "--expire-time")
table.insert(cmd, tostring(expire)) -- Convert to string for command execution
end
-- Append optional arguments if provided, using the corresponding flag
if app_name then
table.insert(cmd, "--app-name")
table.insert(cmd, app_name)
end
if category then
table.insert(cmd, "--category")
table.insert(cmd, category)
end
if icon then
table.insert(cmd, "--icon")
table.insert(cmd, icon)
end
if hint then
table.insert(cmd, "--hint")
table.insert(cmd, hint)
end
-- Always include the urgency flag with the validated urgency value
table.insert(cmd, "--urgency")
table.insert(cmd, urgency)
-- Execute the command using wezterm's built-in child process runner
wezterm.run_child_process(cmd)
end
return M

LibNotify Westerm

I was unhappy, personally, with the usage of window:toast_notification inside of wezterm configs, however not long ago, I stumbled across an absolutely lovely gist called toastify wezterm. This however, came with new issues: Toastify Wezterm requires toastify. To work around this issue, I have created libnotify wezterm, aka notify.lua.

This module (Which, currently only has one function, pfft...) uses notify-send (Installed on most distros by libnotify) to send the notification. Please attribute credit to the original author of toastify wezterm!

Module notify

A notification module for WezTerm using notify-send.

This module provides functionality to send notifications using the 'notify-send' command, which is available on most Linux distributions that implement the org.freedesktop.Notifications specification.

Functions

send (summary, body, expire, options)

Send a notification using 'notify-send'.

Parameters:

  • summary : string The summary ('title') of the notification
  • body : string|nil The body of the notification
  • expire : number|nil The duration, in milliseconds, for the notification to appear on screen. Some implementations may ignore this.
  • options : table|nil A table containing additional options. View notify-send manpage for more details.

Usage:

local notify = require('notify')
notify.send("Hello", "World", 5000, {urgency = "normal"})

Examples:

-- Pull WEZAPI
local wezterm = require 'wezterm'
local act = wezterm.action

-- Pull notify
local notify = require('notify')

wezterm.on('reload_kb', function(window, pane)
    wezterm.reload_configuration()
    notify.send("Config reloaded!", "Configuration has been reloaded.", 5000, {
      urgency = "normal",
      category = "device",
      expire = 5000,
      app_name = "Wezterm",
      hint = "string:desktop-entry:org.wezfurlong.wezterm"
  })

end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment