refactor(utils): back to plenary.job

This commit is contained in:
Smirnov Oleksandr 2023-08-08 18:35:15 +03:00
parent 000ae23aa3
commit 6c2f2749ca

View file

@ -1,54 +1,28 @@
local Job = require "plenary.job"
local runner = {} local runner = {}
local uv = vim.loop ---@class gopher.RunnerOpts
---@field args string[]
---@field cwd? string?
---@field on_exit? fun(data:string, status:number)
---@class ProcessOpts
---@field args? string[]
---@field cwd? string
---@field on_exit? fun(ok:boolean, output:string)
---@param opts? ProcessOpts
---@param cmd string ---@param cmd string
function runner.spawn(cmd, opts) ---@param opts gopher.RunnerOpts
opts = opts or {} ---@return string
function runner.sync(cmd, opts)
local stdout = assert(uv.new_pipe()) local output
local stderr = assert(uv.new_pipe()) Job:new({
command = cmd,
local output = ""
---@type uv_process_t?
local handle = nil
handle = uv.spawn(cmd, {
stdio = { nil, stdout, stderr },
args = opts.args, args = opts.args,
cwd = opts.cwd, cwd = opts.cwd,
}, function(status) on_exit = function(data, status)
if handle then
handle:close()
end
stderr:close()
stdout:close()
if opts.on_exit then
output = output:gsub("[^\r\n]+\r", "")
vim.schedule(function() vim.schedule(function()
opts.on_exit(status == 0, output) output = data:result()
opts.on_exit(output, status)
end) end)
end end,
end) }):sync()
return output
local function on_output(err, data)
assert(not err, err)
if data then
output = output .. data:gsub("\r\n", "\n")
end
end
uv.read_start(stdout, on_output)
uv.read_start(stderr, on_output)
return handle
end end
return runner return runner