-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adding lua function to use result of RunBackgroundShell #3692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Adding lua function to use result of RunBackgroundShell #3692
Conversation
I do not know what use cases does local micro = import("micro")
local shell = import("micro/shell")
local job
local function onexit(output, data)
local state = job.ProcessState
-- ProcessState is set if process (sh if using JobStart) exits
-- onExit is called even if an error occurs with something like I/O within
-- Go, but it is probably rare
--if state == nil then return end
micro.TermMessage(state:Success() and output or state:String())
end
function init()
job = shell.JobStart("kill $$", nil, nil, onexit)
end I think it is better to instead change the function so that it returns an error when occuring on process startup, and passes the error that |
The main thing I wanted was to able to do something like Yeah it's true that I can do But even then you still need to keep track of the command execution by storing it in global (probably in a map or some sort?) If this PR gets merged, what you need to do instead it's just local function onexit(output, err)
-- Handle err here...
micro.TermMessage(output)
end
function init()
local retFunc, err = shell.RunBackgroundShell("curl whatever")
-- Handle err...
shell.LaunchBackgroundShellFunc(retFunc, onexit)
end where you can handle the error directly in the callback as param, not to mention it has a simpler interface than In my mind, the Job* functions make sense for long-running tasks like LSP but overkill and painful to manage for a command that takes a few seconds to complete. |
Yes, that is right. Tables in local t = { i = 1 }
-- in onexit, userargs[1] is { i = 1 } with job not set
t.job = shell.JobStart("sleep 1", nil, nil, onexit, t)
t.i = 2 There are workarounds, so I don't think changing the function to pass Lua values without conversion needs to be prioritized. Arrays converted using gopher-luar can be iterated by writing
I agree that the interface is simpler, especially since no objects have to be created in plugins to check errors. However, the interface seems a bit odd since the names are similar even though A function that's like a simplified version of
I have no experience with adding APIs so I'll leave this to maintainers or others to decide and discuss. |
I think you misunderstood what I meant. I meant you need to keep track of the
Yes, I totally agree that
Yeah I would like to do that but I feel like that will be confusing as well since that will be doing what Honestly though, I am happy to modify But I think at the minimum, we should at least merge the fix for |
Sorry, I meant to mention another approach on keeping track of the job object, that currently doesn't work even though it would be better than using something like a map in some cases.
I think it's better to submit this change in another pull request even if it's a lot smaller. I have done something similar in #3672 but I honestly have other pull requests where I haven't done such yet. |
Currently,
RunBackgroundShell()
returns a function and an error. I don't think there's a way to run that function asynchronously from lua plugin.Adding a helper lua function for that.
Although there's an additional error to the return of the returned function from
RunBackgroundShell()
, this has no impact the lua plugins that are currently using it since it first return var is the same. I have tested this with a lua plugin.