Skip to content

Commit

Permalink
feat(history): show notifications history in echo buffer
Browse files Browse the repository at this point in the history
Closes #149.
  • Loading branch information
j-hui committed Dec 19, 2023
1 parent 9a8c672 commit 921ee3f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lua/fidget/notification.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
---
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions lua/fidget/notification/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions lua/fidget/notification/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 921ee3f

Please sign in to comment.