mirror of
https://github.com/neovim/neovim.git
synced 2024-12-23 12:45:17 -07:00
Compare commits
9 Commits
174f1b528e
...
19b77ee003
Author | SHA1 | Date | |
---|---|---|---|
|
19b77ee003 | ||
|
3db3947b0e | ||
|
ce818a9a91 | ||
|
a56a5f0117 | ||
|
021b8f6e5d | ||
|
2ad8324092 | ||
|
c50d7747a5 | ||
|
ce26f7c332 | ||
|
25820b5e24 |
@ -31,6 +31,7 @@ for k, v in pairs({
|
||||
loader = true,
|
||||
func = true,
|
||||
F = true,
|
||||
img = true,
|
||||
lsp = true,
|
||||
hl = true,
|
||||
diagnostic = true,
|
||||
|
78
runtime/lua/vim/img.lua
Normal file
78
runtime/lua/vim/img.lua
Normal file
@ -0,0 +1,78 @@
|
||||
local img = vim._defer_require('vim.img', {
|
||||
_backend = ..., --- @module 'vim.img._backend'
|
||||
_detect = ..., --- @module 'vim.img._detect'
|
||||
_image = ..., --- @module 'vim.img._image'
|
||||
_terminal = ..., --- @module 'vim.img._terminal'
|
||||
})
|
||||
|
||||
---Loads an image into memory, returning a wrapper around the image.
|
||||
---
|
||||
---Accepts `data` as base64-encoded bytes, or a `filename` that will be loaded.
|
||||
---@param opts {data?:string, filename?:string}
|
||||
---@return vim.img.Image
|
||||
function img.load(opts)
|
||||
return img._image:new(opts)
|
||||
end
|
||||
|
||||
img.protocol = (function()
|
||||
---@class vim.img.Protocol 'iterm2'|'kitty'|'sixel'
|
||||
|
||||
---@type vim.img.Protocol|nil
|
||||
local protocol = nil
|
||||
|
||||
local loaded = false
|
||||
|
||||
---Determines the preferred graphics protocol to use by default.
|
||||
---
|
||||
---@return vim.img.Protocol|nil
|
||||
return function()
|
||||
if not loaded then
|
||||
local graphics = img._detect().graphics
|
||||
|
||||
---@diagnostic disable-next-line:cast-type-mismatch
|
||||
---@cast graphics vim.img.Protocol|nil
|
||||
protocol = graphics
|
||||
|
||||
loaded = true
|
||||
end
|
||||
|
||||
return protocol
|
||||
end
|
||||
end)()
|
||||
|
||||
---@class vim.img.Opts: vim.img.Backend.RenderOpts
|
||||
---@field backend? vim.img.Protocol|vim.img.Backend
|
||||
|
||||
---Displays the image within the terminal used by neovim.
|
||||
---@param image vim.img.Image
|
||||
---@param opts? vim.img.Opts
|
||||
function img.show(image, opts)
|
||||
opts = opts or {}
|
||||
|
||||
local backend = opts.backend
|
||||
|
||||
-- If no graphics are explicitly defined, attempt to detect the
|
||||
-- preferred graphics. If we still cannot figure out a backend,
|
||||
-- throw an error early versus silently trying a protocol.
|
||||
if not backend then
|
||||
backend = img.protocol()
|
||||
assert(backend, 'no graphics backend available')
|
||||
end
|
||||
|
||||
-- For named protocols, grab the appropriate backend, failing
|
||||
-- if there is not a default backend for the specified protocol.
|
||||
if type(backend) == 'string' then
|
||||
local protocol = backend
|
||||
backend = img._backend[protocol]
|
||||
assert(backend, 'unsupported backend: ' .. protocol)
|
||||
end
|
||||
|
||||
---@cast backend vim.img.Backend
|
||||
backend.render(image, {
|
||||
pos = opts.pos,
|
||||
size = opts.size,
|
||||
crop = opts.crop,
|
||||
})
|
||||
end
|
||||
|
||||
return img
|
12
runtime/lua/vim/img/_backend.lua
Normal file
12
runtime/lua/vim/img/_backend.lua
Normal file
@ -0,0 +1,12 @@
|
||||
---@class vim.img.Backend
|
||||
---@field render fun(image:vim.img.Image, opts?:vim.img.Backend.RenderOpts)
|
||||
|
||||
---@class vim.img.Backend.RenderOpts
|
||||
---@field crop? {x:integer, y:integer, width:integer, height:integer} units are pixels
|
||||
---@field pos? {row:integer, col:integer} units are cells
|
||||
---@field size? {width:integer, height:integer} units are cells
|
||||
|
||||
return vim._defer_require('vim.img._backend', {
|
||||
iterm2 = ..., --- @module 'vim.img._backend.iterm2'
|
||||
kitty = ..., --- @module 'vim.img._backend.kitty'
|
||||
})
|
100
runtime/lua/vim/img/_backend/iterm2.lua
Normal file
100
runtime/lua/vim/img/_backend/iterm2.lua
Normal file
@ -0,0 +1,100 @@
|
||||
---@class vim.img.Iterm2Backend: vim.img.Backend
|
||||
local M = {}
|
||||
|
||||
---@param data string
|
||||
local function write_seq(data)
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
terminal.write(terminal.code.ESC) -- Begin sequence
|
||||
terminal.write(']1337;')
|
||||
|
||||
terminal.write(data) -- Write primary message
|
||||
|
||||
terminal.write(terminal.code.BEL) -- End sequence
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param args table<string, string>
|
||||
local function write_multipart_image(image, args)
|
||||
-- Begin the transfer of the image file
|
||||
write_seq('MultipartFile=' .. table.concat(args, ';'))
|
||||
|
||||
-- Begin sending parts as chunks
|
||||
image:for_each_chunk(function(chunk)
|
||||
write_seq('FilePart=' .. chunk)
|
||||
end)
|
||||
|
||||
-- Conclude the image display
|
||||
write_seq('FileEnd')
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param args table<string, string>
|
||||
local function write_image(image, args)
|
||||
local data = image.data
|
||||
if not data then
|
||||
return
|
||||
end
|
||||
|
||||
write_seq('File=' .. table.concat(args, ';') .. ':' .. data)
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param opts? vim.img.Backend.RenderOpts
|
||||
function M.render(image, opts)
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
if not image:is_loaded() then
|
||||
return
|
||||
end
|
||||
|
||||
opts = opts or {}
|
||||
if opts.pos then
|
||||
terminal.cursor.move(opts.pos.col, opts.pos.row, true)
|
||||
end
|
||||
|
||||
local args = {
|
||||
-- NOTE: We MUST mark as inline otherwise not rendered and put in a downloads folder
|
||||
'inline=1',
|
||||
|
||||
-- This will show a progress indicator for a multipart image
|
||||
'size=' .. tostring(image:size()),
|
||||
}
|
||||
|
||||
-- Specify the name of the image, which iterm2 requires to be base64 encoded
|
||||
if image.name then
|
||||
table.insert(args, 'name=' .. vim.base64.encode(image.name))
|
||||
end
|
||||
|
||||
-- If a size is provided (in cells), we add it as arguments
|
||||
if opts.size then
|
||||
table.insert(args, 'width=' .. tostring(opts.size.width))
|
||||
table.insert(args, 'height=' .. tostring(opts.size.height))
|
||||
|
||||
-- We need to disable aspect ratio preservation, otherwise
|
||||
-- the desired width/height won't be respected
|
||||
table.insert(args, 'preserveAspectRatio=0')
|
||||
end
|
||||
|
||||
-- Only iTerm2 3.5+ supports multipart images
|
||||
--
|
||||
-- WezTerm and others are assumed to NOT support multipart images
|
||||
--
|
||||
-- iTerm2 should have set TERM_PROGRAM and TERM_PROGRAM_VERSION,
|
||||
-- otherwise we assume a different terminal!
|
||||
---@type string|nil
|
||||
local prog = vim.env.TERM_PROGRAM
|
||||
---@type vim.Version|nil
|
||||
local version = vim.version.parse(vim.env.TERM_PROGRAM_VERSION or '')
|
||||
if prog == 'iTerm.app' and version and vim.version.ge(version, { 3, 5, 0 }) then
|
||||
write_multipart_image(image, args)
|
||||
else
|
||||
write_image(image, args)
|
||||
end
|
||||
|
||||
if opts.pos then
|
||||
terminal.cursor.restore()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
108
runtime/lua/vim/img/_backend/kitty.lua
Normal file
108
runtime/lua/vim/img/_backend/kitty.lua
Normal file
@ -0,0 +1,108 @@
|
||||
---@class vim.img.KittyBackend: vim.img.Backend
|
||||
local M = {}
|
||||
|
||||
---For kitty, we need to write an image in chunks
|
||||
---
|
||||
---Graphics codes are in this form:
|
||||
---
|
||||
--- <ESC>_G<control data>;<payload><ESC>\
|
||||
---
|
||||
---To stream data for a PNG, we specify the format `f=100`.
|
||||
---
|
||||
---To simultaneously transmit and display an image, we use `a=T`.
|
||||
---
|
||||
---Chunking data (such as from over a network) requires the
|
||||
---specification of `m=0|1`, where all chunks must have a
|
||||
---value of `1` except the very last chunk.
|
||||
---@param data string
|
||||
local function write_seq(data)
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
terminal.write(terminal.code.ESC .. '_G') -- Begin sequence
|
||||
terminal.write(data) -- Primary data
|
||||
terminal.write(terminal.code.ESC .. '\\') -- End sequence
|
||||
end
|
||||
|
||||
---Builds a header table of key value pairs.
|
||||
---@param opts vim.img.Backend.RenderOpts
|
||||
---@return table<string, string>
|
||||
local function make_header(opts)
|
||||
---@type table<string, string>
|
||||
local header = {}
|
||||
|
||||
header['a'] = 'T'
|
||||
header['f'] = '100'
|
||||
|
||||
local crop = opts.crop
|
||||
local size = opts.size
|
||||
|
||||
if crop then
|
||||
header['x'] = tostring(crop.x)
|
||||
header['y'] = tostring(crop.y)
|
||||
header['w'] = tostring(crop.width)
|
||||
header['h'] = tostring(crop.height)
|
||||
end
|
||||
|
||||
if size then
|
||||
header['c'] = tostring(size.width)
|
||||
header['r'] = tostring(size.height)
|
||||
end
|
||||
|
||||
return header
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param opts vim.img.Backend.RenderOpts
|
||||
local function write_multipart_image(image, opts)
|
||||
image:for_each_chunk(function(chunk, pos, has_more)
|
||||
local data = {}
|
||||
|
||||
-- If at the beginning of our image, mark as a PNG to be
|
||||
-- transmitted and displayed immediately
|
||||
if pos == 1 then
|
||||
-- Add an entry in our data to write out to the terminal
|
||||
-- that is "k=v," for the key-value entries from the header
|
||||
for key, value in pairs(make_header(opts)) do
|
||||
table.insert(data, key .. '=' .. value .. ',')
|
||||
end
|
||||
end
|
||||
|
||||
-- If we are still sending chunks and not at the end
|
||||
if has_more then
|
||||
table.insert(data, 'm=1')
|
||||
else
|
||||
table.insert(data, 'm=0')
|
||||
end
|
||||
|
||||
-- If we have a chunk available, write it
|
||||
if string.len(chunk) > 0 then
|
||||
table.insert(data, ';')
|
||||
table.insert(data, chunk)
|
||||
end
|
||||
|
||||
write_seq(table.concat(data))
|
||||
end)
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param opts? vim.img.Backend.RenderOpts
|
||||
function M.render(image, opts)
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
if not image:is_loaded() then
|
||||
return
|
||||
end
|
||||
|
||||
opts = opts or {}
|
||||
if opts.pos then
|
||||
terminal.cursor.move(opts.pos.col, opts.pos.row, true)
|
||||
end
|
||||
|
||||
write_multipart_image(image, opts)
|
||||
|
||||
if opts.pos then
|
||||
terminal.cursor.restore()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
88
runtime/lua/vim/img/_detect.lua
Normal file
88
runtime/lua/vim/img/_detect.lua
Normal file
@ -0,0 +1,88 @@
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
local TERM_QUERY = {
|
||||
-- Request device attributes (DA2).
|
||||
--
|
||||
-- It typically returns information about the terminal type and supported features.
|
||||
--
|
||||
-- Response format is typically something like '\033[>...;...;...c'
|
||||
DEVICE_ATTRIBUTES = terminal.code.ESC .. '[>q',
|
||||
|
||||
-- Request device status report (DSR), checking if terminal is okay.
|
||||
--
|
||||
-- Response indicates its current state.
|
||||
DEVICE_STATUS_REPORT = terminal.code.ESC .. '[5n',
|
||||
}
|
||||
|
||||
local TERM_RESPONSE = {
|
||||
-- Indicates that the terminal is functioning normally (no error).
|
||||
--
|
||||
-- 0 means 'OK'; other values indicate different states or errors.
|
||||
OK = terminal.code.ESC .. '[0n',
|
||||
}
|
||||
|
||||
---Detects supported graphics of the terminal.
|
||||
---@return {graphics:'iterm2'|'kitty'|'sixel'|nil, tmux:boolean, broken_sixel_cursor_placement:boolean}
|
||||
return function()
|
||||
local results = { graphics = nil, tmux = false, broken_sixel_cursor_placement = false }
|
||||
|
||||
local term = os.getenv('TERM')
|
||||
if term == 'xterm-kitty' or term == 'xterm-ghostty' or term == 'ghostty' then
|
||||
results.graphics = 'kitty'
|
||||
end
|
||||
|
||||
local term_program = os.getenv('TERM_PROGRAM')
|
||||
if term_program == 'vscode' then
|
||||
results.graphics = 'iterm2'
|
||||
results.broken_sixel_cursor_placement = true
|
||||
end
|
||||
|
||||
local _, err = terminal.query({
|
||||
query = table.concat({
|
||||
TERM_QUERY.DEVICE_ATTRIBUTES,
|
||||
TERM_QUERY.DEVICE_STATUS_REPORT,
|
||||
}),
|
||||
handler = function(buffer)
|
||||
local function has(s)
|
||||
return string.find(buffer, s, 1, true) ~= nil
|
||||
end
|
||||
|
||||
if has('iTerm2') or has('Konsole 2') then
|
||||
results.graphics = 'iterm2'
|
||||
end
|
||||
|
||||
if has('WezTerm') then
|
||||
results.graphics = 'iterm2'
|
||||
results.broken_sixel_cursor_placement = true
|
||||
end
|
||||
|
||||
if has('kitty') or has('ghostty') then
|
||||
results.graphics = 'kitty'
|
||||
end
|
||||
|
||||
if has('mlterm') then
|
||||
results.graphics = 'sixel'
|
||||
end
|
||||
|
||||
if has('XTerm') or has('foot') then
|
||||
results.graphics = 'sixel'
|
||||
results.broken_sixel_cursor_placement = true
|
||||
end
|
||||
|
||||
if has('tmux') then
|
||||
results.tmux = true
|
||||
end
|
||||
|
||||
-- Check if we have received the ok terminal response
|
||||
local start = string.find(buffer, TERM_RESPONSE.OK, 1, true)
|
||||
if start then
|
||||
return string.sub(buffer, start)
|
||||
end
|
||||
end,
|
||||
timeout = 250,
|
||||
})
|
||||
|
||||
assert(not err, err)
|
||||
|
||||
return results
|
||||
end
|
144
runtime/lua/vim/img/_image.lua
Normal file
144
runtime/lua/vim/img/_image.lua
Normal file
@ -0,0 +1,144 @@
|
||||
---@class vim.img.Image
|
||||
---@field name string|nil name of the image if loaded from disk
|
||||
---@field data string|nil base64 encoded data
|
||||
local M = {}
|
||||
M.__index = M
|
||||
|
||||
---Creates a new image instance.
|
||||
---@param opts? {data?:string, filename?:string}
|
||||
---@return vim.img.Image
|
||||
function M:new(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local instance = {}
|
||||
setmetatable(instance, M)
|
||||
|
||||
instance.data = opts.data
|
||||
if not instance.data and opts.filename then
|
||||
instance:load_from_file(opts.filename)
|
||||
end
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Returns true if the image is loaded into memory.
|
||||
---@return boolean
|
||||
function M:is_loaded()
|
||||
return self.data ~= nil
|
||||
end
|
||||
|
||||
---Returns the size of the base64 encoded image.
|
||||
---@return integer
|
||||
function M:size()
|
||||
return string.len(self.data or '')
|
||||
end
|
||||
|
||||
---Iterates over the chunks of the image, invoking `f` per chunk.
|
||||
---@param f fun(chunk:string, pos:integer, has_more:boolean)
|
||||
---@param opts? {size?:integer}
|
||||
function M:for_each_chunk(f, opts)
|
||||
opts = opts or {}
|
||||
|
||||
-- Chunk size, defaulting to 4k
|
||||
local chunk_size = opts.size or 4096
|
||||
|
||||
local data = self.data
|
||||
if not data then
|
||||
return
|
||||
end
|
||||
|
||||
local pos = 1
|
||||
local len = string.len(data)
|
||||
while pos <= len do
|
||||
-- Get our next chunk from [pos, pos + chunk_size)
|
||||
local end_pos = pos + chunk_size - 1
|
||||
local chunk = data:sub(pos, end_pos)
|
||||
|
||||
-- If we have a chunk available, invoke our callback
|
||||
if string.len(chunk) > 0 then
|
||||
local has_more = end_pos + 1 <= len
|
||||
pcall(f, chunk, pos, has_more)
|
||||
end
|
||||
|
||||
pos = end_pos + 1
|
||||
end
|
||||
end
|
||||
|
||||
---Displays the image within the terminal used by neovim.
|
||||
---@param opts? vim.img.Opts
|
||||
function M:show(opts)
|
||||
vim.img.show(self, opts)
|
||||
end
|
||||
|
||||
---Loads data for an image from a file, replacing any existing data.
|
||||
---If a callback provided, will load asynchronously; otherwise, is blocking.
|
||||
---@param filename string
|
||||
---@param cb fun(err:string|nil, image:vim.img.Image|nil)
|
||||
---@overload fun(filename:string):vim.img.Image
|
||||
function M:load_from_file(filename, cb)
|
||||
local name = vim.fn.fnamemodify(filename, ':t:r')
|
||||
|
||||
if not cb then
|
||||
local stat = vim.uv.fs_stat(filename)
|
||||
assert(stat, 'unable to stat ' .. filename)
|
||||
|
||||
local fd = vim.uv.fs_open(filename, 'r', 644) --[[ @type integer|nil ]]
|
||||
assert(fd, 'unable to open ' .. filename)
|
||||
|
||||
local data = vim.uv.fs_read(fd, stat.size, -1) --[[ @type string|nil ]]
|
||||
assert(data, 'unable to read ' .. filename)
|
||||
|
||||
self.name = name
|
||||
self.data = vim.base64.encode(data)
|
||||
return self
|
||||
end
|
||||
|
||||
---@param err string|nil
|
||||
---@return boolean
|
||||
local function report_err(err)
|
||||
if err then
|
||||
vim.schedule(function()
|
||||
cb(err)
|
||||
end)
|
||||
end
|
||||
|
||||
return err ~= nil
|
||||
end
|
||||
|
||||
vim.uv.fs_stat(filename, function(stat_err, stat)
|
||||
if report_err(stat_err) then
|
||||
return
|
||||
end
|
||||
if not stat then
|
||||
report_err('missing stat')
|
||||
return
|
||||
end
|
||||
|
||||
vim.uv.fs_open(filename, 'r', 644, function(open_err, fd)
|
||||
if report_err(open_err) then
|
||||
return
|
||||
end
|
||||
if not fd then
|
||||
report_err('missing fd')
|
||||
return
|
||||
end
|
||||
|
||||
vim.uv.fs_read(fd, stat.size, -1, function(read_err, data)
|
||||
if report_err(read_err) then
|
||||
return
|
||||
end
|
||||
|
||||
vim.uv.fs_close(fd, function() end)
|
||||
|
||||
self.name = name
|
||||
self.data = vim.base64.encode(data or '')
|
||||
|
||||
vim.schedule(function()
|
||||
cb(nil, self)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
140
runtime/lua/vim/img/_terminal.lua
Normal file
140
runtime/lua/vim/img/_terminal.lua
Normal file
@ -0,0 +1,140 @@
|
||||
---@class vim.img.terminal
|
||||
---@field private __tty_name string
|
||||
local M = {}
|
||||
|
||||
local TERM_CODE = {
|
||||
BEL = '\x07', -- aka ^G
|
||||
ESC = '\x1B', -- aka ^[ aka \033
|
||||
}
|
||||
|
||||
---Retrieve the tty name used by the editor.
|
||||
---
|
||||
---E.g. /dev/ttys008
|
||||
---@return string|nil
|
||||
local function get_tty_name()
|
||||
if vim.fn.has('win32') == 1 then
|
||||
-- On windows, we use \\.\CON for reading and writing
|
||||
return '\\\\.\\CON'
|
||||
else
|
||||
-- Linux/Mac: Use `tty` command, which reads the terminal name
|
||||
-- in the form of something like /dev/ttys008
|
||||
local handle = io.popen('tty 2>/dev/null')
|
||||
if not handle then
|
||||
return nil
|
||||
end
|
||||
local result = handle:read('*a')
|
||||
handle:close()
|
||||
result = vim.fn.trim(result)
|
||||
if result == '' then
|
||||
return nil
|
||||
end
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
---Returns the name of the tty associated with the terminal.
|
||||
---@return string
|
||||
function M.tty_name()
|
||||
if not M.__tty_name then
|
||||
M.__tty_name = assert(get_tty_name(), 'failed to read editor tty name')
|
||||
end
|
||||
|
||||
return M.__tty_name
|
||||
end
|
||||
|
||||
---Writes data to the editor tty.
|
||||
---@param ... string|number
|
||||
function M.write(...)
|
||||
local handle = assert(io.open(M.tty_name(), 'w'))
|
||||
handle:write(...)
|
||||
handle:close()
|
||||
end
|
||||
|
||||
---@class vim.img.terminal.cursor
|
||||
M.cursor = {}
|
||||
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param save? boolean
|
||||
function M.cursor.move(x, y, save)
|
||||
if save then
|
||||
M.cursor.save()
|
||||
end
|
||||
M.write(TERM_CODE.ESC .. '[' .. y .. ';' .. x .. 'H')
|
||||
vim.uv.sleep(1)
|
||||
end
|
||||
|
||||
function M.cursor.save()
|
||||
M.write(TERM_CODE.ESC .. '[s')
|
||||
end
|
||||
|
||||
function M.cursor.restore()
|
||||
M.write(TERM_CODE.ESC .. '[u')
|
||||
end
|
||||
|
||||
---Terminal escape codes.
|
||||
M.code = TERM_CODE
|
||||
|
||||
---@param opts {query:string, handler:(fun(buffer:string):string|nil), timeout?:integer}
|
||||
---@return string|nil result, string|nil err
|
||||
function M.query(opts)
|
||||
local uv = vim.uv
|
||||
|
||||
opts = opts or {}
|
||||
local query = opts.query
|
||||
local handler = opts.handler
|
||||
local timeout = opts.timeout or 250
|
||||
|
||||
local tty_fd, err
|
||||
local function cleanup()
|
||||
if tty_fd then
|
||||
uv.fs_close(tty_fd)
|
||||
tty_fd = nil
|
||||
end
|
||||
end
|
||||
|
||||
-- Identify the path to the editor's tty
|
||||
-- NOTE: This only works on Unix-like systems!
|
||||
local ok, tty_path = pcall(M.tty_name)
|
||||
if not ok then
|
||||
return nil, tty_path
|
||||
end
|
||||
|
||||
-- Open the tty so we can write our query
|
||||
tty_fd, err = uv.fs_open(tty_path, 'r+', 438)
|
||||
if not tty_fd then
|
||||
return nil, err
|
||||
end
|
||||
|
||||
-- Write query to terminal.
|
||||
local success, write_err = uv.fs_write(tty_fd, query, -1)
|
||||
if not success then
|
||||
cleanup()
|
||||
return nil, write_err
|
||||
end
|
||||
|
||||
-- Read response with timeout.
|
||||
local buffer = ''
|
||||
local start_time = uv.now()
|
||||
|
||||
while uv.now() - start_time < timeout do
|
||||
local data, read_err = uv.fs_read(tty_fd, 512, -1)
|
||||
if data then
|
||||
buffer = buffer .. data
|
||||
local result = handler(buffer)
|
||||
if result then
|
||||
cleanup()
|
||||
return result
|
||||
end
|
||||
elseif read_err ~= 'EAGAIN' then
|
||||
cleanup()
|
||||
return nil, read_err
|
||||
end
|
||||
uv.sleep(1)
|
||||
end
|
||||
|
||||
cleanup()
|
||||
return nil, 'Timeout'
|
||||
end
|
||||
|
||||
return M
|
@ -641,9 +641,6 @@ bool terminal_enter(void)
|
||||
curwin->w_p_so = 0;
|
||||
curwin->w_p_siso = 0;
|
||||
|
||||
// Save the existing cursor entry since it may be modified by the application
|
||||
cursorentry_T save_cursorentry = shape_table[SHAPE_IDX_TERM];
|
||||
|
||||
// Update the cursor shape table and flush changes to the UI
|
||||
s->term->pending.cursor = true;
|
||||
refresh_cursor(s->term);
|
||||
@ -674,8 +671,8 @@ bool terminal_enter(void)
|
||||
RedrawingDisabled = s->save_rd;
|
||||
apply_autocmds(EVENT_TERMLEAVE, NULL, NULL, false, curbuf);
|
||||
|
||||
shape_table[SHAPE_IDX_TERM] = save_cursorentry;
|
||||
ui_mode_info_set();
|
||||
// Restore the terminal cursor to what is set in 'guicursor'
|
||||
(void)parse_shape_opt(SHAPE_CURSOR);
|
||||
|
||||
if (save_curwin == curwin->handle) { // Else: window was closed.
|
||||
curwin->w_p_cul = save_w_p_cul;
|
||||
|
@ -15,9 +15,20 @@ local skip = t.skip
|
||||
describe(':terminal cursor', function()
|
||||
local screen
|
||||
|
||||
local terminal_mode_idx ---@type number
|
||||
|
||||
before_each(function()
|
||||
clear()
|
||||
screen = tt.setup_screen()
|
||||
|
||||
if terminal_mode_idx == nil then
|
||||
for i, v in ipairs(screen._mode_info) do
|
||||
if v.name == 'terminal' then
|
||||
terminal_mode_idx = i
|
||||
end
|
||||
end
|
||||
assert(terminal_mode_idx)
|
||||
end
|
||||
end)
|
||||
|
||||
it('moves the screen cursor when focused', function()
|
||||
@ -143,13 +154,6 @@ describe(':terminal cursor', function()
|
||||
|
||||
it('can be modified by application #3681', function()
|
||||
skip(is_os('win'), '#31587')
|
||||
local idx ---@type number
|
||||
for i, v in ipairs(screen._mode_info) do
|
||||
if v.name == 'terminal' then
|
||||
idx = i
|
||||
end
|
||||
end
|
||||
assert(idx)
|
||||
|
||||
local states = {
|
||||
[1] = { blink = true, shape = 'block' },
|
||||
@ -171,13 +175,13 @@ describe(':terminal cursor', function()
|
||||
]],
|
||||
condition = function()
|
||||
if v.blink then
|
||||
eq(500, screen._mode_info[idx].blinkon)
|
||||
eq(500, screen._mode_info[idx].blinkoff)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
|
||||
else
|
||||
eq(0, screen._mode_info[idx].blinkon)
|
||||
eq(0, screen._mode_info[idx].blinkoff)
|
||||
eq(0, screen._mode_info[terminal_mode_idx].blinkon)
|
||||
eq(0, screen._mode_info[terminal_mode_idx].blinkoff)
|
||||
end
|
||||
eq(v.shape, screen._mode_info[idx].cursor_shape)
|
||||
eq(v.shape, screen._mode_info[terminal_mode_idx].cursor_shape)
|
||||
end,
|
||||
})
|
||||
end
|
||||
@ -191,20 +195,13 @@ describe(':terminal cursor', function()
|
||||
]])
|
||||
|
||||
-- Cursor returns to default on TermLeave
|
||||
eq(500, screen._mode_info[idx].blinkon)
|
||||
eq(500, screen._mode_info[idx].blinkoff)
|
||||
eq('block', screen._mode_info[idx].cursor_shape)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
|
||||
eq('block', screen._mode_info[terminal_mode_idx].cursor_shape)
|
||||
end)
|
||||
|
||||
it('can be modified per terminal', function()
|
||||
skip(is_os('win'), '#31587')
|
||||
local idx ---@type number
|
||||
for i, v in ipairs(screen._mode_info) do
|
||||
if v.name == 'terminal' then
|
||||
idx = i
|
||||
end
|
||||
end
|
||||
assert(idx)
|
||||
|
||||
-- Set cursor to vertical bar with blink
|
||||
tt.feed_csi('5 q')
|
||||
@ -216,9 +213,9 @@ describe(':terminal cursor', function()
|
||||
{3:-- TERMINAL --} |
|
||||
]],
|
||||
condition = function()
|
||||
eq(500, screen._mode_info[idx].blinkon)
|
||||
eq(500, screen._mode_info[idx].blinkoff)
|
||||
eq('vertical', screen._mode_info[idx].cursor_shape)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
|
||||
eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
|
||||
end,
|
||||
})
|
||||
|
||||
@ -231,9 +228,9 @@ describe(':terminal cursor', function()
|
||||
{3:-- TERMINAL --} |
|
||||
]],
|
||||
condition = function()
|
||||
eq(500, screen._mode_info[idx].blinkon)
|
||||
eq(500, screen._mode_info[idx].blinkoff)
|
||||
eq('vertical', screen._mode_info[idx].cursor_shape)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
|
||||
eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
|
||||
end,
|
||||
})
|
||||
|
||||
@ -256,9 +253,9 @@ describe(':terminal cursor', function()
|
||||
]],
|
||||
condition = function()
|
||||
-- New terminal, cursor resets to defaults
|
||||
eq(500, screen._mode_info[idx].blinkon)
|
||||
eq(500, screen._mode_info[idx].blinkoff)
|
||||
eq('block', screen._mode_info[idx].cursor_shape)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
|
||||
eq('block', screen._mode_info[terminal_mode_idx].cursor_shape)
|
||||
end,
|
||||
})
|
||||
|
||||
@ -275,9 +272,9 @@ describe(':terminal cursor', function()
|
||||
{3:-- TERMINAL --} |
|
||||
]],
|
||||
condition = function()
|
||||
eq(0, screen._mode_info[idx].blinkon)
|
||||
eq(0, screen._mode_info[idx].blinkoff)
|
||||
eq('horizontal', screen._mode_info[idx].cursor_shape)
|
||||
eq(0, screen._mode_info[terminal_mode_idx].blinkon)
|
||||
eq(0, screen._mode_info[terminal_mode_idx].blinkoff)
|
||||
eq('horizontal', screen._mode_info[terminal_mode_idx].cursor_shape)
|
||||
end,
|
||||
})
|
||||
|
||||
@ -294,9 +291,9 @@ describe(':terminal cursor', function()
|
||||
{3:-- TERMINAL --} |
|
||||
]],
|
||||
condition = function()
|
||||
eq(500, screen._mode_info[idx].blinkon)
|
||||
eq(500, screen._mode_info[idx].blinkoff)
|
||||
eq('vertical', screen._mode_info[idx].cursor_shape)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkon)
|
||||
eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
|
||||
eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
|
||||
end,
|
||||
})
|
||||
end)
|
||||
@ -326,6 +323,32 @@ describe(':terminal cursor', function()
|
||||
{3:-- TERMINAL --} |
|
||||
]])
|
||||
end)
|
||||
|
||||
it('preserves guicursor value on TermLeave #31612', function()
|
||||
eq(3, screen._mode_info[terminal_mode_idx].hl_id)
|
||||
|
||||
-- Change 'guicursor' while terminal mode is active
|
||||
command('set guicursor+=t:Error')
|
||||
|
||||
local error_hl_id = call('hlID', 'Error')
|
||||
|
||||
screen:expect({
|
||||
condition = function()
|
||||
eq(error_hl_id, screen._mode_info[terminal_mode_idx].hl_id)
|
||||
end,
|
||||
})
|
||||
|
||||
-- Exit terminal mode
|
||||
feed([[<C-\><C-N>]])
|
||||
|
||||
screen:expect([[
|
||||
tty ready |
|
||||
^ |
|
||||
|*5
|
||||
]])
|
||||
|
||||
eq(error_hl_id, screen._mode_info[terminal_mode_idx].hl_id)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('buffer cursor position is correct in terminal without number column', function()
|
||||
|
Loading…
Reference in New Issue
Block a user