From 9cc323a07c63161962900babd9c690e6993315df Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Thu, 13 Jul 2023 00:53:03 +0800 Subject: [PATCH] feat(job): improve job api --- docs/api/job.md | 33 +++++++++++++++++++++++ lua/spacevim/api/job.lua | 57 +++++++++++++++++++++++++++++----------- test/lua/test_job.lua | 6 ++--- 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/docs/api/job.md b/docs/api/job.md index 1ed5e9bd3..667c3ebfc 100644 --- a/docs/api/job.md +++ b/docs/api/job.md @@ -42,6 +42,39 @@ call s:JOB.start(cmd, \ ) ``` + +The lua job api: + +```lua +local job = require('spacevim.api.job') + +local jobid = job.start({'lua53', '-'}, { + -- the on_stdout and on_stderr function can be: + -- fun(id, data) end or fun(id, data, event) end + on_stdout = function(id, data) + vim.print(id) + vim.print(vim.inspect(data)) + end, + on_stderr = function(id, data) + vim.print(id) + vim.print(vim.inspect(data)) + end, + on_exit = function(id, code, signal) + vim.print(id) + vim.print('exit code', code) + vim.print('exit signal', signal) + end, +}) + + +job.send(jobid, 'print(1)\n') +job.send(jobid, 'print(1)\n') +job.send(jobid, 'print(1)\n') +job.send(jobid, 'print(1)\n') +job.chanclose(jobid, 'stdin') +job.stop(jobid) +``` + ## Functions | function name | description | diff --git a/lua/spacevim/api/job.lua b/lua/spacevim/api/job.lua index b4992626f..a33eefcc4 100644 --- a/lua/spacevim/api/job.lua +++ b/lua/spacevim/api/job.lua @@ -110,25 +110,50 @@ function M.start(cmd, opts) pid = pid, }) if opts.on_stdout then - uv.read_start(stdout, function(err, data) - if data then - data = data:gsub('\r', '') - vim.schedule(function() - opts.on_stdout(current_id, vim.split(data, '\n'), 'stdout') - end) - end - end) + -- define on_stdout function based on stdout's nparams + local nparams = debug.getinfo(opts.on_stdout).nparams + if nparams == 2 then + uv.read_start(stdout, function(err, data) + if data then + data = data:gsub('\r', '') + vim.schedule(function() + opts.on_stdout(current_id, vim.split(data, '\n')) + end) + end + end) + else + uv.read_start(stdout, function(err, data) + if data then + data = data:gsub('\r', '') + vim.schedule(function() + opts.on_stdout(current_id, vim.split(data, '\n'), 'stdout') + end) + end + end) + end end if opts.on_stderr then - uv.read_start(stderr, function(err, data) - if data then - data = data:gsub('\r', '') - vim.schedule(function() - opts.on_stderr(current_id, vim.split(data, '\n'), 'stderr') - end) - end - end) + local nparams = debug.getinfo(opts.on_stderr).nparams + if nparams == 2 then + uv.read_start(stderr, function(err, data) + if data then + data = data:gsub('\r', '') + vim.schedule(function() + opts.on_stderr(current_id, vim.split(data, '\n')) + end) + end + end) + else + uv.read_start(stderr, function(err, data) + if data then + data = data:gsub('\r', '') + vim.schedule(function() + opts.on_stderr(current_id, vim.split(data, '\n'), 'stderr') + end) + end + end) + end end return current_id end diff --git a/test/lua/test_job.lua b/test/lua/test_job.lua index b232f7905..2e7b8fe59 100644 --- a/test/lua/test_job.lua +++ b/test/lua/test_job.lua @@ -1,15 +1,13 @@ local job = require('spacevim.api.job') local jobid = job.start({'lua53', '-'}, { - on_stdout = function(id, data, event) + on_stdout = function(id, data) vim.print(id) vim.print(vim.inspect(data)) - vim.print(event) end, - on_stderr = function(id, data, event) + on_stderr = function(id, data) vim.print(id) vim.print(vim.inspect(data)) - vim.print(event) end, on_exit = function(id, code, signal) vim.print(id)