Merge pull request #15910 from glacambre/silent_stdioopen

feat(--headless): do not print anything when stdioopen() has been used
This commit is contained in:
bfredl 2022-01-24 22:33:55 +01:00 committed by GitHub
commit 1b6ae2dbb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 1 deletions

View File

@ -96,6 +96,8 @@ struct Channel {
EXTERN PMap(uint64_t) channels INIT(= MAP_INIT);
EXTERN Callback on_print INIT(= CALLBACK_INIT);
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "channel.h.generated.h"
#endif

View File

@ -10215,6 +10215,10 @@ static void f_stdioopen(typval_T *argvars, typval_T *rettv, FunPtr fptr)
if (!tv_dict_get_callback(opts, S_LEN("on_stdin"), &on_stdin.cb)) {
return;
}
if (!tv_dict_get_callback(opts, S_LEN("on_print"), &on_print)) {
return;
}
on_stdin.buffered = tv_dict_get_number(opts, "stdin_buffered");
if (on_stdin.buffered && on_stdin.cb.type == kCallbackNone) {
on_stdin.self = opts;

View File

@ -81,7 +81,8 @@ typedef struct {
} data;
CallbackType type;
} Callback;
#define CALLBACK_NONE ((Callback){ .type = kCallbackNone })
#define CALLBACK_INIT { .type = kCallbackNone }
#define CALLBACK_NONE ((Callback)CALLBACK_INIT)
/// Structure holding dictionary watcher
typedef struct dict_watcher {

View File

@ -2647,6 +2647,17 @@ static void msg_puts_printf(const char *str, const ptrdiff_t maxlen)
char buf[7];
char *p;
if (on_print.type != kCallbackNone) {
typval_T argv[1];
argv[0].v_type = VAR_STRING;
argv[0].v_lock = VAR_UNLOCKED;
argv[0].vval.v_string = (char_u *)str;
typval_T rettv = TV_INITIAL_VALUE;
callback_call(&on_print, 1, argv, &rettv);
tv_clear(&rettv);
return;
}
while ((maxlen < 0 || s - str < maxlen) && *s != NUL) {
int len = utf_ptr2len((const char_u *)s);
if (!(silent_mode && p_verbose == 0)) {

View File

@ -100,6 +100,38 @@ describe('channels', function()
eq({"notification", "exit", {3,0}}, next_msg())
end)
it('can use stdio channel and on_print callback', function()
source([[
let g:job_opts = {
\ 'on_stdout': function('OnEvent'),
\ 'on_stderr': function('OnEvent'),
\ 'on_exit': function('OnEvent'),
\ }
]])
meths.set_var("nvim_prog", nvim_prog)
meths.set_var("code", [[
function! OnStdin(id, data, event) dict
echo string([a:id, a:data, a:event])
if a:data == ['']
quit
endif
endfunction
function! OnPrint(text) dict
call chansend(g:x, ['OnPrint:' .. a:text])
endfunction
let g:x = stdioopen({'on_stdin': funcref('OnStdin'), 'on_print':'OnPrint'})
call chansend(x, "hello")
]])
command("let g:id = jobstart([ g:nvim_prog, '-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--headless', '--cmd', g:code], g:job_opts)")
local id = eval("g:id")
ok(id > 0)
eq({ "notification", "stdout", {id, { "hello" } } }, next_msg())
command("call chansend(id, 'howdy')")
eq({"notification", "stdout", {id, {"OnPrint:[1, ['howdy'], 'stdin']"}}}, next_msg())
end)
local function expect_twoline(id, stream, line1, line2, nobr)
local msg = next_msg()
local joined = nobr and {line1..line2} or {line1, line2}