refactor(nvim): improve appearance of notifications

This commit is contained in:
Price Hiller 2024-03-13 21:55:50 -05:00
parent 4e406baa08
commit 6165c01128
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB

View File

@ -56,7 +56,6 @@ return {
},
},
dependencies = {
-- if you lazy-load any plugin below, make sure to add proper `module="..."` entries
"MunifTanjim/nui.nvim",
{
"rcarriga/nvim-notify",
@ -73,38 +72,79 @@ return {
desc = "Notifications: Dismiss",
},
},
opts = { -- Animation style ()
stages = "slide",
fps = 60,
config = function()
local base = require("notify.render.base")
-- Function called when a new window is opened, use for changing win settings/config
on_open = nil,
local opts = {
stages = "slide",
fps = 60,
on_open = function(win)
local buf = vim.api.nvim_win_get_buf(win)
local ft = vim.bo[buf].filetype
if ft == "" or ft == "notify" then
vim.api.nvim_set_option_value(
"filetype",
"markdown",
{ buf = vim.api.nvim_win_get_buf(win) }
)
vim.api.nvim_set_option_value("wrap", true, { win = win })
end
end,
render = function(bufnr, notif, highlights, _)
local left_icon = notif.icon .. " "
local max_message_width = math.max(math.max(unpack(vim.tbl_map(function(line)
return vim.fn.strchars(line)
end, notif.message))))
local right_title = notif.title[2]
local left_title = notif.title[1]
local title_accum = vim.str_utfindex(left_icon)
+ vim.str_utfindex(right_title)
+ vim.str_utfindex(left_title)
-- Function called when a window is closed
on_close = nil,
local left_buffer = string.rep(" ", math.max(0, max_message_width - title_accum))
-- Render function for notifications. See notify-render()
render = "default",
local namespace = base.namespace()
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, { "", "" })
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, 0, {
virt_text = {
{ left_icon, highlights.icon },
{ left_title .. left_buffer, highlights.title },
},
virt_text_win_col = 0,
priority = 10,
})
vim.api.nvim_buf_set_extmark(bufnr, namespace, 0, 0, {
virt_text = { { right_title, highlights.title } },
virt_text_pos = "right_align",
priority = 10,
})
vim.api.nvim_buf_set_lines(bufnr, 1, -1, false, notif.message)
-- Default timeout for notifications
timeout = 5000,
vim.api.nvim_buf_set_extmark(bufnr, namespace, 1, 0, {
hl_group = highlights.body,
end_line = #notif.message,
end_col = #notif.message[#notif.message],
priority = 50, -- Allow treesitter to override
})
end,
-- For stages that change opacity this is treated as the highlight behind the window
-- Set this to either a highlight group, an RGB hex value e.g. "#000000" or a function returning an RGB code for dynamic values
background_colour = "#000000",
-- Minimum width for notification windows
minimum_width = 50,
-- Icons for the different levels
icons = {
ERROR = "",
WARN = "",
INFO = "",
DEBUG = "",
TRACE = "",
},
},
timeout = 5000,
-- For stages that change opacity this is treated as the highlight behind the window
-- Set this to either a highlight group, an RGB hex value e.g. "#000000" or a function returning an RGB code for dynamic values
background_colour = "#000000",
-- Minimum width for notification windows
minimum_width = 50,
-- Icons for the different levels
icons = {
ERROR = "",
WARN = "",
INFO = "",
DEBUG = "",
TRACE = "",
},
}
require("notify").setup(opts)
end,
},
},
},