You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using vim.loop.spawn (e.g., the luv library) in Neovim v0.9.2 to develop a plugin, which supports Windows.
I use the spawn API in this way:
localfunctionprintln(line)
io.write(line..'\n')
end--- @paramdata_bufferstring--- @paramfn_line_processorfun(line:string?):nillocalfunctionconsume(data_buffer, fn_line_processor)
locali=1whilei<=#data_bufferdolocalnewline_pos=shell_helpers.string_find(data_buffer, "\n", i)
ifnotnewline_posthenbreakendlocalline=data_buffer:sub(i, newline_pos)
fn_line_processor(line)
i=newline_pos+1endreturniendlocalcmds= {'fd', '.', '-cnever', '-tf', '-i', '-u'}
localout_pipe=vim.loop.new_pipe()
localerr_pipe=vim.loop.new_pipe()
localdata_buffer=nillocalfunctionon_exit(code)
out_pipe:close()
err_pipe:close()
vim.loop.stop()
endlocalprocess_handler, process_id=vim.loop.spawn(cmds[1], {
args= { unpack(cmds, 2) },
stdio= { nil, out_pipe, err_pipe },
}, function(code, signal)
out_pipe:read_stop()
err_pipe:read_stop()
out_pipe:shutdown()
err_pipe:shutdown()
on_exit(code)
end)
localfunctionon_output(err, data)
iferrthenon_exit(1)
returnendifnotdatathenifdata_bufferthen-- foreach the data_buffer and find every linelocali=consume(data_buffer, println)
ifi<=#data_bufferthenlocalline=data_buffer:sub(i, #data_buffer)
println(line)
data_buffer=nilendendon_exit(0)
returnend-- append data to data_bufferdata_buffer=data_bufferand (data_buffer..data) ordata-- foreach the data_buffer and find every linelocali=consume(data_buffer, println)
-- truncate the printed lines if found anydata_buffer=i<=#data_bufferanddata_buffer:sub(i, #data_buffer)
ornilendlocalfunctionon_error(err, data)
-- if err then-- on_exit(1)-- return-- end-- if not data then-- on_exit(0)-- return-- endendout_pipe:read_start(on_output)
err_pipe:read_start(on_error)
vim.loop.run() -- start uv loop here, it will block the main thread until the command ends
This is a sample code which is simple but mostly tells the basic structure of the logic.
But in this screen recording, you can see the print data is not consist.
You can see that, during two fd searchings, the lines count is different: first is 1550, second is 1388.
I add some log, and it looks like that when fd command exit, there maybe still some data not read to buffer, but I called the out_pipe:read_stop() function in on_exit function, which could stop reading.
So maybe I should just close the process handle in spawn's on_exit (because it's just being invoked when the process exit).
Invoke read_stop and close out_pipe when data == nil in read_start callback (e.g. on_output), this will ensure all data is read into buffer.
Let me try this solution when I am back to keyboard.
Hi,
I'm using
vim.loop.spawn
(e.g., the luv library) in Neovim v0.9.2 to develop a plugin, which supports Windows.I use the
spawn
API in this way:This is a sample code which is simple but mostly tells the basic structure of the logic.
But in this screen recording, you can see the print data is not consist.
You can see that, during two
fd
searchings, the lines count is different: first is 1550, second is 1388.2_5.C__WINDOWS_system32_cmd.exe.2023-10-03.07-10-25.mp4
I guess the reason is that the timing of
on_exit
callback inspawn
3rd parameter comes, there's still some data not read into buffer.But is there an arrival order?
The text was updated successfully, but these errors were encountered: