Skip to content

Commit 3afa6a0

Browse files
feat: use vim.notify for user notifications (#507)
1 parent 1ec7b56 commit 3afa6a0

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

lua/diffview/utils.lua

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,65 +23,44 @@ function M.now()
2323
return vim.loop.hrtime() / 1000000
2424
end
2525

26-
---Echo string with multiple lines.
2726
---@param msg string|string[]
28-
---@param hl? string Highlight group name.
2927
---@param schedule? boolean Schedule the echo call.
30-
function M.echo_multiln(msg, hl, schedule)
28+
---@param level integer
29+
function M.notify(msg, level, schedule)
3130
if schedule then
32-
vim.schedule(function() M.echo_multiln(msg, hl, false) end)
31+
vim.schedule(function() M.notify(msg, level, false) end)
32+
return
33+
end
34+
if type(msg) == "table" then
35+
msg = table.concat(msg, "\n")
36+
end
37+
if msg == "" then
3338
return
3439
end
3540

36-
if type(msg) ~= "table" then msg = { msg } end
41+
if level == vim.log.levels.ERROR then
42+
logger:error(msg)
43+
end
3744

38-
local text = table.concat(msg, "\n")
39-
api.nvim_echo({ { text, hl } }, true, {})
45+
vim.notify(msg, level, { title = "diffview.nvim" })
4046
end
4147

4248
---@param msg string|string[]
4349
---@param schedule? boolean Schedule the echo call.
4450
function M.info(msg, schedule)
45-
if type(msg) ~= "table" then
46-
msg = { msg }
47-
end
48-
if not msg[1] or (msg[1] == "" and #msg == 1) then
49-
return
50-
end
51-
msg = M.vec_slice(msg)
52-
msg[1] = "[Diffview.nvim] " .. msg[1]
53-
M.echo_multiln(msg, "DiagnosticInfo", schedule)
51+
M.notify(msg, vim.log.levels.INFO, schedule)
5452
end
5553

5654
---@param msg string|string[]
5755
---@param schedule? boolean Schedule the echo call.
5856
function M.warn(msg, schedule)
59-
if type(msg) ~= "table" then
60-
msg = { msg }
61-
end
62-
if not msg[1] or (msg[1] == "" and #msg == 1) then
63-
return
64-
end
65-
msg = M.vec_slice(msg)
66-
msg[1] = "[Diffview.nvim] " .. msg[1]
67-
M.echo_multiln(msg, "WarningMsg", schedule)
57+
M.notify(msg, vim.log.levels.WARN, schedule)
6858
end
6959

7060
---@param msg string|string[]
7161
---@param schedule? boolean Schedule the echo call.
7262
function M.err(msg, schedule)
73-
if type(msg) ~= "table" then
74-
msg = { msg }
75-
end
76-
if not msg[1] or (msg[1] == "" and #msg == 1) then
77-
return
78-
end
79-
msg = M.vec_slice(msg)
80-
81-
logger:error(table.concat(msg, "\n"))
82-
83-
msg[1] = "[Diffview.nvim] " .. msg[1]
84-
M.echo_multiln(msg, "ErrorMsg", schedule)
63+
M.notify(msg, vim.log.levels.ERROR, schedule)
8564
end
8665

8766
---Call the function `f`, ignoring most of the window and buffer related

0 commit comments

Comments
 (0)