Skip to content

Commit

Permalink
feat(util): better notify functions
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 71032c9 commit c68c915
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,46 @@ function M.try(fn, opts)
end
end

function M.warn(msg)
vim.notify(msg, vim.log.levels.WARN, { title = "Trouble" })
---@alias NotifyOpts {level?: number, title?: string, once?: boolean}

---@param msg string|string[]
---@param opts? NotifyOpts
function M.notify(msg, opts)
opts = opts or {}
msg = type(msg) == "table" and table.concat(msg, "\n") or msg
---@cast msg string
msg = vim.trim(msg)
return vim[opts.once and "notify_once" or "notify"](msg, opts.level, {
title = opts.title or "Trouble",
on_open = function(win)
vim.wo.conceallevel = 3
vim.wo.concealcursor = "n"
vim.wo.spell = false
vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown")
end,
})
end

---@param msg string|string[]
---@param opts? NotifyOpts
function M.warn(msg, opts)
M.notify(msg, vim.tbl_extend("keep", { level = vim.log.levels.WARN }, opts or {}))
end

function M.error(msg)
vim.notify(msg, vim.log.levels.ERROR, { title = "Trouble" })
---@param msg string|string[]
---@param opts? NotifyOpts
function M.error(msg, opts)
M.notify(msg, vim.tbl_extend("keep", { level = vim.log.levels.ERROR }, opts or {}))
end

---@param msg string
function M.debug(msg, ...)
if Config.debug then
if select("#", ...) > 0 then
local obj = select("#", ...) == 1 and ... or { ... }
msg = msg .. "\n" .. vim.inspect(obj)
end
vim.notify(msg, vim.log.levels.INFO, { title = "Trouble (debug)" })
M.notify(msg, { title = "Trouble (debug)" })
end
end

Expand Down

0 comments on commit c68c915

Please sign in to comment.