Skip to content

Commit

Permalink
fix: seperate picker layout config
Browse files Browse the repository at this point in the history
  • Loading branch information
neo451 committed Feb 25, 2025
1 parent 757b1e6 commit 86ea61d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 30 deletions.
17 changes: 17 additions & 0 deletions lua/feed/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ local default = {
},
},

picker = {
order = { "feed", "tags", "title" },
feed = {
width = 15,
color = "FeedTitle",
},
tags = {
width = 15,
color = "FeedTags",
},
title = {
color = "FeedTitle",
},
},

-- TODO: layout for winbar

search = {
default_query = "@2-weeks-ago +unread ",
backend = {
Expand Down
2 changes: 1 addition & 1 deletion lua/feed/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ local function render_entry(buf, body, id)

local header = {}
for i, v in ipairs({ "title", "author", "feed", "link", "date" }) do
header[i] = ut.capticalize(v) .. ": " .. Format[v](id)
header[i] = ut.capticalize(v) .. ": " .. Format[v](id, db)
end

local urls = ut.get_urls(body, db[id].link)
Expand Down
24 changes: 4 additions & 20 deletions lua/feed/ui/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ end
---@param db feed.db
---@return string
function M.tags(id, db)
db = db or require("feed.db")
local tags = db:get_tags(id)
return "[" .. table.concat(tags, ", ") .. "]"
end
Expand All @@ -24,7 +23,6 @@ end
---@param db feed.db
---@return string
M.title = function(id, db)
db = db or require("feed.db")
local entry = db[id]
return cleanup(entry.title)
end
Expand All @@ -33,7 +31,6 @@ end
---@param db feed.db
---@return string
M.feed = function(id, db)
db = db or require("feed.db")
local feeds = db.feeds
local entry = db[id]
local feed = feeds[entry.feed] and feeds[entry.feed].title or entry.feed
Expand All @@ -44,7 +41,6 @@ end
---@param db feed.db
---@return string
M.author = function(id, db)
db = db or require("feed.db")
---@type feed.entry
local entry = db[id]
if entry.author then
Expand All @@ -58,41 +54,29 @@ end
---@param db feed.db
---@return string
M.link = function(id, db)
db = db or require("feed.db")
return db[id].link
end

---@param id string
---@param db feed.db
---@return string
M.date = function(id, db)
db = db or require("feed.db")
---@diagnostic disable-next-line: return-type-mismatch
return os.date(Config.date_format.short, db[id].time)
end

---return a formated line for an entry base on user config
---@param id string
---@param comps table
---@param layout table
---@param db feed.db
---@return string
M.entry = function(id, comps, db)
db = db or require("feed.db")
M.entry = function(id, layout, db)
local entry = db[id]
if not entry then
return ""
end

-- comps = comps
-- or {
-- { "feed", width = 20 },
-- { "tags", width = 20 },
-- { "title", width = math.huge },
-- }
local acc = 0
local res = {}

local layout = Config.layout
local c, res = 0, {}

for _, name in ipairs(layout.order) do
local v = layout[name]
Expand All @@ -102,7 +86,7 @@ M.entry = function(id, comps, db)
local width = v.width or #text
text = ut.align(text, width, v.right_justify) .. " "
res[#res + 1] = text
acc = acc + width
c = c + width
end
return table.concat(res)
end
Expand Down
3 changes: 1 addition & 2 deletions lua/feed/ui/fzf-lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local function feed_search()
ui.state.entries = db:filter(str)
local ret = {}
for i, id in ipairs(ui.state.entries) do
ret[i] = Format.entry(id) .. (" "):rep(100) .. id
ret[i] = Format.entry(id, Config.picker, db) .. (" "):rep(100) .. id
end
return ret
end, {
Expand Down Expand Up @@ -64,7 +64,6 @@ local function feed_grep(opts)
opts.previewer = "builtin"
opts.fn_transform = function(x)
local id = x:sub(10, 10 + 63)
-- return Format.entry(id)
return fzf_lua.make_entry.file(x, opts)
end
opts.cwd = tostring(db.dir / "data")
Expand Down
4 changes: 2 additions & 2 deletions lua/feed/ui/pick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local function feed_search()
local show = function(buf_id, items_arr, _)
local lines = vim.iter(items_arr)
:map(function(id)
return format.entry(id, nil, db)
return format.entry(id, Config.picker, db)
end)
:totable()
vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, lines)
Expand Down Expand Up @@ -66,7 +66,7 @@ local function feed_grep()
show = function(buf_id, items_arr, _)
for i, line in ipairs(items_arr) do
local id = line:sub(1, 64)
vim.api.nvim_buf_set_lines(buf_id, i - 1, i, false, { format.entry(id, nil, db) })
vim.api.nvim_buf_set_lines(buf_id, i - 1, i, false, { format.entry(id, Config.picker, db) })
end
end,
},
Expand Down
4 changes: 2 additions & 2 deletions lua/feed/ui/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ local function feed_search()
entry_maker = function(line)
return {
value = line,
text = format.entry(line),
text = format.entry(line, config.picker, db),
filename = tostring(db.dir / "data" / line),
display = function(entry)
return format.entry(entry.value)
return format.entry(entry.value, config.picker, db)
end,
ordinal = line,
}
Expand Down
26 changes: 23 additions & 3 deletions tests/test_format.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
local M = require("feed.ui.format")
local eq = MiniTest.expect.equality
local db = require("feed.db")

local T = MiniTest.new_set()
local layout = require("feed.config").layout

local db = {
["1"] = {
Expand All @@ -24,11 +26,29 @@ local db = {
},
}

db.__index = db

function db:get_tags(id)
local ret = {}
-- 1. auto tag no [read] as [unread]
if not (self.tags.read and self.tags.read[id]) then
ret = { "unread" }
end

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

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

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

Expand All @@ -49,8 +69,8 @@ end

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

return T

0 comments on commit 86ea61d

Please sign in to comment.