This commit is contained in:
2025-02-26 14:55:20 -08:00
parent e5a4785c0c
commit b4789ac055
7 changed files with 166 additions and 136 deletions

View File

@@ -1,14 +1,8 @@
local notify = require("notify")
local spinner_frames = { "", "", "", "", "", "", "", "", "", "" }
local spinner_len = #spinner_frames -- cache spinner length
local M = {}
local timeout = 2999
-- lua/plugins/codecompanion/fidget-spinner.lua
-- 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
local progress = require("fidget.progress")
local M = {}
function M:init()
local group = vim.api.nvim_create_augroup("CodeCompanionFidgetHooks", {})
@@ -48,78 +42,31 @@ function M:pop_progress_handle(id)
end
function M:create_progress_handle(request)
local title = " Requesting assistance"
.. " ("
.. request.data.strategy
.. ") from "
.. request.data.adapter.formatted_name
.. " using "
.. request.data.adapter.model
local idx = 1
local start_time = os.time()
local notification_id =
notify(spinner_frames[idx] .. " In progress (" .. "0s" .. ")...", "info", { title = title, timeout = false })
local handle = { notification_id = notification_id, title = title, finished = false }
local timer = vim.loop.new_timer()
timer:start(
0,
100,
vim.schedule_wrap(function()
if handle.finished then
return
end -- stop updating if finished
idx = idx % spinner_len + 1
local elapsed = os.difftime(os.time(), start_time)
local opts = { replace = handle.notification_id, title = title, timeout = false }
local ok, new_id = safe_notify(spinner_frames[idx] .. " In progress (" .. elapsed .. "s)...", "info", opts)
if ok then
handle.notification_id = new_id
else
handle.notification_id = notify(
spinner_frames[idx] .. " In progress (" .. elapsed .. "s)...",
"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()
handle.timer = nil
end
return progress.handle.create({
title = " Requesting assistance (" .. request.data.strategy .. ")",
message = "In progress...",
lsp_client = {
name = M:llm_role_title(request.data.adapter),
},
})
end
function M:llm_role_title(adapter)
local parts = {}
table.insert(parts, adapter.formatted_name)
if adapter.model and adapter.model ~= "" then
table.insert(parts, "(" .. adapter.model .. ")")
end
return handle
return table.concat(parts, " ")
end
function M:report_exit_status(handle, request)
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
report("Completed", "info")
handle.message = "Completed"
elseif request.data.status == "error" then
report(" Error", "error")
handle.message = " Error"
else
report("󰜺 Cancelled", "warn")
handle.message = "󰜺 Cancelled"
end
end