2019-06-17 15:00:51 -07:00
|
|
|
-- To test tui/input.c, this module spawns `nvim` inside :terminal and sends
|
|
|
|
-- bytes via jobsend(). Note: the functional/helpers.lua test-session methods
|
|
|
|
-- operate on the _host_ session, _not_ the child session.
|
2016-04-23 16:53:11 -07:00
|
|
|
local helpers = require('test.functional.helpers')(nil)
|
2015-03-25 05:14:47 -07:00
|
|
|
local Screen = require('test.functional.ui.screen')
|
2022-06-22 05:51:52 -07:00
|
|
|
local testprg = helpers.testprg
|
2022-11-19 05:46:04 -07:00
|
|
|
local exec_lua = helpers.exec_lua
|
2023-07-22 21:46:56 -07:00
|
|
|
local nvim = helpers.nvim
|
2023-12-05 15:26:46 -07:00
|
|
|
local nvim_prog = helpers.nvim_prog
|
2015-03-25 05:14:47 -07:00
|
|
|
|
|
|
|
local function feed_data(data)
|
2022-11-19 05:46:04 -07:00
|
|
|
if type(data) == 'table' then
|
|
|
|
data = table.concat(data, '\n')
|
|
|
|
end
|
|
|
|
exec_lua('vim.api.nvim_chan_send(vim.b.terminal_job_id, ...)', data)
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function feed_termcode(data)
|
2022-11-19 05:46:04 -07:00
|
|
|
feed_data('\027' .. data)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function make_lua_executor(session)
|
|
|
|
return function(code, ...)
|
|
|
|
local status, rv = session:request('nvim_exec_lua', code, {...})
|
|
|
|
if not status then
|
|
|
|
session:stop()
|
|
|
|
error(rv[2])
|
|
|
|
end
|
|
|
|
return rv
|
|
|
|
end
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2022-11-19 05:46:04 -07:00
|
|
|
|
2015-03-25 05:14:47 -07:00
|
|
|
-- some helpers for controlling the terminal. the codes were taken from
|
|
|
|
-- infocmp xterm-256color which is less what libvterm understands
|
|
|
|
-- civis/cnorm
|
|
|
|
local function hide_cursor() feed_termcode('[?25l') end
|
|
|
|
local function show_cursor() feed_termcode('[?25h') end
|
|
|
|
-- smcup/rmcup
|
|
|
|
local function enter_altscreen() feed_termcode('[?1049h') end
|
|
|
|
local function exit_altscreen() feed_termcode('[?1049l') end
|
|
|
|
-- character attributes
|
|
|
|
local function set_fg(num) feed_termcode('[38;5;'..num..'m') end
|
|
|
|
local function set_bg(num) feed_termcode('[48;5;'..num..'m') end
|
|
|
|
local function set_bold() feed_termcode('[1m') end
|
|
|
|
local function set_italic() feed_termcode('[3m') end
|
|
|
|
local function set_underline() feed_termcode('[4m') end
|
2022-09-24 22:54:30 -07:00
|
|
|
local function set_underdouble() feed_termcode('[4:2m') end
|
|
|
|
local function set_undercurl() feed_termcode('[4:3m') end
|
2019-09-13 14:46:19 -07:00
|
|
|
local function set_strikethrough() feed_termcode('[9m') end
|
2015-03-25 05:14:47 -07:00
|
|
|
local function clear_attrs() feed_termcode('[0;10m') end
|
|
|
|
-- mouse
|
|
|
|
local function enable_mouse() feed_termcode('[?1002h') end
|
|
|
|
local function disable_mouse() feed_termcode('[?1002l') end
|
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
local default_command = '["'..testprg('tty-test')..'"]'
|
2015-03-25 05:14:47 -07:00
|
|
|
|
2019-01-25 10:44:29 -07:00
|
|
|
local function screen_setup(extra_rows, command, cols, opts)
|
2017-02-22 08:10:10 -07:00
|
|
|
extra_rows = extra_rows and extra_rows or 0
|
|
|
|
command = command and command or default_command
|
|
|
|
cols = cols and cols or 50
|
|
|
|
|
2015-04-08 04:34:27 -07:00
|
|
|
nvim('command', 'highlight TermCursor cterm=reverse')
|
|
|
|
nvim('command', 'highlight TermCursorNC ctermbg=11')
|
2017-02-22 08:10:10 -07:00
|
|
|
|
|
|
|
local screen = Screen.new(cols, 7 + extra_rows)
|
2015-03-25 05:14:47 -07:00
|
|
|
screen:set_default_attr_ids({
|
|
|
|
[1] = {reverse = true}, -- focused cursor
|
|
|
|
[2] = {background = 11}, -- unfocused cursor
|
2016-08-09 08:01:56 -07:00
|
|
|
[3] = {bold = true},
|
|
|
|
[4] = {foreground = 12},
|
|
|
|
[5] = {bold = true, reverse = true},
|
2019-10-11 10:27:15 -07:00
|
|
|
-- 6 was a duplicate item
|
2016-08-09 08:01:56 -07:00
|
|
|
[7] = {foreground = 130},
|
|
|
|
[8] = {foreground = 15, background = 1}, -- error message
|
|
|
|
[9] = {foreground = 4},
|
2018-11-20 02:52:49 -07:00
|
|
|
[10] = {foreground = 121}, -- "Press ENTER" in embedded :terminal session.
|
2019-09-01 02:25:00 -07:00
|
|
|
[11] = {foreground = tonumber('0x00000b')},
|
2023-01-08 21:20:50 -07:00
|
|
|
[12] = {underline = true},
|
|
|
|
[13] = {underline = true, reverse = true},
|
|
|
|
[14] = {underline = true, reverse = true, bold = true},
|
|
|
|
[15] = {underline = true, foreground = 12},
|
2015-03-25 05:14:47 -07:00
|
|
|
})
|
|
|
|
|
2019-01-25 10:44:29 -07:00
|
|
|
screen:attach(opts or {rgb=false})
|
2017-02-22 11:14:06 -07:00
|
|
|
|
2023-07-22 21:46:56 -07:00
|
|
|
nvim('command', 'enew | call termopen('..command..')')
|
2017-02-22 11:14:06 -07:00
|
|
|
nvim('input', '<CR>')
|
|
|
|
local vim_errmsg = nvim('eval', 'v:errmsg')
|
|
|
|
if vim_errmsg and "" ~= vim_errmsg then
|
|
|
|
error(vim_errmsg)
|
|
|
|
end
|
|
|
|
|
2023-07-22 21:46:56 -07:00
|
|
|
nvim('command', 'setlocal scrollback=10')
|
|
|
|
nvim('command', 'startinsert')
|
|
|
|
nvim('input', '<Ignore>') -- Add input to separate two RPC requests
|
2017-02-22 11:14:06 -07:00
|
|
|
|
|
|
|
-- tty-test puts the terminal into raw mode and echoes input. Tests work by
|
|
|
|
-- feeding termcodes to control the display and asserting by screen:expect.
|
2019-01-25 10:44:29 -07:00
|
|
|
if command == default_command and opts == nil then
|
2017-02-22 08:10:10 -07:00
|
|
|
-- Wait for "tty ready" to be printed before each test or the terminal may
|
|
|
|
-- still be in canonical mode (will echo characters for example).
|
2018-12-18 04:50:44 -07:00
|
|
|
local empty_line = (' '):rep(cols)
|
2015-10-01 11:03:40 -07:00
|
|
|
local expected = {
|
2018-12-18 04:50:44 -07:00
|
|
|
'tty ready'..(' '):rep(cols - 9),
|
|
|
|
'{1: }' ..(' '):rep(cols - 1),
|
2015-10-01 11:03:40 -07:00
|
|
|
empty_line,
|
|
|
|
empty_line,
|
|
|
|
empty_line,
|
|
|
|
empty_line,
|
|
|
|
}
|
2017-02-22 08:10:10 -07:00
|
|
|
for _ = 1, extra_rows do
|
2015-10-01 11:03:40 -07:00
|
|
|
table.insert(expected, empty_line)
|
|
|
|
end
|
2015-03-25 05:14:47 -07:00
|
|
|
|
2018-12-18 04:50:44 -07:00
|
|
|
table.insert(expected, '{3:-- TERMINAL --}' .. ((' '):rep(cols - 14)))
|
|
|
|
screen:expect(table.concat(expected, '|\n')..'|')
|
2015-10-01 11:03:40 -07:00
|
|
|
else
|
2022-03-02 07:30:35 -07:00
|
|
|
-- This eval also acts as a poke_eventloop().
|
2017-02-22 11:14:06 -07:00
|
|
|
if 0 == nvim('eval', "exists('b:terminal_job_id')") then
|
|
|
|
error("terminal job failed to start")
|
|
|
|
end
|
2015-10-01 11:03:40 -07:00
|
|
|
end
|
2015-03-25 05:14:47 -07:00
|
|
|
return screen
|
|
|
|
end
|
|
|
|
|
2023-12-05 15:26:46 -07:00
|
|
|
local function setup_child_nvim(args, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
|
|
|
local argv = { nvim_prog, unpack(args) }
|
|
|
|
local cmd = string.format('[%s]', vim.iter(argv):map(function(s)
|
|
|
|
return string.format('\'%s\'', s)
|
|
|
|
end):join(', '))
|
|
|
|
|
|
|
|
if opts.env then
|
|
|
|
local s = {}
|
|
|
|
for k, v in pairs(opts.env) do
|
|
|
|
table.insert(s, string.format('%s: \'%s\'', k, v))
|
|
|
|
end
|
|
|
|
|
|
|
|
cmd = string.format('%s, #{env: #{%s}}', cmd, table.concat(s, ', '))
|
|
|
|
end
|
|
|
|
|
|
|
|
return screen_setup(0, cmd, opts.cols)
|
|
|
|
end
|
|
|
|
|
2015-03-25 05:14:47 -07:00
|
|
|
return {
|
|
|
|
feed_data = feed_data,
|
|
|
|
feed_termcode = feed_termcode,
|
2022-11-19 05:46:04 -07:00
|
|
|
make_lua_executor = make_lua_executor,
|
2015-03-25 05:14:47 -07:00
|
|
|
hide_cursor = hide_cursor,
|
|
|
|
show_cursor = show_cursor,
|
|
|
|
enter_altscreen = enter_altscreen,
|
|
|
|
exit_altscreen = exit_altscreen,
|
|
|
|
set_fg = set_fg,
|
|
|
|
set_bg = set_bg,
|
|
|
|
set_bold = set_bold,
|
|
|
|
set_italic = set_italic,
|
|
|
|
set_underline = set_underline,
|
2022-09-24 22:54:30 -07:00
|
|
|
set_underdouble = set_underdouble,
|
|
|
|
set_undercurl = set_undercurl,
|
2019-09-13 14:46:19 -07:00
|
|
|
set_strikethrough = set_strikethrough,
|
2015-03-25 05:14:47 -07:00
|
|
|
clear_attrs = clear_attrs,
|
|
|
|
enable_mouse = enable_mouse,
|
|
|
|
disable_mouse = disable_mouse,
|
2023-12-05 15:26:46 -07:00
|
|
|
screen_setup = screen_setup,
|
|
|
|
setup_child_nvim = setup_child_nvim,
|
2015-03-25 05:14:47 -07:00
|
|
|
}
|