Group repeated messages based on notification ids #1892
Replies: 1 comment 5 replies
-
---@module 'snacks'
local notifs = {}
---@param msg string
---@param title? string
---@param level? string|number
local key = function(msg, title, level)
return string.format('msg:%s,title:%s,level:%s', msg, title or '', level or '')
end
---@param notif snacks.notifier.Notif
local use_count = function(notif)
local get_count = function()
return notifs[key(notif.msg, notif.title, notif.level)] or 0
end
---@param num integer
local set_count = function(num)
notifs[key(notif.msg, notif.title, notif.level)] = num
end
return get_count, set_count
end
return {
'folke/snacks.nvim',
config = function(_, spec_opts)
require('snacks').setup(spec_opts)
--
local notify = Snacks.notifier.notify
Snacks.notifier.notify = function(msg, level, opts)
opts = opts or {}
opts = vim.tbl_deep_extend('keep', opts, {
id = key(msg, opts.title, opts.level),
})
return notify(msg, level, opts)
end
end,
---@type snacks.Config
opts = {
notifier = {
---@param buf integer
---@param notif snacks.notifier.Notif
---@param ctx snacks.notifier.ctx
style = function(buf, notif, ctx)
local get_count, set_count = use_count(notif)
if notif.shown then
set_count(get_count() + 1)
else
set_count(1)
end
-- Compat derived style
ctx.opts.title = {}
if get_count() > 1 then
table.insert(ctx.opts.title, { string.format('(%sx)', get_count()) })
end
--
local title = vim.trim(notif.icon .. ' ' .. (notif.title or ''))
if title ~= '' then
table.insert(ctx.opts.title, { ' ' .. title .. ' ', ctx.hl.title })
ctx.opts.title_pos = 'center'
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(notif.msg, '\n'))
end,
},
},
} |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
tlmp59
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
One feature that i really enjoy using in fidget-nvim is that it automatically group and count number of instances of the same messages. Is there a way that i can replicate this in notifier?

Beta Was this translation helpful? Give feedback.
All reactions