Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/lua/bugabinga/image/terminal.lua

Raw
-- Modified from folke/snacks.nvim (Apache-2.0); see lua/bugabinga/image/NOTICE.
local metrics
local pending
local dirty = false
local callbacks = {}

local function available()
  for _, ui in ipairs(vim.api.nvim_list_uis()) do
    local channel = vim.api.nvim_get_chan_info(ui.chan)
    if channel.client and channel.client.name == 'nvim-tui' then return true end
  end
  return false
end

local function valid(width, height)
  return type(width) == 'number' and type(height) == 'number'
    and width > 0 and height > 0 and width < math.huge and height < math.huge
end

local function ioctl_metrics()
  local request = ({ Linux = 0x5413, OSX = 0x40087468, BSD = 0x40087468 })[jit.os]
  if not request then return end
  local ok, result = pcall(function()
    local ffi = require 'ffi'
    if not pcall(ffi.typeof, 'bugabinga_image_winsize') then
      ffi.cdef [[
        typedef struct { unsigned short row, col, xpixel, ypixel; } bugabinga_image_winsize;
        int ioctl(int, unsigned long, ...);
      ]]
    end
    local size = ffi.new('bugabinga_image_winsize')
    if ffi.C.ioctl(1, request, size) ~= 0 or size.row == 0 or size.col == 0 then return end
    if not valid(tonumber(size.xpixel), tonumber(size.ypixel)) then return end
    return { cell_width = size.xpixel / size.col, cell_height = size.ypixel / size.row }
  end)
  if ok then return result end
end

local function decode(panes)
  vim.validate('panes', panes, 'table')
  for _, pane in ipairs(panes) do
    if type(pane) == 'table' and tostring(pane.pane_id) == vim.env.WEZTERM_PANE then
      local size = type(pane.size) == 'table' and pane.size or {}
      if valid(size.cols, size.rows) and valid(size.pixel_width, size.pixel_height) then
        return { cell_width = size.pixel_width / size.cols, cell_height = size.pixel_height / size.rows,
          explicit_size = true }
      end
      return nil, 'WezTerm did not report valid pane pixel dimensions'
    end
  end
  return nil, 'Current pane was not found in WezTerm'
end

local function get()
  if not vim.env.WEZTERM_PANE then return ioctl_metrics() end
  return metrics
end

local function refresh(callback)
  vim.validate('callback', callback, 'function')
  if not available() then
    vim.schedule(function() callback(nil, 'Image viewing requires a Neovim terminal UI') end)
    return
  end
  if not vim.env.WEZTERM_PANE then
    local size = ioctl_metrics()
    vim.schedule(function()
      callback(size, not size and 'Terminal did not provide valid cell pixel dimensions' or nil)
    end)
    return
  end
  callbacks[#callbacks + 1] = callback
  if pending then dirty = true; return end
  local function finish(result, err)
    vim.schedule(function()
      pending = nil
      local waiting = callbacks
      callbacks = {}
      if dirty then
        dirty = false
        refresh(function(size, failure)
          for _, cb in ipairs(waiting) do cb(size, failure) end
        end)
        return
      end
      metrics = result
      for _, cb in ipairs(waiting) do cb(result, err) end
    end)
  end
  local ok, job = pcall(vim.system, { 'wezterm', 'cli', 'list', '--format', 'json' },
    { text = true, timeout = 2000 }, vim.schedule_wrap(function(result)
      if result.code ~= 0 then
        finish(nil, 'Cannot measure WezTerm pane: ' .. vim.trim(result.stderr or 'command failed'))
        return
      end
      local parsed, panes = pcall(vim.json.decode, result.stdout)
      if not parsed or type(panes) ~= 'table' then
        finish(nil, 'Invalid WezTerm pane response')
        return
      end
      finish(decode(panes))
    end))
  if ok then
    pending = job
  else
    finish(nil, tostring(job))
  end
end

return { available = available, get = get, refresh = refresh, decode = decode }