The job
you get on call-back is a Job object. When used as a string, e.g:
echo "Job: " . a:job
you get the PID. You can however get a lot of information from the Job spec using various functions, for example job_info(a:job)
. For example:
job {
status : 'dead' # String
cmd [ # List
0 : 'ls' # String
]
termsig : '' # String
stoponexit : 'term' # String
tty_out : '/dev/pts/31' # String
exitval : 0 # Number
exit_cb : '<SNR>123_ValidateQuickfix' # String
tty_in : '/dev/pts/31' # String
channel { # Channel
status : 'buffered' # String
id : 2 # Number
in_io : 'pipe' # String
err_mode : 'RAW' # String
in_status : 'closed' # String
out_io : 'buffer' # String
err_io : 'buffer' # String
err_status : 'closed' # String
out_timeout : 2000 # Number
out_status : 'buffered' # String
out_mode : 'RAW' # String
in_mode : 'RAW' # String
in_timeout : 2000 # Number
err_timeout : 2000 # Number
}
process : 15695 # Number
}
In your case you can use ch_getbufnr(a:job, "out")
- this returns the buffer number for the process.
At least on my tests there is a caveat though. If out_io
is not set to "buffer"
there is no guarantee the buffer has been written to on call-back. With it set to "buffer"
the terminal window itself would be empty (as it writes to another buffer).
Testing with exit_cb
and something like this:
fun! s:SafeRead(bufnr)
let txt = getbufline(a:bufnr, 1, '$')
endfun
fun! s:ValidateQuickfix(job, exit_status)
let bufnr = ch_getbufnr(a:job, "out")
exe "autocmd SafeState * ++once call s:SafeRead(" . bufnr . ")"
endfun
the buffer is empty. Using close_cb
and the same routine the text is present, but not sure how reliable that is. Without the autocmd
routine the buffer is always empty on call-back. (You could perhaps do some flush routine).
Best approach would likely be to use some combination of "out_io": "buffer"
. That would of course render the terminal rather useless at is empty.
You can also use ch_readraw()
to read the contents from the buffer. :h channel-more
. This would also render terminal empty.
You could also read specific stream, e.g: ch_readraw(a:job, {"part":"err"})
:h job-options
, try to useout_cb
:help job-functions
for manipulating the job object:help :make
for running a program synchronously and putting the output in the quickfix list (via:help errorformat
and others)