From e7753def1078174bfaf1b9f172bf0a8333342ee5 Mon Sep 17 00:00:00 2001 From: V1OL3TF0X Date: Sun, 20 Oct 2024 20:18:12 +0200 Subject: [PATCH 1/4] Add update spinner event to listen to with UI and clean up timers to create only one --- .../ui/generic/loading-status.lua | 52 +++++++++---------- lua/package-info/utils/constants.lua | 1 + 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/lua/package-info/ui/generic/loading-status.lua b/lua/package-info/ui/generic/loading-status.lua index 06c4f53..645efdc 100644 --- a/lua/package-info/ui/generic/loading-status.lua +++ b/lua/package-info/ui/generic/loading-status.lua @@ -19,11 +19,13 @@ local M = { is_running = false, notification = nil, }, + timer = vim.loop.new_timer() } -- 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 @@ -47,7 +49,10 @@ M.new = function(message) table.insert(M.queue, instance) - M.update_spinner(message) + if not M.timer then + M.timer = vim.loop.new_timer() + M.timer:start(60, 60, function() M.update_spinner(message) end) + end return instance.id end @@ -121,41 +126,34 @@ M.update_spinner = function(message) M.state.notification = new_notif end - vim.fn.timer_start(60, function() - M.update_spinner() - end) + -- this can be used to post updates (ex. refresh the statusline) + vim.api.nvim_exec_autocmds('User', { + group = constants.AUTOGROUP, + pattern = constants.LOAD_EVENT + }) 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 + M.update_spinner(instance.message) + 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 "" - 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 + M.timer:stop() + M.timer:close() + M.timer = nil + M.state.is_running = false + M.state.current_spinner = "" + M.state.index = 1 + return "" end return M diff --git a/lua/package-info/utils/constants.lua b/lua/package-info/utils/constants.lua index 3d31488..3204caa 100644 --- a/lua/package-info/utils/constants.lua +++ b/lua/package-info/utils/constants.lua @@ -34,5 +34,6 @@ M.COMMANDS = { } M.AUTOGROUP = "PackageInfoAutogroup" +M.LOAD_EVENT = "PackageInfoStatusUpdate" return M From f2218d5e9457c4591ad06d23b1e92edc771d6b2e Mon Sep 17 00:00:00 2001 From: V1OL3TF0X Date: Sun, 20 Oct 2024 21:07:21 +0200 Subject: [PATCH 2/4] fixes - move timer to state - fix index wrapping - send event to update ui after clearing the spinner --- .../ui/generic/loading-status.lua | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/lua/package-info/ui/generic/loading-status.lua b/lua/package-info/ui/generic/loading-status.lua index 645efdc..e862553 100644 --- a/lua/package-info/ui/generic/loading-status.lua +++ b/lua/package-info/ui/generic/loading-status.lua @@ -18,8 +18,8 @@ local M = { index = 1, is_running = false, notification = nil, + timer = nil }, - timer = vim.loop.new_timer() } -- nvim-notify support @@ -49,9 +49,9 @@ M.new = function(message) table.insert(M.queue, instance) - if not M.timer then - M.timer = vim.loop.new_timer() - M.timer:start(60, 60, function() M.update_spinner(message) end) + 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 @@ -114,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, { @@ -127,10 +127,13 @@ M.update_spinner = function(message) end -- this can be used to post updates (ex. refresh the statusline) - vim.api.nvim_exec_autocmds('User', { - group = constants.AUTOGROUP, - pattern = constants.LOAD_EVENT - }) + vim.schedule(function() + vim.api.nvim_exec_autocmds('User', { + group = constants.AUTOGROUP, + pattern = constants.LOAD_EVENT + }) + end) + end --- Get the first ready instance message if there are instances @@ -142,17 +145,24 @@ M.get = function() return instance.message end M.state.is_running = true - M.update_spinner(instance.message) return instance.message end end - - M.timer:stop() - M.timer:close() - M.timer = nil 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 return "" end From d534ee59ae3c0f99e09be45cf9007f9f2457dd7e Mon Sep 17 00:00:00 2001 From: V1OL3TF0X Date: Sun, 3 Nov 2024 18:34:11 +0100 Subject: [PATCH 3/4] Formatting fix --- .../ui/generic/loading-status.lua | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lua/package-info/ui/generic/loading-status.lua b/lua/package-info/ui/generic/loading-status.lua index e862553..84770a0 100644 --- a/lua/package-info/ui/generic/loading-status.lua +++ b/lua/package-info/ui/generic/loading-status.lua @@ -18,14 +18,14 @@ local M = { index = 1, is_running = false, notification = nil, - timer = nil + timer = nil, }, } -- nvim-notify support local nvim_notify = pcall(require, "notify") local title = "package-info.nvim" -local constants = require'package-info.utils.constants' +local constants = require("package-info.utils.constants") --- Spawn a new loading instance -- @param log: string - message to display in the loading status @@ -51,7 +51,9 @@ M.new = function(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) + M.state.timer:start(60, 60, function() + M.update_spinner(message) + end) end return instance.id @@ -114,7 +116,7 @@ end M.update_spinner = function(message) M.state.current_spinner = SPINNERS[M.state.index] - M.state.index = M.state.index % #SPINNERS + 1 + 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, { @@ -128,9 +130,9 @@ M.update_spinner = function(message) -- this can be used to post updates (ex. refresh the statusline) vim.schedule(function() - vim.api.nvim_exec_autocmds('User', { + vim.api.nvim_exec_autocmds("User", { group = constants.AUTOGROUP, - pattern = constants.LOAD_EVENT + pattern = constants.LOAD_EVENT, }) end) @@ -142,7 +144,7 @@ M.get = function() for _, instance in pairs(M.queue) do if instance.is_ready then if M.state.is_running then - return instance.message + return instance.message end M.state.is_running = true return instance.message @@ -157,9 +159,9 @@ M.get = function() M.state.timer = nil -- ensure this gets called *after* last chedule from update_spinner vim.schedule(function() - vim.api.nvim_exec_autocmds('User', { + vim.api.nvim_exec_autocmds("User", { group = constants.AUTOGROUP, - pattern = constants.LOAD_EVENT + pattern = constants.LOAD_EVENT, }) end) end From fefca76198716bf9d60c9c4b2d0e659c5e420379 Mon Sep 17 00:00:00 2001 From: V1OL3TF0X Date: Sun, 3 Nov 2024 18:46:03 +0100 Subject: [PATCH 4/4] One more (missed an extra line) --- lua/package-info/ui/generic/loading-status.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/package-info/ui/generic/loading-status.lua b/lua/package-info/ui/generic/loading-status.lua index 84770a0..959aadf 100644 --- a/lua/package-info/ui/generic/loading-status.lua +++ b/lua/package-info/ui/generic/loading-status.lua @@ -135,7 +135,6 @@ M.update_spinner = function(message) pattern = constants.LOAD_EVENT, }) end) - end --- Get the first ready instance message if there are instances