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:
Chip Senkbeil 2024-11-30 13:55:51 -06:00
parent 25820b5e24
commit ce26f7c332
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131
3 changed files with 46 additions and 0 deletions

View File

@ -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

View 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 {
}

View File

@ -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