feat(notifier): allow overriding hl groups per notification

This commit is contained in:
Folke Lemaitre 2024-11-07 11:08:31 +01:00
parent 4e0ce77f2f
commit 8bcb2bc805
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 26 additions and 20 deletions

View file

@ -173,6 +173,7 @@ Notification options
---@field keep? fun(notif: snacks.notifier.Notif): boolean ---@field keep? fun(notif: snacks.notifier.Notif): boolean
---@field style? snacks.notifier.style ---@field style? snacks.notifier.style
---@field opts? fun(notif: snacks.notifier.Notif) -- dynamic opts ---@field opts? fun(notif: snacks.notifier.Notif) -- dynamic opts
---@field hl? snacks.notifier.hl -- highlight overrides
< <
Notification object Notification object
@ -211,7 +212,7 @@ RENDERING *snacks-notifier-types-rendering*
>lua >lua
---@class snacks.notifier.ctx ---@class snacks.notifier.ctx
---@field opts snacks.win.Config ---@field opts snacks.win.Config
---@field notifier snacks.notifier ---@field notifier snacks.notifier.Class
---@field hl snacks.notifier.hl ---@field hl snacks.notifier.hl
---@field ns number ---@field ns number
< <

View file

@ -162,6 +162,7 @@ Notification options
---@field keep? fun(notif: snacks.notifier.Notif): boolean ---@field keep? fun(notif: snacks.notifier.Notif): boolean
---@field style? snacks.notifier.style ---@field style? snacks.notifier.style
---@field opts? fun(notif: snacks.notifier.Notif) -- dynamic opts ---@field opts? fun(notif: snacks.notifier.Notif) -- dynamic opts
---@field hl? snacks.notifier.hl -- highlight overrides
``` ```
Notification object Notification object
@ -199,7 +200,7 @@ Notification object
```lua ```lua
---@class snacks.notifier.ctx ---@class snacks.notifier.ctx
---@field opts snacks.win.Config ---@field opts snacks.win.Config
---@field notifier snacks.notifier ---@field notifier snacks.notifier.Class
---@field hl snacks.notifier.hl ---@field hl snacks.notifier.hl
---@field ns number ---@field ns number
``` ```

View file

@ -27,6 +27,7 @@ local M = setmetatable({}, {
---@field keep? fun(notif: snacks.notifier.Notif): boolean ---@field keep? fun(notif: snacks.notifier.Notif): boolean
---@field style? snacks.notifier.style ---@field style? snacks.notifier.style
---@field opts? fun(notif: snacks.notifier.Notif) -- dynamic opts ---@field opts? fun(notif: snacks.notifier.Notif) -- dynamic opts
---@field hl? snacks.notifier.hl -- highlight overrides
--- Notification object --- Notification object
---@class snacks.notifier.Notif: snacks.notifier.Notif.opts ---@class snacks.notifier.Notif: snacks.notifier.Notif.opts
@ -54,7 +55,7 @@ local M = setmetatable({}, {
---@class snacks.notifier.ctx ---@class snacks.notifier.ctx
---@field opts snacks.win.Config ---@field opts snacks.win.Config
---@field notifier snacks.notifier ---@field notifier snacks.notifier.Class
---@field hl snacks.notifier.hl ---@field hl snacks.notifier.hl
---@field ns number ---@field ns number
@ -309,6 +310,16 @@ function N:render(notif)
if type(notif.opts) == "function" then if type(notif.opts) == "function" then
notif.opts(notif) notif.opts(notif)
end end
---@type snacks.notifier.hl
local hls = vim.tbl_extend("force", {
title = hl("Title", notif.level),
icon = hl("Icon", notif.level),
border = hl("Border", notif.level),
footer = hl("Footer", notif.level),
msg = hl("", notif.level),
}, notif.hl or {})
local win = notif.win local win = notif.win
or Snacks.win({ or Snacks.win({
show = false, show = false,
@ -319,11 +330,11 @@ function N:render(notif)
noautocmd = true, noautocmd = true,
wo = { wo = {
winhighlight = table.concat({ winhighlight = table.concat({
"Normal:" .. hl("", notif.level), "Normal:" .. hls.msg,
"NormalNC:" .. hl("", notif.level), "NormalNC:" .. hls.msg,
"FloatBorder:" .. hl("Border", notif.level), "FloatBorder:" .. hls.border,
"FloatTitle:" .. hl("Title", notif.level), "FloatTitle:" .. hls.title,
"FloatFooter:" .. hl("Footer", notif.level), "FloatFooter:" .. hls.footer,
}, ","), }, ","),
}, },
keys = { keys = {
@ -338,21 +349,14 @@ function N:render(notif)
vim.api.nvim_buf_clear_namespace(buf, N.ns, 0, -1) vim.api.nvim_buf_clear_namespace(buf, N.ns, 0, -1)
local render = self:get_render(notif.style) local render = self:get_render(notif.style)
local ctx = { vim.bo[buf].modifiable = true
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {})
render(buf, notif, {
opts = win.opts, opts = win.opts,
notifier = self, notifier = self,
ns = N.ns, ns = N.ns,
hl = { hl = hls,
title = hl("Title", notif.level), })
icon = hl("Icon", notif.level),
border = hl("Border", notif.level),
footer = hl("Footer", notif.level),
msg = hl("", notif.level),
},
}
vim.bo[buf].modifiable = true
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {})
render(buf, notif, ctx)
vim.bo[buf].modifiable = false vim.bo[buf].modifiable = false
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)