refactor(tty): tty.request() #39489

Problem:
- Various `TermRequest` handlers which all do similar things.
- `tty.query` is specific to `XTGETTCAP DCS`, can't be reused for other kinds of terminal queries.

Solution:
Provide `tty.request()`.
This commit is contained in:
Justin M. Keyes
2026-04-30 07:26:40 -04:00
committed by GitHub
parent 071f9748c0
commit d9ed4c8566
11 changed files with 223 additions and 244 deletions

View File

@@ -22,23 +22,17 @@ function M.paste(reg)
local clipboard = reg == '+' and 'c' or 'p'
return function()
local contents = nil --- @type string?
local id = vim.api.nvim_create_autocmd('TermResponse', {
callback = function(ev)
local resp = ev.data.sequence ---@type string
local encoded = resp:match('\027%]52;%w?;([A-Za-z0-9+/=]*)')
if encoded then
contents = vim.base64.decode(encoded)
return true
end
end,
})
vim.api.nvim_ui_send(osc52(clipboard, '?'))
local ok, res
-- timeout=0: we manage the staged 1s/9s timeout via vim.wait below.
local id = vim.tty.request(osc52(clipboard, '?'), { timeout = 0 }, function(resp)
local encoded = resp:match('\027%]52;%w?;([A-Za-z0-9+/=]*)')
if encoded then
contents = vim.base64.decode(encoded)
return true
end
end)
-- Wait 1s first for terminals that respond quickly
ok, res = vim.wait(1000, function()
local ok, res = vim.wait(1000, function()
return contents ~= nil
end)
@@ -55,7 +49,7 @@ function M.paste(reg)
end
if not ok then
vim.api.nvim_del_autocmd(id)
pcall(vim.api.nvim_del_autocmd, id)
if res == -1 then
vim.notify(
'Timed out waiting for a clipboard response from the terminal',

View File

@@ -178,7 +178,7 @@ function M.supported(opts)
---@type string?
local msg
require('vim.tty').query_apc(
vim.tty.query_apc(
seq({ a = 'q', i = query_id, s = 1, v = 1 }),
{ timeout = timeout },
function(resp)