robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local nvim_on =require ( 'vim._core.util' ). nvim_on 2 3local M ={} 4 5---@brief 6--- 7---EXPERIMENTAL: This API may change in the future. Its semantics are not yet finalized. 8--- 9---This provides a functional API for displaying images in Nvim. 10---Currently supports PNG images via the Kitty graphics protocol. 11--- 12---To override the image backend, replace `vim.ui.img` with your own 13---implementation providing set/get/del. 14--- 15---Examples: 16--- 17---```lua 18----- Load image bytes from disk and display at row 5, column 10 19---local id = vim.ui.img.set( 20--- vim.fn.readblob('/path/to/img.png'), 21--- { row = 5, col = 10, width = 40, height = 20, zindex = 50 } 22---) 23--- 24----- Update the image position 25---vim.ui.img.set(id, { row = 8, col = 12 }) 26--- 27----- Retrieve the current image opts 28---local opts = vim.ui.img.get(id) 29--- 30----- Remove the image 31---vim.ui.img.del(id) 32--- 33----- Remove all images 34---vim.ui.img.del(math.huge) 35---``` 36 37---@class vim.ui.img.Opts 38---@inlinedoc 39---@field row? integer starting row (1-indexed) 40---@field col? integer starting column (1-indexed) 41---@field width? integer width in cells 42---@field height? integer height in cells 43---@field zindex? integer stacking order (higher = on top) 44 45--- Maps user-facing ID to internal tracking info. 46---@type table<integer, { img_id: integer, opts: vim.ui.img.Opts }?> 47local state ={} 48 49---Display an image or update an existing one. 50--- 51---When {data_or_id} is a string, displays the image bytes at the position 52---given by {opts}. Returns an integer id for later use. 53--- 54---When {data_or_id} is an integer (a previously returned id), updates 55---the image with new {opts}. 56--- 57---@param data_or_id string|integer image bytes (string) or existing id (integer) 58---@param opts? vim.ui.img.Opts 59---@return integer id 60function M . set ( data_or_id , opts ) 61opts =opts or {} 62vim . validate ( 'data_or_id' , data_or_id , { 'string' , 'number' } ) 63vim . validate ( 'opts' , opts , 'table' ) 64 65local kitty =require ( 'vim.ui.img._kitty' ) 66 67-- If given a string, this should be the bytes of a new image to display 68if type ( data_or_id ) == 'string' then 69local img_id , placement_id =kitty . set ( data_or_id , opts ) 70state [ placement_id ] ={ img_id =img_id , opts =vim . deepcopy ( opts ) } 71return placement_id 72end 73 74-- Otherwise, we update an existing image that is actively displayed 75local id =data_or_id 76local entry =state [ id ] 77assert ( entry , 'invalid image id: ' .. tostring ( id )) 78 79-- We always want to have a full set of options when passing to kitty 80local merged =vim . tbl_extend ( 'force' , entry . opts , opts ) 81kitty . update ( entry . img_id , id , merged ) 82entry . opts =merged 83return id 84end 85 86---Get the opts for an image. 87--- 88---@param id integer 89---@return vim.ui.img.Opts? opts copy of image opts, or nil if not found 90function M . get ( id ) 91vim . validate ( 'id' , id , 'number' ) 92 93-- Grab a copy of the most recent opts used for the image 94local entry =state [ id ] 95if not entry then 96return nil 97end 98 99return vim . deepcopy ( entry . opts ) 100end 101 102---Delete an image, or all images if `math.huge` is given as the id. 103--- 104---@param id integer image id, or `math.huge` to delete all images 105---@return boolean found true if any image existed 106function M . del ( id ) 107vim . validate ( 'id' , id , 'number' ) 108 109-- Check if we have images to delete, and clear them out of our memory 110-- being tracked here and also send a request to delete them all from 111-- the terminal 112if id == math . huge then 113local has_ids =next ( state ) ~= nil 114state ={} 115 116if has_ids then 117local kitty =require ( 'vim.ui.img._kitty' ) 118kitty . delete ( math . huge --[[@as integer]] ) 119end 120 121return has_ids 122end 123 124-- Skip performing the deletion if we don't have an active image with the id 125local entry =state [ id ] 126if not entry then 127return false 128end 129 130local kitty =require ( 'vim.ui.img._kitty' ) 131kitty . delete ( entry . img_id ) 132state [ id ] =nil 133return true 134end 135 136---@private 137--- Query whether the host terminal supports displaying images. 138--- Blocks until the terminal responds or times out. 139--- 140---@param opts? {timeout?: integer, chan?: integer} timeout in milliseconds (default: 1000) 141---@return boolean supported true if the terminal supports image display 142---@return string? msg error detail if the terminal responded but not with OK 143function M . _supported ( opts ) 144return require ( 'vim.ui.img._kitty' ). supported ( opts ) 145end 146 147nvim_on ( 'VimLeavePre' , nil , function () 148M . del ( math . huge --[[@as integer]] ) 149end ) 150 151return M