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 was looking into the possibility of fetching the output of stdout while the process is running, and I think I was able to come up with a relatively good solution.
I'd like to hear your opinion on this and possibly add this feature.
Proposal
It is as simple as changing the condition inside streams.lua as follows.
-- not read_err-- -- n is specified, wait until buffer has n length but no need to wait for complete.set()-- -- n is not specified, then wait until complete.set()whilenotread_errandnotcomplete.is_set() and (notnor#buffer<n) do
Example Usage
Here is an example code that uses this feature and as you can see in # outputs, the elapsed ms value when using stdout.read(100) changes incrementally opposed to when no n is provided stdout.read().
Please go to the bottom to see explanation of each function.
localnio=require("nio")
localfunctionmake_proc()
localproc=nio.process.run({
cmd="ping",
args= { "google.com" },
})
assert(proc, "proc is nil")
returnprocendlocalfunctiontest_no_read_n()
localproc=make_proc()
localstart=vim.loop.hrtime()
nio.run(function()
nio.sleep(5*1000) -- wait 5 sec and kill pingproc.signal(15)
end)
localout=proc.stdout.read()
localelapsed= (vim.loop.hrtime() -start) /1000/1000vim.print(string.format([[[%8.2f ms] '%s']], elapsed, out))
endlocalfunctiontest_hundred_bytes()
localproc=make_proc()
localstart=vim.loop.hrtime()
fori=0, 9dolocalout=proc.stdout.read(100)
localelapsed= (vim.loop.hrtime() -start) /1000/1000vim.print(string.format([[[%8.2f ms, i: %s] '%s']], elapsed, i, out))
endproc.signal(15)
endlocalfunctiontest_print_each_newline()
localproc=make_proc()
localstart=vim.loop.hrtime()
localbuffer=""localline_count=0localiter_count=0whiletruedolocalout=proc.stdout.read(100)
ifnotoutthenbreakenditer_count=iter_count+1buffer=buffer..outwhiletruedolocalcr=string.find(buffer, "\n")
ifnotcrthenbreakendline_count=line_count+1localelapsed= (vim.loop.hrtime() -start) /1000/1000localmsg=[[[%8.2f ms, line: %s, iter: %s] '%s']]vim.print(string.format(msg, elapsed, line_count, iter_count, buffer:sub(1, cr-1)))
buffer=buffer:sub(cr+1)
endifline_count>=10thenbreakendendproc.signal(15)
endnio.run(function()
vim.print("====== test_no_read_n ======")
-- `stdout.read()` (with no n) will wait until the process is finished.-- So, we don't see any outputs until ping is done (5 sec).test_no_read_n()
vim.print("====== test_hundred_bytes ======")
-- `stdout.read(100)` will fetch 100 bytes from the buffer.-- This will output stdout **as soon as** there are more than 100 bytes from the last call.test_hundred_bytes()
vim.print("====== test_print_each_newline ======")
-- Same as above but the function has mechanism to cut the buffer at "\n" using string.find.-- This is meant to be implemented on the user side, but maybe it'd be nice if-- nio could add this functionality as it is relatively simple. Perhaps...-- `stdout.read(n: integer?, until: string?)` where-- -- `stdout.read()`: wait and flush all-- -- `stdout.read(100)`: flush 100 bytes at a time-- -- `stdout.read(100, "\n")`: flush every 100 bytes but only return at every "\n"-- -- `stdout.read(nil, "\n")`: ERROR, user must provide `n` as the best guess of the line length (which will vary between applications) for performance reasons.test_print_each_newline()
end)
Thanks for the plugin as always @rcarriga !
I was looking into the possibility of fetching the output of stdout while the process is running, and I think I was able to come up with a relatively good solution.
I'd like to hear your opinion on this and possibly add this feature.
Proposal
It is as simple as changing the condition inside
streams.lua
as follows.Example Usage
Here is an example code that uses this feature and as you can see in
# outputs
, the elapsed ms value when usingstdout.read(100)
changes incrementally opposed to when non
is providedstdout.read()
.Please go to the bottom to see explanation of each function.
Output
The text was updated successfully, but these errors were encountered: