1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-04-13 13:39:09 +08:00

perf(job): use table for job stdout/stderr data

This commit is contained in:
wsdjeg 2023-07-06 00:01:14 +08:00
parent cfc0d2d6d4
commit b5d6e1cb5f
2 changed files with 10 additions and 6 deletions

View File

@ -35,7 +35,9 @@ function M.start(cmd, opts)
-- :call jobstart(split(&shell) + split(&shellcmdflag) + ['{cmd}'])
command = shell[1]
argv = vim.list_slice(shell, 2)
for _, v in ipairs(shellcmdflag) do table.insert(argv, v) end
for _, v in ipairs(shellcmdflag) do
table.insert(argv, v)
end
table.insert(argv, cmd)
elseif type(cmd) == 'table' then
command = cmd[1]
@ -66,7 +68,8 @@ function M.start(cmd, opts)
if opts.on_stdout then
uv.read_start(stdout, function(err, data)
if data then
opts.on_stdout(current_id, data, 'stdout')
data = data:gsub('\r', '')
opts.on_stdout(current_id, vim.split(data, '\n'), 'stdout')
end
end)
end
@ -74,7 +77,8 @@ function M.start(cmd, opts)
if opts.on_stderr then
uv.read_start(stderr, function(err, data)
if data then
opts.on_stderr(current_id, data, 'stderr')
data = data:gsub('\r', '')
opts.on_stderr(current_id, vim.split(data, '\n'), 'stderr')
end
end)
end

View File

@ -1,14 +1,14 @@
local job = require('spacevim.api.job')
local jobid = job.start({ 'rg', 'wsdjeg', '.' }, {
local jobid = job.start('echo 1', {
on_stdout = function(id, data, event)
vim.print(id)
vim.print(data)
vim.print(vim.inspect(data))
vim.print(event)
end,
on_stderr = function(id, data, event)
vim.print(id)
vim.print(data)
vim.print(vim.inspect(data))
vim.print(event)
end,
on_exit = function(id, code, signal)