cp.nvim/lua/cp/commands/cache.lua
Barrett Ruth 29af2df858
refactor(logger): table-based LogOpts; add sync, on_done to test stream
Problem: `logger.log` positional args were hard to extend, and adding
`sync` support for pre-block notifications required a clean API. Test
stream completion had no user-visible signal. `setup_contest` could
silently overwrite files when a user's `filename` config returned
colliding paths.

Solution: Replace `(msg, level, override)` with `(msg, LogOpts?)` where
`LogOpts` carries `level`, `override`, and `sync`. Sync path calls
`vim.notify` directly; async path uses `vim.schedule` as before. Add
`on_done` callback to `scrape_all_tests`, fired via `on_exit` and
surfaced as a "Loaded N tests." notification. Detect filename collisions
in `proceed()` before touching the filesystem. Migrate all call sites.
2026-03-05 12:54:37 -05:00

72 lines
2.2 KiB
Lua

local M = {}
local cache = require('cp.cache')
local constants = require('cp.constants')
local logger = require('cp.log')
local platforms = constants.PLATFORMS
--- Dispatch any `:CP cache ...` command
---@param cmd table
---@return nil
function M.handle_cache_command(cmd)
if cmd.subcommand == 'read' then
local data = cache.get_data_pretty()
local name = 'cp.nvim://cache.lua'
local existing = vim.fn.bufnr(name)
local buf
if existing ~= -1 then
buf = existing
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(data, '\n'))
else
buf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_name(buf, name)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(data, '\n'))
vim.bo[buf].filetype = 'lua'
vim.bo[buf].buftype = 'nofile'
vim.bo[buf].bufhidden = 'wipe'
vim.bo[buf].swapfile = false
vim.api.nvim_buf_set_keymap(
buf,
'n',
'q',
'<cmd>bd!<cr>',
{ nowait = true, noremap = true, silent = true }
)
end
vim.api.nvim_set_current_buf(buf)
elseif cmd.subcommand == 'clear' then
cache.load()
if cmd.platform and cmd.contest then
if vim.tbl_contains(platforms, cmd.platform) then
cache.clear_contest_data(cmd.platform, cmd.contest)
logger.log(
("Cache cleared for %s contest '%s'"):format(
constants.PLATFORM_DISPLAY_NAMES[cmd.platform],
cmd.contest
),
{ level = vim.log.levels.INFO, override = true }
)
else
logger.log(("Unknown platform '%s'."):format(cmd.platform), { level = vim.log.levels.ERROR })
end
elseif cmd.platform then
if vim.tbl_contains(platforms, cmd.platform) then
cache.clear_platform(cmd.platform)
logger.log(
("Cache cleared for platform '%s'"):format(constants.PLATFORM_DISPLAY_NAMES[cmd.platform]),
{ level = vim.log.levels.INFO, override = true }
)
else
logger.log(("Unknown platform '%s'."):format(cmd.platform), { level = vim.log.levels.ERROR })
end
else
cache.clear_all()
logger.log('Cache cleared', { level = vim.log.levels.INFO, override = true })
end
end
end
return M