Skip to content

Commit 8e49101

Browse files
committed
feat(actions): improve error message for wrong actions
1 parent 296ad98 commit 8e49101

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

lua/gitlinker/actions.lua

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
local spawn = require("gitlinker.commons.spawn")
2+
local str = require("gitlinker.commons.str")
3+
local tbl = require("gitlinker.commons.tbl")
4+
local logging = require("gitlinker.commons.logging")
5+
16
--- @alias gitlinker.Action fun(url:string):any
27

38
-- copy url to clipboard
@@ -10,14 +15,62 @@ end
1015
-- see: https://github.com/axieax/urlview.nvim/blob/b183133fd25caa6dd98b415e0f62e51e061cd522/lua/urlview/actions.lua#L38
1116
--- @param url string
1217
local function system(url)
18+
local errors = {}
19+
local logger = logging.get("gitlinker")
20+
21+
local function _dummy() end
22+
local function _error(line)
23+
if str.not_empty(line) then
24+
table.insert(errors, line)
25+
end
26+
end
27+
local function _has_exitcode(result)
28+
return type(result) == "table" and type(result.exitcode) == "number" and result.exitcode ~= 0
29+
end
30+
local function _exit(result)
31+
if tbl.list_not_empty(errors) then
32+
if _has_exitcode(result) then
33+
logger:err(
34+
string.format(
35+
"failed to open url, error:%s, exitcode:%s",
36+
vim.inspect(table.concat(errors, " ")),
37+
vim.inspect(result.exitcode)
38+
)
39+
)
40+
else
41+
logger:err(
42+
string.format("failed to open url, error:%s", vim.inspect(table.concat(errors, " ")))
43+
)
44+
end
45+
elseif _has_exitcode(result) then
46+
logger:err(string.format("failed to open url, exitcode:%s", vim.inspect(result.exitcode)))
47+
end
48+
end
49+
1350
if vim.fn.has("mac") > 0 then
14-
vim.fn.jobstart({ "open", url })
51+
spawn.detached({ "open", url }, {
52+
on_stdout = _dummy,
53+
on_stderr = _error,
54+
}, _exit)
55+
-- vim.fn.jobstart({ "open", url }, { on_stderr = function() end })
1556
elseif vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0 then
16-
vim.fn.jobstart({ "cmd", "/C", "start", url })
57+
spawn.detached({ "cmd", "/C", "start", url }, {
58+
on_stdout = _dummy,
59+
on_stderr = _error,
60+
}, _exit)
61+
-- vim.fn.jobstart({ "cmd", "/C", "start", url })
1762
elseif vim.fn.executable("wslview") > 0 then
18-
vim.fn.jobstart({ "wslview", url })
63+
spawn.detached({ "wslview", url }, {
64+
on_stdout = _dummy,
65+
on_stderr = _error,
66+
}, _exit)
67+
-- vim.fn.jobstart({ "wslview", url })
1968
else
20-
vim.fn.jobstart({ "xdg-open", url })
69+
spawn.detached({ "xdg-open", url }, {
70+
on_stdout = _dummy,
71+
on_stderr = _error,
72+
}, _exit)
73+
-- vim.fn.jobstart({ "xdg-open", url })
2174
end
2275
end
2376

0 commit comments

Comments
 (0)