Merge pull request #18357 from bfredl/ui_stdin

feat(ui): allow embedder to emulate "cat data | nvim -" behaviour
This commit is contained in:
bfredl
2022-05-02 23:24:37 +02:00
committed by GitHub
8 changed files with 106 additions and 17 deletions

View File

@@ -361,14 +361,15 @@ local function remove_args(args, args_rm)
return new_args
end
function module.spawn(argv, merge, env, keep)
--- @param io_extra used for stdin_fd, see :help ui-option
function module.spawn(argv, merge, env, keep, io_extra)
if session and not keep then
session:close()
end
local child_stream = ChildProcessStream.spawn(
merge and module.merge_args(prepend_argv, argv) or argv,
env)
env, io_extra)
return Session.new(child_stream)
end
@@ -415,8 +416,8 @@ end
-- clear('-e')
-- clear{args={'-e'}, args_rm={'-i'}, env={TERM=term}}
function module.clear(...)
local argv, env = module.new_argv(...)
module.set_session(module.spawn(argv, nil, env))
local argv, env, io_extra = module.new_argv(...)
module.set_session(module.spawn(argv, nil, env, nil, io_extra))
end
-- Builds an argument list for use in clear().
@@ -426,6 +427,7 @@ function module.new_argv(...)
local args = {unpack(module.nvim_argv)}
table.insert(args, '--headless')
local new_args
local io_extra
local env = nil
local opts = select(1, ...)
if type(opts) == 'table' then
@@ -461,13 +463,14 @@ function module.new_argv(...)
end
end
new_args = opts.args or {}
io_extra = opts.io_extra
else
new_args = {...}
end
for _, arg in ipairs(new_args) do
table.insert(args, arg)
end
return args, env
return args, env, io_extra
end
function module.insert(...)

View File

@@ -1,3 +1,5 @@
local uv = require'luv'
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
@@ -98,3 +100,49 @@ end
describe('--embed UI on startup (ext_linegrid=true)', function() test_embed(true) end)
describe('--embed UI on startup (ext_linegrid=false)', function() test_embed(false) end)
describe('--embed UI', function()
it('can pass stdin', function()
local pipe = assert(uv.pipe())
local writer = assert(uv.new_pipe(false))
writer:open(pipe.write)
clear {args_rm={'--headless'}, io_extra=pipe.read}
-- attach immediately after startup, for early UI
local screen = Screen.new(40, 8)
screen:attach {stdin_fd=3}
screen:set_default_attr_ids {
[1] = {bold = true, foreground = Screen.colors.Blue1};
[2] = {bold = true};
}
writer:write "hello nvim\nfrom external input\n"
writer:shutdown(function() writer:close() end)
screen:expect{grid=[[
^hello nvim |
from external input |
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
|
]]}
-- stdin (rpc input) still works
feed 'o'
screen:expect{grid=[[
hello nvim |
^ |
from external input |
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{2:-- INSERT --} |
]]}
end)
end)