From 921ee3f38985967b8654eaf4357089a634530e9a Mon Sep 17 00:00:00 2001 From: j-hui Date: Mon, 18 Dec 2023 22:13:03 -0500 Subject: [PATCH] feat(history): show notifications history in echo buffer Closes #149. --- lua/fidget/notification.lua | 13 ++++++++- lua/fidget/notification/model.lua | 2 ++ lua/fidget/notification/view.lua | 48 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lua/fidget/notification.lua b/lua/fidget/notification.lua index 9e2d4bc..aa57e79 100644 --- a/lua/fidget/notification.lua +++ b/lua/fidget/notification.lua @@ -79,7 +79,10 @@ local logger = require("fidget.logger") --- ---@class HistoryItem : Item ---@field removed boolean Whether this item is deleted ----@field data any|nil Arbitrary data attached to notification item +---@field group_key Key Key of the group this item belongs to +---@field group_name string|nil Title of the group this item belongs to +---@field group_icon string|nil Icon of the group this item belongs to +---@field last_updated number What time this item was last updated, in seconds since Jan 1, 1970 --- Filter options when querying for notifications history. --- @@ -369,4 +372,12 @@ function notification.get_history(filter) return notification.model.make_history(state, poll.get_time(), filter) end +--- Show the notifications history in the |nvim_echo()| buffer. +--- +---@param filter HistoryFilter|Key|nil options or group_key for filtering history +function notification.show_history(filter) + local history = notification.get_history(filter) + notification.view.echo_history(history) +end + return notification diff --git a/lua/fidget/notification/model.lua b/lua/fidget/notification/model.lua index 407b778..54578e0 100644 --- a/lua/fidget/notification/model.lua +++ b/lua/fidget/notification/model.lua @@ -13,6 +13,7 @@ --- be added to code documentation. local M = {} local logger = require("fidget.logger") +local poll = require("fidget.poll") --- The abstract state of the notifications subsystem. ---@class State @@ -98,6 +99,7 @@ end local function item_to_history(item, extra) ---@type HistoryItem item = vim.tbl_extend("force", item, extra) + item.last_updated = poll.unix_time(item.last_updated) return item end diff --git a/lua/fidget/notification/view.lua b/lua/fidget/notification/view.lua index 7aea846..2b330ae 100644 --- a/lua/fidget/notification/view.lua +++ b/lua/fidget/notification/view.lua @@ -253,4 +253,52 @@ function M.render(now, groups) } end +--- Display notification items in Neovim mesasges. +--- +--- TODO(j-hui): this is not very configurable, but I'm not sure what options to +--- expose to strike a balance between flexibility and simplicity. Then again, +--- nothing done here is "special"; the user can easily (and is encouraged to) +--- write a custom `echo_history()` by consuming the results of `get_history()`. +--- +---@param items HistoryItem[] +function M.echo_history(items) + for _, item in ipairs(items) do + local is_multiline_msg = string.find(item.message, "\n") ~= nil + + local chunks = {} + + table.insert(chunks, { vim.fn.strftime("%c", item.last_updated), "Comment" }) + + -- if item.group_icon and #item.group_icon > 0 then + -- table.insert(chunks, { " ", "MsgArea" }) + -- table.insert(chunks, { item.group_icon, "Special" }) + -- end + + if item.group_name and #item.group_name > 0 then + table.insert(chunks, { " ", "MsgArea" }) + table.insert(chunks, { item.group_name, "Special" }) + end + + table.insert(chunks, { " | ", "Comment" }) + + if item.annote and #item.annote > 0 then + table.insert(chunks, { item.annote, item.style }) + end + + if is_multiline_msg then + table.insert(chunks, { "\n", "MsgArea" }) + else + table.insert(chunks, { " ", "MsgArea" }) + end + + table.insert(chunks, { item.message, "MsgArea" }) + + if is_multiline_msg then + table.insert(chunks, { "\n", "MsgArea" }) + end + + vim.api.nvim_echo(chunks, false, {}) + end +end + return M