Skip to content

Commit

Permalink
refractor: wrap vim.notify
Browse files Browse the repository at this point in the history
  • Loading branch information
sontungexpt committed Aug 20, 2023
1 parent eb25c12 commit 7737c09
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
61 changes: 16 additions & 45 deletions lua/url-open/modules/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local api = vim.api
local fn = vim.fn

local patterns_module = require("url-open.modules.patterns")
local logger = require("url-open.modules.logger")

local M = {}

Expand All @@ -13,21 +14,19 @@ local M = {}
-- @tparam table msg : The message to print on success or error
M.call_cmd = function(command, msg)
local success, error_message = pcall(api.nvim_command, command)
vim.schedule(function()
if success then
if msg and msg.success then
vim.notify(msg.success, vim.log.levels.INFO, { title = "URL Handler" })
else
vim.notify("Success", vim.log.levels.INFO, { title = "URL Handler" })
end
if success then
if msg and msg.success then
logger.info(msg.success, { title = "URL Handler" })
else
if msg and msg.error then
vim.notify(msg.error .. ": " .. error_message, vim.log.levels.ERROR, { title = "URL Handler" })
else
vim.notify(error_message, vim.log.levels.ERROR, { title = "URL Handler" })
end
logger.info("Success", { title = "URL Handler" })
end
end)
else
if msg and msg.error then
logger.error(msg.error .. ": " .. error_message, { title = "URL Handler" })
else
logger.error("Error: " .. error_message, { title = "URL Handler" })
end
end
end

--- Find the first url in the text
Expand Down Expand Up @@ -102,53 +101,25 @@ M.open_url = function(user_opts)
elseif fn.executable("gnome-open") then
command = "silent! !gnome-open " .. shell_safe_url
else
vim.schedule(
function()
vim.notify(
"No known command to open url on Linux",
vim.log.levels.ERROR,
{ title = "URL Handler" }
)
end
)
logger.error("Unknown command to open url on Linux", { title = "URL Handler" })
return
end
elseif vim.loop.os_uname().sysname == "Darwin" then
if fn.executable("open") == 1 then
command = "silent! !open " .. shell_safe_url
else
vim.schedule(
function()
vim.notify(
"No known command to open url on MacOS",
vim.log.levels.ERROR,
{ title = "URL Handler" }
)
end
)
logger.error("Unknown command to open url on MacOS", { title = "URL Handler" })
return
end
elseif vim.loop.os_uname().sysname == "Windows" then
if fn.executable("start") == 1 then
command = "silent! !start " .. shell_safe_url
else
vim.schedule(
function()
vim.notify(
"No known command to open url on Windows",
vim.log.levels.ERROR,
{ title = "URL Handler" }
)
end
)
logger.error("Unknown command to open url on Windows", { title = "URL Handler" })
return
end
else
vim.schedule(
function()
vim.notify("Unknown operating system.", vim.log.levels.ERROR, { title = "URL Handler" })
end
)
logger.error("Unknown operating system.", { title = "URL Handler" })
return
end
M.call_cmd(command, {
Expand Down
32 changes: 32 additions & 0 deletions lua/url-open/modules/logger.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--- This module provides a simple wrapper around vim.notify to make it easier to
local M = {}

local levels = vim.log.levels
local notify = vim.notify
local schedule = vim.schedule

M.info = function(msg, opts)
schedule(function() notify(msg, levels.INFO, opts or { title = "Information" }) end)
end

M.warn = function(msg, opts)
schedule(function() notify(msg, levels.WARN, opts or { title = "Warning" }) end)
end

M.error = function(msg, opts)
schedule(function() notify(msg, levels.ERROR, opts or { title = "Error" }) end)
end

M.debug = function(msg, opts)
schedule(function() notify(msg, levels.DEBUG, opts or { title = "Debug" }) end)
end

M.trace = function(msg, opts)
schedule(function() notify(msg, levels.TRACE, opts or { title = "Trace" }) end)
end

M.off = function(msg, opts)
schedule(function() notify(msg, levels.OFF, opts or { title = "Off" }) end)
end

return M

0 comments on commit 7737c09

Please sign in to comment.