Skip to content

Commit

Permalink
feat: add update_hook option to notification group Config
Browse files Browse the repository at this point in the history
  • Loading branch information
j-hui committed Jan 8, 2024
1 parent 2b44812 commit 0fb9e3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lua/fidget/notification.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ local logger = require("fidget.logger")
---
--- If both name and icon are nil, then no group header is rendered.
---
--- Note that the actual `|fidget.notification.default_config|` defines a few
--- The `update_hook` function can be used to dynamically adjust fields of
--- a |fidget.notification.Item|, e.g., to set the `hidden` field according to
--- the message. If set to `false`, nothing is done when an item is updated.
---
--- Note that the actual |fidget.notification.default_config| defines a few
--- more defaults than what is documented here, which pertain to the fallback
--- used if the corresponding field in the `default` config table is `nil`.
---
Expand All @@ -61,6 +65,7 @@ local logger = require("fidget.logger")
---@field error_annote string|nil Default annotation for error items
---@field priority number|nil Order in which group should be displayed; defaults to `50`
---@field skip_history boolean|nil Whether messages should be preserved in history
---@field update_hook fun(item: Item)|false|nil Called when an item is updated; defaults to `false`

--- Notification element containing a message and optional annotation.
---
Expand Down Expand Up @@ -116,6 +121,9 @@ local state = {
--- :lua print(vim.inspect(require("fidget.notification").default_config))
---<
---
--- See also:~
--- |fidget.notification.Config|
---
---@type Config
notification.default_config = {
name = "Notifications",
Expand All @@ -132,6 +140,7 @@ notification.default_config = {
info_annote = "INFO",
warn_annote = "WARN",
error_annote = "ERROR",
update_hook = false,
}

---@options notification [[
Expand Down
6 changes: 6 additions & 0 deletions lua/fidget/notification/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ function M.update(now, configs, state, msg, level, opts)
last_updated = now,
data = opts.data,
}
if group.config.update_hook then
group.config.update_hook(new_item)
end
table.insert(group.items, new_item)
else
-- Item with the same key already exists; update it in place
Expand All @@ -256,6 +259,9 @@ function M.update(now, configs, state, msg, level, opts)
item.skip_history = opts.skip_history or item.skip_history
item.last_updated = now
item.data = opts.data ~= nil and opts.data or item.data
if group.config.update_hook then
group.config.update_hook(item)
end
end

if new_index then
Expand Down

0 comments on commit 0fb9e3f

Please sign in to comment.