Skip to content

Commit

Permalink
feat: entries inherit tags from feed
Browse files Browse the repository at this point in the history
  • Loading branch information
neo451 committed Feb 20, 2025
1 parent 293b022 commit d84d31b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
10 changes: 9 additions & 1 deletion lua/feed/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,17 @@ function M._sync_feedlist()
feeds[url] = {}
elseif type(feeds[url]) == "table" then
feeds[url].title = title or feeds[url].title
feeds[url].tags = tags or feeds[url].tags
if tags and (tags ~= feeds[url].tags) then
for id, entry in db:iter() do
if entry.feed == url then
db:tag(id, tags)
end
end
feeds[url].tags = tags or feeds[url].tags
end
end
end
db:save_feeds()
end

return M
2 changes: 1 addition & 1 deletion lua/feed/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ local default = {
},
{
"tags",
width = 15,
width = 25,
color = "FeedTags",
},
{
Expand Down
11 changes: 6 additions & 5 deletions lua/feed/db/local.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,12 @@ function M:filter(str)
local iter

if q.must_have then
local must_have = q.must_have[1]
local ids = {}
for k, t in pairs(self.tags) do
if must_have == k then
vim.list_extend(ids, vim.tbl_keys(t))
for _, must in ipairs(q.must_have) do
for k, t in pairs(self.tags) do
if must == k then
vim.list_extend(ids, vim.tbl_keys(t))
end
end
end
iter = vim.iter(ids):map(function(id)
Expand Down Expand Up @@ -283,7 +284,7 @@ function M:filter(str)
return false
end
for _, reg in ipairs(q.re) do
if not reg:match_str(entry.title) or not reg:match(entry.link) then
if not reg:match_str(entry.title) or not reg:match_str(entry.link) then
return false
end
end
Expand Down
31 changes: 14 additions & 17 deletions lua/feed/ui/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ end
---@return string
function M.tags(id, db)
db = db or require("feed.db")
local taglist = vim.iter(db.tags):fold({}, function(acc, tag, v)
if type(v) == "table" and v[id] then
if icons.enabled then
acc[#acc + 1] = icons[tag] or tag
else
acc[#acc + 1] = tag
end
end
return acc
end)
if vim.tbl_isempty(taglist) then
if icons.enabled then
taglist = { icons.unread }
else
taglist = { "unread" }

local acc = {}

-- 1. auto tag no [read] as [unread]
if not (db.tags.read and db.tags.read[id]) then
acc = { "unread" }
end

-- 2. get tags from tags.lua
for tag, tagees in pairs(db.tags) do
if tagees[id] then
acc[#acc + 1] = tag
end
end

Expand All @@ -45,7 +42,7 @@ function M.tags(id, db)
end
end

return "[" .. ut.truncate(table.concat(taglist, ", "), tags_len) .. "]"
return "[" .. ut.truncate(table.concat(acc, ", "), tags_len) .. "]"
end

---@param id string
Expand Down Expand Up @@ -116,7 +113,7 @@ M.entry = function(id, comps, db)
comps = comps
or {
{ "feed", width = 20 },
{ "tags", width = 15 },
{ "tags", width = 20 },
{ "title", width = math.huge },
}
local acc = 0
Expand Down
11 changes: 8 additions & 3 deletions tests/test_format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ local db = {
feeds = {
["https://neovim.io/news.xml"] = {
title = "neovim",
tags = { "nvim" },
},
},
tags = {
star = {
["1"] = true,
},
},
tags = {},
}

T["format"] = MiniTest.new_set({})

T.format["tags"] = function()
local id = "1"
eq("[unread]", M.tags(id, db))
eq("[unread, star]", M.tags(id, db))
end

T.format["title"] = function()
Expand All @@ -43,7 +48,7 @@ end

T.format["entry"] = function()
local id = "1"
local expect = "neovim [unread] title "
local expect = "neovim [unread, star] title "
eq(expect, M.entry(id, nil, db))
end

Expand Down

0 comments on commit d84d31b

Please sign in to comment.