nvim/.config/nvim/lua/util/float.lua

-- nvim/.config/nvim/lua/util/float.lua
-- Tiny helper to open a centered floating scratch buffer.

local M = {}

---@param opts table|nil { width=0..1, height=0..1, border='rounded', title=string }
function M.open(opts)
  opts = opts or {}
  local w_ratio = opts.width  or 0.7
  local h_ratio = opts.height or 0.6

  local ui = vim.api.nvim_list_uis()[1]
  local W  = math.floor(ui.width  * w_ratio)
  local H  = math.floor(ui.height * h_ratio)
  local row = math.floor((ui.height - H) / 2)
  local col = math.floor((ui.width  - W) / 2)

  local buf = vim.api.nvim_create_buf(false, true)
  vim.bo[buf].bufhidden = "wipe"
  vim.bo[buf].swapfile  = false

  local win = vim.api.nvim_open_win(buf, true, {
    relative = "editor",
    row      = row,
    col      = col,
    width    = W,
    height   = H,
    style    = "minimal",
    border   = opts.border or "rounded",
    title    = opts.title,
    title_pos = opts.title and "center" or nil,
  })

  vim.wo[win].winhl = "Normal:NormalFloat,FloatBorder:FloatBorder"

  vim.keymap.set("n", "q",     "<cmd>close<cr>", { buffer = buf, silent = true })
  vim.keymap.set("n", "<esc>", "<cmd>close<cr>", { buffer = buf, silent = true })

  return { buf = buf, win = win }
end

return M