robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
4.3 KiB151 linesraw
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)
61  opts = opts or {}
62  vim.validate('data_or_id', data_or_id, { 'string', 'number' })
63  vim.validate('opts', opts, 'table')
64
65  local kitty = require('vim.ui.img._kitty')
66
67  -- If given a string, this should be the bytes of a new image to display
68  if type(data_or_id) == 'string' then
69    local img_id, placement_id = kitty.set(data_or_id, opts)
70    state[placement_id] = { img_id = img_id, opts = vim.deepcopy(opts) }
71    return placement_id
72  end
73
74  -- Otherwise, we update an existing image that is actively displayed
75  local id = data_or_id
76  local entry = state[id]
77  assert(entry, 'invalid image id: ' .. tostring(id))
78
79  -- We always want to have a full set of options when passing to kitty
80  local merged = vim.tbl_extend('force', entry.opts, opts)
81  kitty.update(entry.img_id, id, merged)
82  entry.opts = merged
83  return 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)
91  vim.validate('id', id, 'number')
92
93  -- Grab a copy of the most recent opts used for the image
94  local entry = state[id]
95  if not entry then
96    return nil
97  end
98
99  return 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)
107  vim.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
112  if id == math.huge then
113    local has_ids = next(state) ~= nil
114    state = {}
115
116    if has_ids then
117      local kitty = require('vim.ui.img._kitty')
118      kitty.delete(math.huge --[[@as integer]])
119    end
120
121    return has_ids
122  end
123
124  -- Skip performing the deletion if we don't have an active image with the id
125  local entry = state[id]
126  if not entry then
127    return false
128  end
129
130  local kitty = require('vim.ui.img._kitty')
131  kitty.delete(entry.img_id)
132  state[id] = nil
133  return 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)
144  return require('vim.ui.img._kitty').supported(opts)
145end
146
147nvim_on('VimLeavePre', nil, function()
148  M.del(math.huge --[[@as integer]])
149end)
150
151return M