Skip to content

Commit

Permalink
fix(notify): Improve code quality, new global function.
Browse files Browse the repository at this point in the history
Signed-off-by: Guennadi Maximov C <[email protected]>
  • Loading branch information
DrKJeff16 committed Sep 4, 2024
1 parent 6f149ea commit 3a45a4d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lua/user_api/util/notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ function M.notify(msg, lvl, opts)

local vim_lvl = vim.log.levels

-- WARN: DO NOT SORT
local DEFAULT_LVLS = {
'trace',
'debug',
'info',
'warn',
'error',
'off',
[1] = 'trace',
[2] = 'debug',
[3] = 'info',
[4] = 'warn',
[5] = 'error',
[6] = 'off',
}

---@type notify.Options
Expand All @@ -50,12 +49,10 @@ function M.notify(msg, lvl, opts)
if exists('notify') then
local notify = require('notify')

if lvl == nil then
if lvl == nil or (type(lvl) == 'number' and not (lvl >= 0 and lvl <= 5)) then
lvl = DEFAULT_LVLS[vim_lvl.INFO + 1]
elseif type(lvl) == 'number' and (lvl >= 0 and lvl <= 5) then
lvl = DEFAULT_LVLS[math.floor(lvl) + 1]
elseif type(lvl) == 'number' then
lvl = DEFAULT_LVLS[vim_lvl.INFO + 1]
end

opts = vim.tbl_deep_extend('keep', opts, DEFAULT_OPTS)
Expand Down Expand Up @@ -92,6 +89,15 @@ function _G.anotify(msg, lvl, opts)
require('plenary.async').run(func)
end

---@param msg string
---@param lvl? ('debug'|'error'|'info'|'off'|'trace'|'warn'|0|1|2|3|4|5)?
---@param opts? ({ level: number?, title: string?, once: boolean?, id: string? }|notify.Config)?
function _G.insp_anotify(msg, lvl, opts)
local func = function() M.notify((inspect or vim.inspect)(msg), lvl or 'info', opts or {}) end
---@diagnostic disable-next-line:missing-parameter
require('plenary.async').run(func)
end

return M

--- vim:ts=4:sts=4:sw=4:et:ai:si:sta:noci:nopi:

0 comments on commit 3a45a4d

Please sign in to comment.