Skip to content

feat(actions): improve error message for wrong actions #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions lua/gitlinker/actions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
local spawn = require("gitlinker.commons.spawn")
local str = require("gitlinker.commons.str")
local tbl = require("gitlinker.commons.tbl")
local logging = require("gitlinker.commons.logging")

--- @alias gitlinker.Action fun(url:string):any

-- copy url to clipboard
Expand All @@ -10,14 +15,58 @@
-- see: https://github.com/axieax/urlview.nvim/blob/b183133fd25caa6dd98b415e0f62e51e061cd522/lua/urlview/actions.lua#L38
--- @param url string
local function system(url)
local errors = {}
local logger = logging.get("gitlinker")

local function _dummy() end
local function _error(line)
if str.not_empty(line) then
table.insert(errors, line)
end
end
local function _has_exitcode(result)
return type(result) == "table" and type(result.exitcode) == "number" and result.exitcode ~= 0
end
local function _exit(result)
if tbl.list_not_empty(errors) then
if _has_exitcode(result) then
logger:err(
string.format(
"failed to open url, error:%s, exitcode:%s",
vim.inspect(table.concat(errors, " ")),
vim.inspect(result.exitcode)
)
)
else
logger:err(
string.format("failed to open url, error:%s", vim.inspect(table.concat(errors, " ")))

Check warning on line 42 in lua/gitlinker/actions.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/actions.lua#L41-L42

Added lines #L41 - L42 were not covered by tests
)
end
elseif _has_exitcode(result) then
logger:err(string.format("failed to open url, exitcode:%s", vim.inspect(result.exitcode)))

Check warning on line 46 in lua/gitlinker/actions.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/actions.lua#L45-L46

Added lines #L45 - L46 were not covered by tests
end
end

if vim.fn.has("mac") > 0 then
vim.fn.jobstart({ "open", url })
spawn.detached({ "open", url }, {

Check warning on line 51 in lua/gitlinker/actions.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/actions.lua#L51

Added line #L51 was not covered by tests
on_stdout = _dummy,
on_stderr = _error,
}, _exit)

Check warning on line 54 in lua/gitlinker/actions.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/actions.lua#L54

Added line #L54 was not covered by tests
elseif vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0 then
vim.fn.jobstart({ "cmd", "/C", "start", url })
spawn.detached({ "cmd", "/C", "start", url }, {

Check warning on line 56 in lua/gitlinker/actions.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/actions.lua#L56

Added line #L56 was not covered by tests
on_stdout = _dummy,
on_stderr = _error,
}, _exit)

Check warning on line 59 in lua/gitlinker/actions.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/actions.lua#L59

Added line #L59 was not covered by tests
elseif vim.fn.executable("wslview") > 0 then
vim.fn.jobstart({ "wslview", url })
spawn.detached({ "wslview", url }, {

Check warning on line 61 in lua/gitlinker/actions.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/actions.lua#L61

Added line #L61 was not covered by tests
on_stdout = _dummy,
on_stderr = _error,
}, _exit)

Check warning on line 64 in lua/gitlinker/actions.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/actions.lua#L64

Added line #L64 was not covered by tests
else
vim.fn.jobstart({ "xdg-open", url })
spawn.detached({ "xdg-open", url }, {
on_stdout = _dummy,
on_stderr = _error,
}, _exit)
end
end

Expand Down
Loading