refactor(util): improved notify functions

This commit is contained in:
Folke Lemaitre 2023-01-10 08:42:20 +01:00
parent 1f17bb79b7
commit d3a963d51c
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 37 additions and 26 deletions

View File

@ -105,25 +105,24 @@ function M.disable()
_G.loadfile = M._loadfile
M.enabled = false
if M.debug and vim.tbl_count(M.topmods) > 1 then
M.log(M.topmods, vim.log.levels.WARN, { title = "topmods" })
M.log(M.topmods, { level = vim.log.levels.WARN, title = "topmods" })
end
if M.debug and false then
local stats = vim.deepcopy(M.stats)
stats.time = (stats.time or 0) / 1e6
stats.find.time = (stats.find.time or 0) / 1e6
stats.autoload.time = (stats.autoload.time or 0) / 1e6
M.log(stats, nil, { title = "stats" })
M.log(stats, { title = "stats" })
end
end
---@param msg string|table
---@param level? number
---@param opts? {lang:string, title:string}
function M.log(msg, level, opts)
---@param opts? LazyNotifyOpts
function M.log(msg, opts)
if M.debug then
msg = vim.deepcopy(msg)
vim.schedule(function()
require("lazy.core.util").debug(msg, level, opts)
require("lazy.core.util").debug(msg, opts)
end)
end
end
@ -208,7 +207,7 @@ function M.load(modkey, modpath)
entry.hash = hash
if M.debug then
M.log("`" .. modpath .. "`", vim.log.levels.WARN, { title = "Cache.load" })
M.log("`" .. modpath .. "`", { level = vim.log.levels.WARN, title = "Cache.load" })
end
chunk, err = M._loadfile(entry.modpath)

View File

@ -180,7 +180,7 @@ function Spec:report(level)
level = level or vim.log.levels.ERROR
for _, notif in ipairs(self.notifs) do
if notif.level >= level then
Util.notify(notif.msg, notif.level)
Util.notify(notif.msg, { level = notif.level })
end
end
end

View File

@ -219,14 +219,15 @@ function M.lsmod(modname, fn)
end)
end
---@alias LazyNotifyOpts {lang?:string, title?:string, level?:number}
---@param msg string|string[]
---@param opts? {lang:string, title:string}
function M.notify(msg, level, opts)
---@param opts? LazyNotifyOpts
function M.notify(msg, opts)
if vim.in_fast_event() then
vim.schedule(function()
M.notify(msg, level, opts)
return vim.schedule(function()
M.notify(msg, opts)
end)
return
end
opts = opts or {}
@ -239,7 +240,7 @@ function M.notify(msg, level, opts)
)
end
local lang = opts.lang or "markdown"
vim.notify(msg, level, {
vim.notify(msg, opts.level or vim.log.levels.INFO, {
on_open = function(win)
pcall(require, "nvim-treesitter")
vim.wo[win].conceallevel = 3
@ -251,38 +252,49 @@ function M.notify(msg, level, opts)
vim.bo[buf].syntax = lang
end
end,
title = "lazy.nvim" .. (opts.title and ": " .. opts.title or ""),
title = opts.title or "lazy.nvim",
})
end
---@param msg string|string[]
function M.error(msg)
M.notify(msg, vim.log.levels.ERROR)
---@param opts? LazyNotifyOpts
function M.error(msg, opts)
opts = opts or {}
opts.level = vim.log.levels.ERROR
M.notify(msg, opts)
end
---@param msg string|string[]
function M.info(msg)
M.notify(msg, vim.log.levels.INFO)
---@param opts? LazyNotifyOpts
function M.info(msg, opts)
opts = opts or {}
opts.level = vim.log.levels.INFO
M.notify(msg, opts)
end
---@param msg string|string[]
function M.warn(msg)
M.notify(msg, vim.log.levels.WARN)
---@param opts? LazyNotifyOpts
function M.warn(msg, opts)
opts = opts or {}
opts.level = vim.log.levels.WARN
M.notify(msg, opts)
end
---@param msg string|table
---@param level? number
---@param opts? {lang:string, title:string}
function M.debug(msg, level, opts)
---@param opts? LazyNotifyOpts
function M.debug(msg, opts)
if not require("lazy.core.config").options.debug then
return
end
opts = opts or {}
if opts.title then
opts.title = "lazy.nvim: " .. opts.title
end
if type(msg) == "string" then
M.notify(msg, level, opts)
M.notify(msg, opts)
else
opts.lang = "lua"
M.notify(vim.inspect(msg), level, opts)
M.notify(vim.inspect(msg), opts)
end
end