Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event to enable UI updates + cleanup + fixes #162

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions lua/package-info/ui/generic/loading-status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ local M = {
index = 1,
is_running = false,
notification = nil,
timer = nil
},
}

-- nvim-notify support
local nvim_notify = pcall(require, "notify")
local title = "package-info.nvim"
local constants = require'package-info.utils.constants'

--- Spawn a new loading instance
-- @param log: string - message to display in the loading status
Expand All @@ -47,7 +49,10 @@ M.new = function(message)

table.insert(M.queue, instance)

M.update_spinner(message)
if not M.state.timer then
M.state.timer = vim.loop.new_timer()
M.state.timer:start(60, 60, function() M.update_spinner(message) end)
end

return instance.id
end
Expand Down Expand Up @@ -109,7 +114,7 @@ end
M.update_spinner = function(message)
M.state.current_spinner = SPINNERS[M.state.index]

M.state.index = (M.state.index + 1) % #SPINNERS
M.state.index = M.state.index % #SPINNERS + 1

if nvim_notify and M.state.notification then
local new_notif = vim.notify(message, vim.log.levels.INFO, {
Expand All @@ -121,41 +126,44 @@ M.update_spinner = function(message)
M.state.notification = new_notif
end

vim.fn.timer_start(60, function()
M.update_spinner()
-- this can be used to post updates (ex. refresh the statusline)
vim.schedule(function()
vim.api.nvim_exec_autocmds('User', {
vuki656 marked this conversation as resolved.
Show resolved Hide resolved
group = constants.AUTOGROUP,
pattern = constants.LOAD_EVENT
})
end)

end

--- Get the first ready instance message if there are instances
-- @return string
M.get = function()
local active_instance = nil

for _, instance in pairs(M.queue) do
if not active_instance and instance.is_ready then
active_instance = instance
if instance.is_ready then
if M.state.is_running then
return instance.message
end
M.state.is_running = true
return instance.message
end
end

if not active_instance then
-- FIXME: this is killing all timers, so if a user has any timers, it will interrupt them
-- like lsp status
-- vim.fn.timer_stopall()

M.state.is_running = false
M.state.current_spinner = ""
M.state.index = 1

return ""
M.state.is_running = false
M.state.current_spinner = ""
M.state.index = 1
if M.state.timer then
M.state.timer:stop()
M.state.timer:close()
M.state.timer = nil
-- ensure this gets called *after* last chedule from update_spinner
vim.schedule(function()
vim.api.nvim_exec_autocmds('User', {
group = constants.AUTOGROUP,
pattern = constants.LOAD_EVENT
})
end)
end

if active_instance and not M.state.is_running then
M.state.is_running = true

M.update_spinner(active_instance.message)
end

return active_instance.message
return ""
end

return M
1 change: 1 addition & 0 deletions lua/package-info/utils/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ M.COMMANDS = {
}

M.AUTOGROUP = "PackageInfoAutogroup"
M.LOAD_EVENT = "PackageInfoStatusUpdate"

return M
Loading