fix codecompanion fidget spinner

This commit is contained in:
sudacode 2025-02-20 19:20:49 -08:00
parent dc29310cf1
commit b29d79a356
Signed by: sudacode
SSH Key Fingerprint: SHA256:lT5C2bB398DcX6daCF/gYFNSTK3y+Du3oTGUnYzfTEw

View File

@ -1,7 +1,14 @@
local notify = require("notify") local notify = require("notify")
local spinner_frames = { "", "", "", "", "", "", "", "", "", "" } local spinner_frames = { "", "", "", "", "", "", "", "", "", "" }
local spinner_len = #spinner_frames -- cache spinner length
local M = {} local M = {}
local timeout = 3000 local timeout = 2999
-- Helper function to safely call notify
local function safe_notify(msg, level, opts)
local ok, res = pcall(notify, msg, level, opts)
return ok, res
end
function M:init() function M:init()
local group = vim.api.nvim_create_augroup("CodeCompanionFidgetHooks", {}) local group = vim.api.nvim_create_augroup("CodeCompanionFidgetHooks", {})
@ -41,29 +48,38 @@ function M:pop_progress_handle(id)
end end
function M:create_progress_handle(request) function M:create_progress_handle(request)
local title = " Requesting assistance (" .. request.data.strategy .. ")" local title = " Requesting assistance"
.. " ("
.. request.data.strategy
.. ") from "
.. request.data.adapter.formatted_name
.. " using "
.. request.data.adapter.model
local idx = 1 local idx = 1
local notification_id = notify(spinner_frames[idx] .. " In progress...", "info", { title = title, timeout = false }) local notification_id = notify(spinner_frames[idx] .. " In progress...", "info", { title = title, timeout = false })
local handle = { notification_id = notification_id, title = title } local handle = { notification_id = notification_id, title = title, finished = false }
local timer = vim.loop.new_timer() local timer = vim.loop.new_timer()
if timer == nil then
return
end
timer:start( timer:start(
0, 0,
100, 100,
vim.schedule_wrap(function() vim.schedule_wrap(function()
idx = idx % #spinner_frames + 1 if handle.finished then
local new_id = notify( return
spinner_frames[idx] .. " In progress...", end -- stop updating if finished
"info", idx = idx % spinner_len + 1
{ replace = handle.notification_id, title = title, timeout = false } local opts = { replace = handle.notification_id, title = title, timeout = false }
) local ok, new_id = safe_notify(spinner_frames[idx] .. " In progress...", "info", opts)
if ok then
handle.notification_id = new_id handle.notification_id = new_id
else
handle.notification_id =
notify(spinner_frames[idx] .. " In progress...", "info", { title = title, timeout = false })
end
end) end)
) )
handle.timer = timer handle.timer = timer
handle.finish = function() handle.finish = function()
handle.finished = true -- mark as finished to abort future updates
if handle.timer then if handle.timer then
handle.timer:stop() handle.timer:stop()
handle.timer:close() handle.timer:close()
@ -74,13 +90,30 @@ function M:create_progress_handle(request)
end end
function M:report_exit_status(handle, request) function M:report_exit_status(handle, request)
local title = handle.title or (" Requesting assistance (" .. request.data.strategy .. ")") local title = handle.title
or (
" Requesting assistance"
.. " ("
.. request.data.strategy
.. ") from "
.. request.data.adapter.formatted_name
.. " using "
.. request.data.adapter.model
)
local function report(msg, level)
local opts = { replace = handle.notification_id, title = title, timeout = timeout }
local ok = safe_notify(msg, level, opts)
if not ok then
notify(msg, level, { title = title, timeout = timeout })
end
end
if request.data.status == "success" then if request.data.status == "success" then
notify("Completed", "info", { replace = handle.notification_id, title = title, timeout = timeout }) report("Completed", "info")
elseif request.data.status == "error" then elseif request.data.status == "error" then
notify(" Error", "error", { replace = handle.notification_id, title = title, timeout = timeout }) report(" Error", "error")
else else
notify("󰜺 Cancelled", "warn", { replace = handle.notification_id, title = title, timeout = timeout }) report("󰜺 Cancelled", "warn")
end end
end end