diff --git a/doc/gitlab.nvim.txt b/doc/gitlab.nvim.txt index 523afa20..2f344f48 100644 --- a/doc/gitlab.nvim.txt +++ b/doc/gitlab.nvim.txt @@ -253,6 +253,7 @@ you call this function with no values the defaults will be used: collapsed = " ", -- Icon for collapsed discussion thread indentation = " ", -- Indentation Icon }, + spinner_chars = { "/", "|", "\\", "-" }, -- Characters for the refresh animation auto_open = true, -- Automatically open when the reviewer is opened default_view = "discussions" -- Show "discussions" or "notes" by default blacklist = {}, -- List of usernames to remove from tree (bots, CI, etc) diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index aaa4d48f..7130bfe2 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -48,13 +48,15 @@ M.rebuild_view = function(unlinked, all) else M.rebuild_discussion_tree() end - M.refresh_diagnostics_and_winbar() + state.discussion_tree.last_updated = os.time() + M.refresh_diagnostics() end) end ---Makes API call to get the discussion data, stores it in the state, and calls the callback ---@param callback function|nil M.load_discussions = function(callback) + state.discussion_tree.last_updated = nil state.load_new_state("discussion_data", function(data) if not state.DISCUSSION_DATA then state.DISCUSSION_DATA = {} @@ -70,9 +72,10 @@ end ---Initialize everything for discussions like setup of signs, callbacks for reviewer, etc. M.initialize_discussions = function() + state.discussion_tree.last_updated = os.time() signs.setup_signs() reviewer.set_callback_for_file_changed(function() - M.refresh_diagnostics_and_winbar() + M.refresh_diagnostics() M.modifiable(false) reviewer.set_reviewer_keymaps() end) @@ -102,11 +105,10 @@ M.modifiable = function(bool) end --- Take existing data and refresh the diagnostics, the winbar, and the signs -M.refresh_diagnostics_and_winbar = function() +M.refresh_diagnostics = function() if state.settings.discussion_signs.enabled then diagnostics.refresh_diagnostics() end - winbar.update_winbar() common.add_empty_titles() end @@ -154,7 +156,7 @@ M.open = function(callback) end vim.schedule(function() - M.refresh_diagnostics_and_winbar() + M.refresh_diagnostics() end) end @@ -585,7 +587,7 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked) if keymaps.discussion_tree.jump_to_reviewer then vim.keymap.set("n", keymaps.discussion_tree.jump_to_reviewer, function() if M.is_current_node_note(tree) then - common.jump_to_reviewer(tree, M.refresh_diagnostics_and_winbar) + common.jump_to_reviewer(tree, M.refresh_diagnostics) end end, { buffer = bufnr, desc = "Jump to reviewer", nowait = keymaps.discussion_tree.jump_to_reviewer_nowait }) end @@ -603,7 +605,6 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked) if keymaps.discussion_tree.refresh_data then vim.keymap.set("n", keymaps.discussion_tree.refresh_data, function() - u.notify("Refreshing data...", vim.log.levels.INFO) draft_notes.rebuild_view(unlinked, false) end, { buffer = bufnr, @@ -802,7 +803,6 @@ end ---Toggle between draft mode (comments posted as drafts) and live mode (comments are posted immediately) M.toggle_draft_mode = function() state.settings.discussion_tree.draft_mode = not state.settings.discussion_tree.draft_mode - winbar.update_winbar() end ---Toggle between sorting by "original comment" (oldest at the top) or "latest reply" (newest at the diff --git a/lua/gitlab/actions/discussions/winbar.lua b/lua/gitlab/actions/discussions/winbar.lua index f526a2ac..c3fa8617 100644 --- a/lua/gitlab/actions/discussions/winbar.lua +++ b/lua/gitlab/actions/discussions/winbar.lua @@ -54,7 +54,19 @@ local get_data = function(nodes) return total_resolvable, total_resolved, total_non_resolvable end +local spinner_index = 0 +state.discussion_tree.last_updated = nil + local function content() + local updated + if state.discussion_tree.last_updated then + local last_update = tostring(os.date("!%Y-%m-%dT%H:%M:%S", state.discussion_tree.last_updated)) + updated = "Updated: " .. u.time_since(last_update) + else + spinner_index = (spinner_index % #state.settings.discussion_tree.spinner_chars) + 1 + updated = "Updated: " .. state.settings.discussion_tree.spinner_chars[spinner_index] + end + local resolvable_discussions, resolved_discussions, non_resolvable_discussions = get_data(state.DISCUSSION_DATA.discussions) local resolvable_notes, resolved_notes, non_resolvable_notes = get_data(state.DISCUSSION_DATA.unlinked_discussions) @@ -82,9 +94,10 @@ local function content() resolved_notes = resolved_notes, non_resolvable_notes = non_resolvable_notes, help_keymap = state.settings.keymaps.help, + updated = updated, } - return M.make_winbar(t) + return state.settings.discussion_tree.winbar and state.settings.discussion_tree.winbar(t) or M.make_winbar(t) end ---This function updates the winbar @@ -175,13 +188,16 @@ M.make_winbar = function(t) -- Join everything together and return it local separator = "%#Comment#|" local end_section = "%=" + local updated = "%#Text#" .. t.updated local help = "%#Comment#Help: " .. (t.help_keymap and t.help_keymap:gsub(" ", "") .. " " or "unmapped") return string.format( - " %s %s %s %s %s %s %s %s %s", + " %s %s %s %s %s %s %s %s %s %s %s", discussion_title, separator, notes_title, end_section, + updated, + separator, sort_method, separator, mode, @@ -225,4 +241,8 @@ M.switch_view_type = function(override) M.update_winbar() end +-- Set up a timer to update the winbar periodically +local timer = vim.uv.new_timer() +timer:start(0, 100, vim.schedule_wrap(M.update_winbar)) + return M diff --git a/lua/gitlab/actions/draft_notes/init.lua b/lua/gitlab/actions/draft_notes/init.lua index ea7b5b42..e23665e9 100755 --- a/lua/gitlab/actions/draft_notes/init.lua +++ b/lua/gitlab/actions/draft_notes/init.lua @@ -25,6 +25,7 @@ end ---Makes API call to get the discussion data, stores it in the state, and calls the callback ---@param callback function|nil M.load_draft_notes = function(callback) + state.discussion_tree.last_updated = nil state.load_new_state("draft_notes", function() if callback ~= nil then callback() diff --git a/lua/gitlab/annotations.lua b/lua/gitlab/annotations.lua index 9b1ae923..b1ade3c8 100644 --- a/lua/gitlab/annotations.lua +++ b/lua/gitlab/annotations.lua @@ -92,6 +92,7 @@ ---@field resolved_notes number ---@field non_resolvable_notes number ---@field help_keymap string +---@field updated string --- ---@class SignTable ---@field name string diff --git a/lua/gitlab/init.lua b/lua/gitlab/init.lua index c0f2aab5..f17c6320 100644 --- a/lua/gitlab/init.lua +++ b/lua/gitlab/init.lua @@ -96,7 +96,6 @@ return { publish_all_drafts = draft_notes.publish_all_drafts, refresh_data = function() -- This also rebuilds the regular views - u.notify("Refreshing data...", vim.log.levels.INFO) draft_notes.rebuild_view(false, true) end, -- Other functions 🤷 diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 22278c67..1da4396f 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -151,6 +151,7 @@ M.settings = { collapsed = " ", indentation = " ", }, + spinner_chars = { "-", "\\", "|", "/" }, auto_open = true, default_view = "discussions", blacklist = {}, diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index aa71136b..b75c597e 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -122,7 +122,7 @@ M.time_since = function(date_string, current_date_table) local time_diff = current_date - date if time_diff < 60 then - return M.pluralize(time_diff, "second") .. " ago" + return "just now" elseif time_diff < 3600 then return M.pluralize(math.floor(time_diff / 60), "minute") .. " ago" elseif time_diff < 86400 then