From d4ab66551bfa3f4207bca6b6ba4c7a60427fdde8 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Sat, 14 May 2022 12:02:05 -0700 Subject: [PATCH] feat: libuv stdout|stderr stream redirect options (#414) --- lua/fzf-lua/core.lua | 3 +++ lua/fzf-lua/libuv.lua | 33 ++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index 8fb59f3..7306d72 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -346,6 +346,9 @@ M.mt_cmd_wrapper = function(opts) "argv_expr", "cmd", "cwd", + "stdout", + "stderr", + "stderr_to_stdout", "git_dir", "git_worktree", "git_icons", diff --git a/lua/fzf-lua/libuv.lua b/lua/fzf-lua/libuv.lua index 6ca4251..54fef96 100644 --- a/lua/fzf-lua/libuv.lua +++ b/lua/fzf-lua/libuv.lua @@ -295,21 +295,26 @@ M.spawn_stdio = function(opts, fn_transform, fn_preprocess) return fn_loaded end + -- stdin/stdout are already buffered, not stderr this means + -- that every character is flushed immedietely which casued + -- rendering issues on Mac (#316, #287) and Linux (#414) + -- switch 'stderr' stream to 'line' buffering + -- https://www.lua.org/manual/5.2/manual.html#pdf-file%3asetvbuf + io.stderr:setvbuf 'line' + + -- redirect 'stderr' to 'stdout' on Macs by default + -- only takes effect if 'opts.stderr' was not set + if opts.stderr_to_stdout == nil and + vim.loop.os_uname().sysname == 'Darwin' then + opts.stderr_to_stdout = true + end + fn_transform = load_fn(fn_transform) fn_preprocess = load_fn(fn_preprocess) - -- if opts.argv_expr and opts.debug then - -- io.stdout:write("[DEBUG]: "..opts.cmd.."\n") - -- end - -- run the preprocessing fn if fn_preprocess then fn_preprocess(opts) end - -- for i=0,8 do - -- io.stdout:write(("%d %s\n"):format(i, vim.v.argv[i])) - -- end - - local is_darwin = vim.loop.os_uname().sysname == 'Darwin' if opts.debug then io.stdout:write("[DEBUG]: "..opts.cmd.."\n") @@ -360,10 +365,10 @@ M.spawn_stdio = function(opts, fn_transform, fn_preprocess) end) end - if opts.stderr then + if type(opts.stderr) == 'string' then stderr = pipe_open(opts.stderr) end - if opts.stdout then + if type(opts.stdout) == 'string' then stdout = pipe_open(opts.stdout) end @@ -396,10 +401,8 @@ M.spawn_stdio = function(opts, fn_transform, fn_preprocess) function(data) if stderr then pipe_write(stderr, data) - else - if is_darwin then - -- for some reason io:stderr causes - -- weird rendering issues on Mac (#316, #287) + elseif opts.stderr ~= false then + if opts.stderr_to_stdout then io.stdout:write(data) else io.stderr:write(data)