Skip to content

Commit

Permalink
Merge pull request #172 from neo451/dev
Browse files Browse the repository at this point in the history
feat(format): more customizable format functions
  • Loading branch information
neo451 authored Feb 25, 2025
2 parents aeab067 + 86ea61d commit 30f30ee
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 50 deletions.
127 changes: 125 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Press `?` in to get hints.
| hints | `?` |
| dot_repeat | `.` |
| undo | `u` |
| redo | `<C-r>` |
| entry | `<CR>` |
| split | `<M-CR>` |
| browser | `b` |
Expand Down Expand Up @@ -265,9 +266,127 @@ requires `rg` and one of the search backends:
- `fzf-lua`
- `mini.pick`

## Layout
## Recipes

:TODO:
<details><summary>Change the highlight of the tags section and use emojis and mini.icons for tags</summary>

```lua
require("feed").setup({
layout = {
tags = {
color = "String",
format = function(id, db)
local icons = {
news = "📰",
tech = "💻",
movies = "🎬",
games = "🎮",
music = "🎵",
podcast = "🎧",
books = "📚",
unread = "🆕",
read = "",
junk = "🚮",
star = "",
}

local get_icon = function(name)
if icons[name] then
return icons[name]
end
local has_mini, MiniIcons = pcall(require, "mini.icons")
if has_mini then
local icon = MiniIcons.get("filetype", name)
if icon then
return icon .. " "
end
end
return name
end

local tags = vim.tbl_map(get_icon, db:get_tags(id))

return "[" .. table.concat(tags, ", ") .. "]"
end,
},
},
})
```

</details>

<details><summary>Custom function & keymap for podcast and w3m</summary>

```lua
local function play_podcast()
local link = require("feed").get_entry().link
if link:find("mp3") then
vim.ui.open(link)
-- any other player like:
-- vim.system({ "vlc.exe", link })
else
vim.notify("not a podcast episode")
end
end

local function show_in_w3m()
if not vim.fn.executable("w3m") then
vim.notify("w3m not installed")
return
end
local link = require("feed").get_entry().link
local w3m = require("feed.ui.window").new({
relative = "editor",
col = math.floor(vim.o.columns * 0.1),
row = math.floor(vim.o.lines * 0.1),
width = math.floor(vim.o.columns * 0.8),
height = math.floor(vim.o.lines * 0.8),
border = "rounded",
style = "minimal",
title = "Feed w3m",
zindex = 10,
})
vim.keymap.set({ "n", "t" }, "q", "<cmd>q<cr>", { silent = true, buffer = w3m.buf })
vim.fn.jobstart({ "w3m", link }, { term = true })
vim.cmd("startinsert")
end

require("feed").setup({
keys = {
index = {
[play_podcast] = "p",
[show_in_w3m] = "w",
},
},
})
```

</details>

<details><summary>Custom colorscheme only set when viewing feeds</summary>

```lua
local og_color

vim.api.nvim_create_autocmd("User", {
pattern = "FeedShowIndex",
callback = function()
if not og_color then
og_color = vim.g.colors_name
end
vim.cmd.colorscheme("kanagawa-lotus")
end,
})

vim.api.nvim_create_autocmd("User", {
pattern = "FeedQuitIndex",
callback = function()
vim.cmd.colorscheme(og_color)
end,
})
```

</details>

## Lua API

Expand All @@ -286,3 +405,7 @@ requires `rg` and one of the search backends:
- [vnews](https://github.com/danchoi/vnews)
- [lua-feedparser](https://github.com/slact/lua-feedparser)
<!-- panvimdoc-ignore-start -->

```
```
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
16 changes: 16 additions & 0 deletions lua/feed/db/local.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ function M:untag(id, tag)
self:save_tags()
end

function M: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

function M:sort()
table.sort(self.index, function(a, b)
return a[2] > b[2]
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
44 changes: 6 additions & 38 deletions lua/feed/ui/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,14 @@ end
---@param db feed.db
---@return string
function M.tags(id, db)
db = db or require("feed.db")

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

return "[" .. ut.align(table.concat(acc, ", "), Config.layout.tags.width - 2) .. "]"
local tags = db:get_tags(id)
return "[" .. table.concat(tags, ", ") .. "]"
end

---@param id string
---@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 @@ -47,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 @@ -58,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 @@ -72,53 +54,39 @@ 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]
local text = entry[name] or ""
local f = v.format or M[name]
-- if M[name] then
text = f(id, db)
-- end
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
Loading

0 comments on commit 30f30ee

Please sign in to comment.