diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-04-17 11:59:50 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-04-18 16:11:59 -0300 |
commit | 9acb9607134a461fc342f29a098b83b1bad7134d (patch) | |
tree | 155980a8550ca2e4243e4612129a2cdd3fad499a /src/os/job.h | |
parent | 350144f5113e111fea0d5b33589d6d478280f298 (diff) | |
download | rneovim-9acb9607134a461fc342f29a098b83b1bad7134d.tar.gz rneovim-9acb9607134a461fc342f29a098b83b1bad7134d.tar.bz2 rneovim-9acb9607134a461fc342f29a098b83b1bad7134d.zip |
Refactor job control to use RStream events
Instead of a single 'job read' callback, job control consumers need to provide
callbacks for "stdout read", "stderr read" and "exit". For vimscript, the
JobActivity autocommand is still used to handle every job event, for example:
```vim
:let srv1_id = jobstart('netcat-server-1', 'nc', ['-l', '9991'])
:let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '9991'])
function JobEvent()
" v:job_data[0] = the job id
" v:job_data[1] = the event type, one of "stdout", "stderr" or "exit"
" v:job_data[2] = data read from stdout or stderr
if v:job_data[1] == 'stdout'
let str = 'Message from job '.v:job_data[0].': '.v:job_data[2]
elseif v:job_data[1] == 'stderr'
let str = 'Error message from job '.v:job_data[0].': '.v:job_data[2]
else
" Exit
let str = 'Job '.v:job_data[0].' exited'
endif
call append(line('$'), str)
endfunction
au JobActivity netcat-server-* call JobEvent()
```
And to see messages from 'job 1', run in another terminal:
```sh
bash -c "while true; do echo 123; sleep 1; done" | nc localhost 9991
```
Diffstat (limited to 'src/os/job.h')
-rw-r--r-- | src/os/job.h | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/os/job.h b/src/os/job.h index c0f6734467..37e34700dc 100644 --- a/src/os/job.h +++ b/src/os/job.h @@ -11,7 +11,7 @@ #include <stdbool.h> #include "os/event_defs.h" -#include "os/event.h" +#include "os/rstream_defs.h" /// Initializes job control resources void job_init(void); @@ -24,12 +24,19 @@ void job_teardown(void); /// @param argv Argument vector for the process. The first item is the /// executable to run. /// @param data Caller data that will be associated with the job -/// @param cb Callback that will be invoked everytime data is available in -/// the job's stdout/stderr +/// @param stdout_cb Callback that will be invoked when data is available +/// on stdout +/// @param stderr_cb Callback that will be invoked when data is available +/// on stderr +/// @param exit_cb Callback that will be invoked when the job exits /// @return The job id if the job started successfully. If the the first item / /// of `argv`(the program) could not be executed, -1 will be returned. // 0 will be returned if the job table is full. -int job_start(char **argv, void *data, job_read_cb cb); +int job_start(char **argv, + void *data, + rstream_cb stdout_cb, + rstream_cb stderr_cb, + job_exit_cb exit_cb); /// Terminates a job. This is a non-blocking operation, but if the job exists /// it's guaranteed to succeed(SIGKILL will eventually be sent) @@ -49,10 +56,22 @@ bool job_stop(int id); /// id is invalid(probably because it has already stopped) bool job_write(int id, char *data, uint32_t len); -/// Runs the read callback associated with the job/event +/// Runs the read callback associated with the job exit event /// /// @param event Object containing data necessary to invoke the callback -void job_handle(Event event); +void job_exit_event(Event event); + +/// Get the job id +/// +/// @param job A pointer to the job +/// @return The job id +int job_id(Job *job); + +/// Get data associated with a job +/// +/// @param job A pointer to the job +/// @return The job data +void *job_data(Job *job); #endif // NEOVIM_OS_JOB_H |