Skip to content

Instantly share code, notes, and snippets.

@CodingWithAnxiety
Last active January 23, 2025 07:09

Revisions

  1. CodingWithAnxiety revised this gist Jan 23, 2025. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions notify.lua
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,6 @@
    --- 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.
    -- @module notify
    -- @author [Your Name]
    -- @license [Your License]
    -- @copyright [Your Copyright]

    local wezterm = require 'wezterm'

  2. CodingWithAnxiety revised this gist Dec 11, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion notify.lua
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ function M.send(summary, body, expire, options)
    table.insert(cmd, body)
    end

    -- Append the timeout if provided (timeout is optional)
    -- 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
  3. CodingWithAnxiety renamed this gist Dec 11, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion man.md → ntfy_wezterm_man.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## Hello!
    #### 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](https://gist.github.com/rootiest/4f04bfb25895c91cfd5dc5a5647693b2).
    This however, came with new issues: Toastify Wezterm requires toastify. To work around this issue, I have created `libnotify wezterm`, aka `notify.lua`.
  4. CodingWithAnxiety revised this gist Dec 11, 2024. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions man.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    ## Hello!
    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](https://gist.github.com/rootiest/4f04bfb25895c91cfd5dc5a5647693b2).
    This however, came with new issues: Toastify Wezterm requires toastify. To work around this issue, I have created `libnotify wezterm`, aka `notify.lua`.
    Please attribute credit to the original author of 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`
    ===============
  5. CodingWithAnxiety created this gist Dec 11, 2024.
    55 changes: 55 additions & 0 deletions man.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    ## Hello!
    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](https://gist.github.com/rootiest/4f04bfb25895c91cfd5dc5a5647693b2).
    This however, came with new issues: Toastify Wezterm requires toastify. To work around this issue, I have created `libnotify wezterm`, aka `notify.lua`.
    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:
    ```lua
    local notify = require('notify')
    notify.send("Hello", "World", 5000, {urgency = "normal"})
    ```

    ### Examples:

    ```lua
    -- 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)
    ```
    96 changes: 96 additions & 0 deletions notify.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    --- 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.
    -- @module notify
    -- @author [Your Name]
    -- @license [Your License]
    -- @copyright [Your Copyright]

    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 timeout if provided (timeout 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