doc [ci skip] #10097

- fix broken links (|buffered| was renamed to |channel-buffered|)
- rewrite `:h channel-lines`
This commit is contained in:
Justin M. Keyes 2019-06-01 20:14:29 +02:00 committed by GitHub
parent 95ece7d046
commit ed24a297e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 49 deletions

View File

@ -43,59 +43,66 @@ functions like |chansend()| consume channel ids.
============================================================================== ==============================================================================
2. Reading and writing raw bytes *channel-bytes* 2. Reading and writing raw bytes *channel-bytes*
By default, channels opened by vimscript functions will operate with raw Channels opened by Vimscript functions operate with raw bytes by default. For
bytes. Additionally, for a job channel using rpc, bytes can still be a job channel using RPC, bytes can still be read over its stderr. Similarily,
read over its stderr. Similarily, only bytes can be written to nvim's own stderr. only bytes can be written to Nvim's own stderr.
*channel-callback* *on_stdout* *on_stderr* *on_stdin* *on_data* *channel-callback*
Scripts can react to channel activity (received data) via callback functions on_stdout({chan-id}, {data}, {name}) *on_stdout*
assigned to the `on_stdout`, `on_stderr`, `on_stdin`, and `on_data` options. on_stderr({chan-id}, {data}, {name}) *on_stderr*
Callbacks should be fast, avoid potentially slow/expensive work. on_stdin({chan-id}, {data}, {name}) *on_stdin*
on_data({chan-id}, {data}, {name}) *on_data*
Scripts can react to channel activity (received data) via callback
functions assigned to the `on_stdout`, `on_stderr`, `on_stdin`, or
`on_data` option keys. Callbacks should be fast: avoid potentially
slow/expensive work.
Parameters: ~
{chan-id} Channel handle. |channel-id|
{data} Raw data (|readfile()|-style list of strings) read from
the channel. EOF is a single-item list: `['']`. First and
last items may be partial lines! |channel-lines|
{name} Stream name (string) like "stdout", so the same function
can handle multiple streams. Event names depend on how the
channel was opened and in what mode/protocol.
*channel-buffered* *channel-buffered*
By default the callback is invoked immediately as data is available; empty The callback is invoked immediately as data is available, where
data indicates EOF (stream closed). Alternatively, set the `stdout_buffered`, a single-item list `['']` indicates EOF (stream closed). Alternatively
`stderr_buffered`, `stdin_buffered`, or `data_buffered` options to invoke the set the `stdout_buffered`, `stderr_buffered`, `stdin_buffered`, or
callback only on EOF, after all output was gathered. `data_buffered` option keys to invoke the callback only after all output
was gathered and the stream was closed.
*E5210* *E5210*
If the stream is set as buffered without assigning a callback, the data is If a buffering mode is used without a callback, the data is saved in the
saved in the options dict with the stream name as key. This requires a new stream {name} key of the options dict. It is an error if the key exists.
options dict for each opened channel (|copy()|). If the stream name key
is already set, error E5210 is raised.
Channel callback functions accept these arguments:
0: |channel-id|
1: Raw data read from the channel, formatted as a |readfile()|-style
list. If EOF occured, a single empty string `['']` will be passed in.
Note that the items in this list do not directly correspond to actual
lines in the output. See |channel-lines|
2: Stream name as a string, like `"stdout"`. This is to allow multiple
stream handlers to be implemented by the same function. The available
events depend on how the channel was opened and in what mode/protocol.
*channel-lines* *channel-lines*
Note: Stream event handlers receive data as it becomes available from the OS,
stream event handlers may receive partial (incomplete) lines. For a given thus the first and last items in the {data} list may be partial lines.
invocation of on_stdout etc, `a:data` is not guaranteed to end Empty string completes the previous partial line. Examples (not including
with a newline. the final `['']` emitted at EOF):
- `abcdefg` may arrive as `['abc']`, `['defg']`. - `foobar` may arrive as `['fo'], ['obar']`
- `abc\nefg` may arrive as `['abc', '']`, `['efg']` or `['abc']`, - `foo\nbar` may arrive as
`['','efg']`, or even `['ab']`, `['c','efg']`. `['foo','bar']`
or `['foo',''], ['bar']`
If you only are interested in complete output when the process exits, or `['foo'], ['','bar']`
use buffered mode. Otherwise, an easy way to deal with this: or `['fo'], ['o','bar']`
initialize a list as `['']`, then append to it as follows: > There are two ways to deal with this:
let s:chunks = [''] 1. To wait for the entire output, use |channel-buffered| mode.
func! s:on_event(job_id, data, event) dict 2. To read line-by-line, use the following code: >
let s:chunks[-1] .= a:data[0] let s:lines = ['']
call extend(s:chunks, a:data[1:]) func! s:on_event(job_id, data, event) dict
endf let eof = (a:data == [''])
" Complete the previous line.
let s:lines[-1] .= a:data[0]
" Append (last item may be a partial line, until EOF).
call extend(s:lines, a:data[1:])
endf
< <
Additionally, if the callbacks are Dictionary functions, |self| can be used to If the callback functions are |Dictionary-function|s, |self| refers to the
refer to the options dictionary containing the callbacks. |Partial|s can also be options dictionary containing the callbacks. |Partial|s can also be used as
used as callbacks. callbacks.
Data can be sent to the channel using the |chansend()| function. Here is a Data can be sent to the channel using the |chansend()| function. Here is a
simple example, echoing some data through a cat-process: simple example, echoing some data through a cat-process:

View File

@ -5272,9 +5272,9 @@ jobstart({cmd}[, {opts}]) *jobstart()*
*jobstart-options* *jobstart-options*
{opts} is a dictionary with these keys: {opts} is a dictionary with these keys:
|on_stdout|: stdout event handler (function name or |Funcref|) |on_stdout|: stdout event handler (function name or |Funcref|)
stdout_buffered : read stdout in |buffered| mode. stdout_buffered : read stdout in |channel-buffered| mode.
|on_stderr|: stderr event handler (function name or |Funcref|) |on_stderr|: stderr event handler (function name or |Funcref|)
stderr_buffered : read stderr in |buffered| mode. stderr_buffered : read stderr in |channel-buffered| mode.
|on_exit| : exit event handler (function name or |Funcref|) |on_exit| : exit event handler (function name or |Funcref|)
cwd : Working directory of the job; defaults to cwd : Working directory of the job; defaults to
|current-directory|. |current-directory|.
@ -7681,7 +7681,7 @@ sockconnect({mode}, {address}, {opts}) *sockconnect()*
{opts} is a dictionary with these keys: {opts} is a dictionary with these keys:
|on_data| : callback invoked when data was read from socket |on_data| : callback invoked when data was read from socket
data_buffered : read data from socket in |buffered| mode. data_buffered : read socket data in |channel-buffered| mode.
rpc : If set, |msgpack-rpc| will be used to communicate rpc : If set, |msgpack-rpc| will be used to communicate
over the socket. over the socket.
Returns: Returns:
@ -7850,7 +7850,7 @@ stdioopen({opts}) *stdioopen()*
{opts} is a dictionary with these keys: {opts} is a dictionary with these keys:
|on_stdin| : callback invoked when stdin is written to. |on_stdin| : callback invoked when stdin is written to.
stdin_buffered : read stdin in |buffered| mode. stdin_buffered : read stdin in |channel-buffered| mode.
rpc : If set, |msgpack-rpc| will be used to communicate rpc : If set, |msgpack-rpc| will be used to communicate
over stdio over stdio
Returns: Returns: