Skip to content

Instantly share code, notes, and snippets.

@rootiest
Last active October 24, 2024 14:21
Show Gist options
  • Save rootiest/4f04bfb25895c91cfd5dc5a5647693b2 to your computer and use it in GitHub Desktop.
Save rootiest/4f04bfb25895c91cfd5dc5a5647693b2 to your computer and use it in GitHub Desktop.
Toastify WezTerm
---@function Helper function to check if a value exists in a table
---@param tab table The table to search in
---@param val any The value to search for in the table
---@return boolean exists True if the value is found in the table, 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
---@function Function to send a notification using toastify
---@param title string The title of the notification
---@param body? string The body of the notification
---@param timeout? number Time in milliseconds before the notification expires
---@param options? table A table containing additional optional settings for the notification
local function notify(title, body, timeout, options)
-- Default values for optional fields
-- Initialize 'options' as an empty table if not provided
options = options or {}
-- Retrieve specific options from the 'options' table, or set to nil/default if not provided
local app_name = options.app_name or nil
local categories = options.categories or nil
local icon = options.icon or nil
local sound_name = options.sound_name or nil
local urgency = options.urgency or "normal" -- Default urgency is 'normal'
local hint = options.hint or nil
local debug = options.debug and "--debug" or nil -- Add '--debug' flag if debug is true
-- 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)
-- 'toastify send' is the base command, with 'title' as a required argument
local cmd = { "toastify", "send", title }
-- 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 timeout then
table.insert(cmd, "--expire-time")
table.insert(cmd, tostring(timeout)) -- 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 categories then
table.insert(cmd, "--categories")
table.insert(cmd, categories)
end
if icon then
table.insert(cmd, "--icon")
table.insert(cmd, icon)
end
if sound_name then
table.insert(cmd, "--sound-name")
table.insert(cmd, sound_name)
end
if hint then
table.insert(cmd, "--hint")
table.insert(cmd, hint)
end
-- Append the '--debug' flag if debug mode is enabled
if debug then
table.insert(cmd, debug)
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' takes a table of strings and executes it as a command
wezterm.run_child_process(cmd)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment