Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/lua/bugabinga/image/kitty.lua

Raw
-- Modified from folke/snacks.nvim (Apache-2.0); see lua/bugabinga/image/NOTICE.
local MAX_ID = 0xFFFFFFFF
local CHUNK_BYTES = 3072
local next_image_id = tonumber(vim.fn.sha256(tostring(vim.uv.hrtime()) .. ':' .. vim.fn.getpid()):sub(1, 7), 16)

local function valid_id(value, name)
  vim.validate(name, value, 'number')
  if value < 1 or value > MAX_ID or value % 1 ~= 0 then
    error(name .. ' must be an integer between 1 and 4294967295')
  end
end

local function send(command)
  local ok, err = pcall(vim.api.nvim_ui_send, command)
  if not ok then
    return nil, err
  end
  return true
end

local function transfer_chunk(image_id, payload, first, more)
  local fields
  if first then
    fields = ('a=t,f=100,t=d,i=%d,m=%d,q=1;'):format(image_id, more and 1 or 0)
  else
    fields = ('m=%d,q=1;'):format(more and 1 or 0)
  end
  return send('\27_G' .. fields .. payload .. '\27\\')
end

local function new_id()
  if next_image_id == MAX_ID then
    error('Kitty image ID space exhausted')
  end
  next_image_id = next_image_id + 1
  return next_image_id
end

local function upload(image_id, path)
  valid_id(image_id, 'image_id')
  vim.validate('path', path, 'string')
  local stat = vim.uv.fs_stat(path)
  if not stat or stat.type ~= 'file' then
    return nil, 'path must be a regular file'
  end
  local file, err = io.open(path, 'rb')
  if not file then
    return nil, err
  end
  local pending = file:read(CHUNK_BYTES)
  if not pending then
    file:close()
    return nil, 'image file is empty'
  end
  local first = true
  while true do
    local current = pending
    pending = file:read(CHUNK_BYTES)
    local more = pending ~= nil
    local ok, send_err = transfer_chunk(image_id, vim.base64.encode(current), first, more)
    if not ok then
      file:close()
      return nil, send_err
    end
    first = false
    if not more then
      break
    end
  end
  local closed, close_err = file:close()
  if closed == nil then
    return nil, close_err
  end
  return true
end

local function place(image_id, placement_id, rect)
  valid_id(image_id, 'image_id')
  valid_id(placement_id, 'placement_id')
  vim.validate('rect', rect, 'table')
  for _, field in ipairs({ 'row', 'col', 'cols', 'rows' }) do
    vim.validate(field, rect[field], 'number')
    if rect[field] < 1 or rect[field] % 1 ~= 0 then
      error(field .. ' must be a positive integer')
    end
  end
  local size = rect.axis == 'rows' and ('r=' .. rect.rows) or ('c=' .. rect.cols)
  if rect.axis == 'both' then size = ('c=%d,r=%d'):format(rect.cols, rect.rows) end
  local command = ('\27[s\27[%d;%dH\27_Ga=p,i=%d,p=%d,%s,C=1,q=1\27\\\27[u'):format(
    rect.row, rect.col, image_id, placement_id, size
  )
  return send(command)
end

local function delete(image_id, placement_id)
  valid_id(image_id, 'image_id')
  local command
  if placement_id ~= nil then
    valid_id(placement_id, 'placement_id')
    command = ('\27_Ga=d,d=i,i=%d,p=%d,q=1\27\\'):format(image_id, placement_id)
  else
    command = ('\27_Ga=d,d=I,i=%d,q=1\27\\'):format(image_id)
  end
  return send(command)
end

return { new_id = new_id, upload = upload, place = place, delete = delete }