refactor!: add few wrappers around vim.system
This commit is contained in:
parent
837897a79d
commit
294c07c6aa
1 changed files with 28 additions and 25 deletions
|
|
@ -1,33 +1,36 @@
|
||||||
local Job = require "plenary.job"
|
|
||||||
local runner = {}
|
local runner = {}
|
||||||
|
|
||||||
---@class gopher.RunnerOpts
|
---@class gopher.RunnerOpts
|
||||||
---@field args? string[]
|
---@field cwd? string
|
||||||
---@field cwd? string?
|
---@field timeout? number
|
||||||
---@field on_exit? fun(data:string, status:number)
|
---@field stdin? string|string[]
|
||||||
|
|
||||||
---@param cmd string
|
---@param cmd (string|number)[]
|
||||||
---@param opts gopher.RunnerOpts
|
---@param opts? gopher.RunnerOpts
|
||||||
---@return string[]|nil
|
---@return vim.SystemCompleted
|
||||||
function runner.sync(cmd, opts)
|
function runner.sync(cmd, opts)
|
||||||
local output
|
opts = opts or {}
|
||||||
Job:new({
|
|
||||||
command = cmd,
|
return vim
|
||||||
args = opts.args,
|
.system(cmd, {
|
||||||
cwd = opts.cwd,
|
cwd = opts.cwd or nil,
|
||||||
on_stderr = function(_, data)
|
timeout = opts.timeout or 2000, -- TODO: move out to config
|
||||||
vim.print(data)
|
stdin = opts.stdin or nil,
|
||||||
end,
|
text = true,
|
||||||
on_exit = function(data, status)
|
})
|
||||||
output = data:result()
|
:wait()
|
||||||
vim.schedule(function()
|
|
||||||
if opts.on_exit then
|
|
||||||
opts.on_exit(output, status)
|
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
end,
|
---@param cmd (string|number)[]
|
||||||
}):sync(60000 --[[1 min]])
|
---@param on_exit fun(out:vim.SystemCompleted)
|
||||||
return output
|
---@param opts gopher.RunnerOpts
|
||||||
|
---@return vim.SystemObj
|
||||||
|
function runner.async(cmd, on_exit, opts)
|
||||||
|
return vim.system(cmd, {
|
||||||
|
cwd = opts.cwd or nil,
|
||||||
|
timeout = opts.timeout or 2000,
|
||||||
|
text = true,
|
||||||
|
}, on_exit)
|
||||||
end
|
end
|
||||||
|
|
||||||
return runner
|
return runner
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue