diff --git a/lua/plugins/codecompanion/fidget-spinner.lua b/lua/plugins/codecompanion/fidget-spinner.lua index 197103d..af0d227 100644 --- a/lua/plugins/codecompanion/fidget-spinner.lua +++ b/lua/plugins/codecompanion/fidget-spinner.lua @@ -1,7 +1,14 @@ local notify = require("notify") local spinner_frames = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } +local spinner_len = #spinner_frames -- cache spinner length 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() local group = vim.api.nvim_create_augroup("CodeCompanionFidgetHooks", {}) @@ -41,29 +48,38 @@ function M:pop_progress_handle(id) end 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 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() - if timer == nil then - return - end timer:start( 0, 100, vim.schedule_wrap(function() - idx = idx % #spinner_frames + 1 - local new_id = notify( - spinner_frames[idx] .. " In progress...", - "info", - { replace = handle.notification_id, title = title, timeout = false } - ) - handle.notification_id = new_id + if handle.finished then + return + end -- stop updating if finished + idx = idx % spinner_len + 1 + 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 + else + handle.notification_id = + notify(spinner_frames[idx] .. " In progress...", "info", { title = title, timeout = false }) + end end) ) handle.timer = timer handle.finish = function() + handle.finished = true -- mark as finished to abort future updates if handle.timer then handle.timer:stop() handle.timer:close() @@ -74,13 +90,30 @@ function M:create_progress_handle(request) end 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 - notify("Completed", "info", { replace = handle.notification_id, title = title, timeout = timeout }) + report("Completed", "info") elseif request.data.status == "error" then - notify(" Error", "error", { replace = handle.notification_id, title = title, timeout = timeout }) + report(" Error", "error") else - notify("󰜺 Cancelled", "warn", { replace = handle.notification_id, title = title, timeout = timeout }) + report("󰜺 Cancelled", "warn") end end