|
| 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 | + |
1 | 6 | --- @alias gitlinker.Action fun(url:string):any
|
2 | 7 |
|
3 | 8 | -- copy url to clipboard
|
|
10 | 15 | -- see: https://github.com/axieax/urlview.nvim/blob/b183133fd25caa6dd98b415e0f62e51e061cd522/lua/urlview/actions.lua#L38
|
11 | 16 | --- @param url string
|
12 | 17 | 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 | + |
13 | 50 | 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 }) |
15 | 56 | 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 }) |
17 | 62 | 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 }) |
19 | 68 | 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 }) |
21 | 74 | end
|
22 | 75 | end
|
23 | 76 |
|
|
0 commit comments