mirror of
https://github.com/neovim/neovim.git
synced 2024-12-23 20:55:18 -07:00
feat(img): implement skeleton of vim.img.show()
without backends
Implement the skeleton of `vim.img.show()` without any backend implemented.
This commit is contained in:
parent
25820b5e24
commit
ce26f7c332
@ -1,4 +1,5 @@
|
||||
local img = vim._defer_require('vim.img', {
|
||||
_backend = ..., --- @module 'vim.img._backend'
|
||||
_image = ..., --- @module 'vim.img._image'
|
||||
})
|
||||
|
||||
@ -11,4 +12,33 @@ function img.load(opts)
|
||||
return img._image:new(opts)
|
||||
end
|
||||
|
||||
---@class vim.img.Protocol 'iterm2'|'kitty'|'sixel'
|
||||
|
||||
---@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
|
||||
|
||||
-- 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
|
||||
|
10
runtime/lua/vim/img/_backend.lua
Normal file
10
runtime/lua/vim/img/_backend.lua
Normal file
@ -0,0 +1,10 @@
|
||||
---@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 {
|
||||
}
|
@ -33,6 +33,12 @@ function M:size()
|
||||
return string.len(self.data or '')
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user