Imagine I have a script script.sh that I want to run (not in the background), and after I interact with it, it prints some results. This could be a live grepping session for example.
I can create a command in vifmrc to put those results into a menu, and assign it a key binding:
command! script script.sh %m
nnoremap <c-s> :script<cr>
This works fine.
However, I would like to run something like that using the Lua API, because it would be easier to manipulate those results after I get them.
Currently, I understand the API exposes vifm.startjob which would let me gather command output, but runs in the background. So unless I open another terminal to run the script, I cannot interact with it. And if I open the other terminal, I cannot get the results of the script (for some reason):
local function live_grep_back(info)
-- Command that works
--local cmd = "echo 'one\ntwo\n'"
-- My Script needs a terminal to be interactive
local cmd = "kitty script.sh"
local job = vifm.startjob {
cmd = cmd,
description = "My Script",
visible = true,
onexit = function(job)
local i = 1
local items = {}
while true do
local line = job:stdout():read()
if line == nil then break end
items[i] = line
i = i + 1
end
vifm.menus.loadcustom({ title = "My Script Results", items = items, withnavigation = false })
end
}
return true;
end
local added = vifm.cmds.add {
name = "Script",
description = "Run my script",
handler = script,
minargs = 0,
maxargs = 0,
}
if not added then
vifm.sb.error("Failed to register :Script")
end
return {}
If instead of vifm.startjob I use vifm.run, it seems I cannot get any results back. term() returns the contents of stdout, but vifm.run does not.
I wrote my own term() function for vifm and exposed it to Lua, and that seems to work. But before I try to submit a PR, I would like to know if I am missing something, and this can be done with the current toolset, without needing another function. Or maybe vifm.run could return the contents of stdout at exit.