Skip to content

Commit

Permalink
feat(ui): simplify UI, move non-essential to recipes
Browse files Browse the repository at this point in the history
feat(zenmode): proper backdrop
fix(ui): auto-calc window heght
  • Loading branch information
neo451 committed Feb 22, 2025
1 parent 4810f53 commit 1cdcf37
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 134 deletions.
4 changes: 0 additions & 4 deletions lua/feed/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---@class feed.config
---@field feeds? string | { name: string, tags: table }
---@field colorscheme? string
---@field db_dir? string
---@field date_format? string
---@field curl_params? string[]
Expand All @@ -15,8 +14,6 @@
local default = {
---@type string
db_dir = vim.fn.stdpath("data") .. "/feed",
---@type string
colorscheme = nil,
---@type { long: string, short: string }
date_format = {
short = "%Y-%m-%d",
Expand Down Expand Up @@ -74,7 +71,6 @@ local default = {
progress = {
backend = {
"fidget",
"nvim-notify",
"mini.notify",
"snacks",
"native",
Expand Down
4 changes: 2 additions & 2 deletions lua/feed/db/local.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local uv = vim.uv
---@field untag fun(db: feed.db, id: string, tag: string | string[])
---@field blowup fun(db: feed.db)
---@field update fun(db: feed.db)
---@field lastUpdated fun(db: feed.db): string
---@field last_updated fun(db: feed.db): string
local M = {}

---@param fp feed.path
Expand Down Expand Up @@ -128,7 +128,7 @@ function M:update()
rawset(self, "tags", tags)
end

function M:lastUpdated()
function M:last_updated()
return os.date("%c", vim.fn.getftime(tostring(self.dir / "feeds.lua")))
end

Expand Down
96 changes: 42 additions & 54 deletions lua/feed/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,9 @@ local ns = api.nvim_create_namespace("feed_index")
local ns_read = api.nvim_create_namespace("feed_index_read")
local ns_entry = api.nvim_create_namespace("feed_entry")

local og = {}

---@param colorscheme string
local function set_colorscheme(colorscheme)
if Config.colorscheme and vim.g.colors_name ~= colorscheme then
pcall(vim.cmd.colorscheme, colorscheme)
end
end

local function save_og()
og.colorscheme = vim.g.colors_name
og.cmdheight = vim.o.cmdheight
end
local function set_color_n_height()
vim.o.cmdheight = 0
set_colorscheme(Config.colorscheme)
end
local function restore_color_n_height()
vim.o.cmdheight = og.cmdheight
set_colorscheme(og.colorscheme)
end

---get entry base on current context, and update current_index
---@return feed.entry
---@return string
---@return feed.entry?
---@return string?
local function get_entry(ctx)
ctx = ctx or {}
local id
Expand Down Expand Up @@ -94,12 +72,11 @@ end
---@param buf number
local function image_attach(buf)
if not Snacks then
vim.notify("Snacks is not available")
return
end
pcall(function()
local ok, f = pcall(Snacks.image.doc.inline, buf)
return ok and f and f()
end)
local ok, f = pcall(Snacks.image.doc.inline, buf)
return ok and f and pcall(f)
end

local body_transforms = {
Expand Down Expand Up @@ -158,6 +135,12 @@ local function render_entry(buf, body, id)

image_attach(buf)
mark_read(id)

api.nvim_buf_set_name(buf, "FeedEntry")

api.nvim_exec_autocmds("User", {
pattern = "FeedShowEntry",
})
end

local function hl_index(buf)
Expand All @@ -175,15 +158,16 @@ local function hl_index(buf)
end

M.show_index = function()
save_og()
if not state.index or not state.index:valid() then
local cursor_pos, scroll_pos
if state.index then
cursor_pos = api.nvim_win_get_cursor(state.index.win)
scroll_pos = fn.line("w0")
else
state.index = Win.new({
wo = Config.options.index.wo,
bo = Config.options.index.bo,
keys = Config.keys.index,
zindex = 3,
on_open = set_color_n_height,
on_leave = restore_color_n_height,
zindex = 8,
})
end

Expand All @@ -202,6 +186,15 @@ M.show_index = function()
api.nvim_buf_set_lines(buf, 0, -1, false, lines)
hl_index(buf)
vim.bo[buf].modifiable = false

if cursor_pos and scroll_pos then
pcall(api.nvim_win_set_cursor, win, cursor_pos)
fn.winrestview({ topline = scroll_pos })
end

api.nvim_exec_autocmds("User", {
pattern = "FeedShowIndex",
})
end

---@param ctx? { row: integer, id: string, buf: integer, link: string, read: boolean }
Expand Down Expand Up @@ -242,24 +235,15 @@ local function show_entry(ctx)
buf = api.nvim_create_buf(false, true)
end

Config.options.entry.wo.winbar = M.show_keyhints()

if vim.tbl_isempty(og) then
save_og()
end

state.entry = Win.new({
prev_win = (state.index and state.index:valid()) and state.index.win or nil,
prev_win = state.index and state.index.win or nil,
buf = buf,
wo = Config.options.entry.wo,
bo = Config.options.entry.bo,
keys = Config.keys.entry,
ft = "markdown",
on_open = set_color_n_height,
on_leave = restore_color_n_height,
zen = Config.zen.enabled,
zindex = 8,
backdrop = state.index and state.index or { win = api.nvim_get_current_win() },
zindex = 10,
})

if ctx.link then
Expand All @@ -284,21 +268,21 @@ local function show_entry(ctx)
end,
})
end

local win = state.entry.win

api.nvim_buf_set_name(buf, "FeedEntry")
-- TODO: restore cursor and scroll pos
api.nvim_win_set_cursor(win, { 1, 0 })
end

M.quit = function()
if ut.in_index() then
state.index:close()
state.index = nil
vim.api.nvim_exec_autocmds("User", {
pattern = "FeedQuitIndex",
})
elseif ut.in_entry() then
state.entry:close()
state.entry = nil
vim.api.nvim_exec_autocmds("User", {
pattern = "FeedQuitEntry",
})
end
end

Expand All @@ -316,15 +300,15 @@ end
M.show_prev = function()
if state.cur > 1 then
api.nvim_exec_autocmds("ExitPre", { buffer = state.entry.buf })
show_entry({ row = state.cur - 1, buf = state.entry.buf })
M.preview_entry({ row = state.cur - 1, buf = state.entry.buf })
api.nvim_exec_autocmds("BufWritePost", {})
end
end

M.show_next = function()
if state.cur < #state.entries then
api.nvim_exec_autocmds("ExitPre", { buffer = state.entry.buf })
show_entry({ row = state.cur + 1, buf = state.entry.buf })
M.preview_entry({ row = state.cur + 1, buf = state.entry.buf })
api.nvim_exec_autocmds("BufWritePost", {})
end
end
Expand Down Expand Up @@ -388,7 +372,9 @@ M.show_hints = function()

local lines = {}
for k, v in pairs(maps) do
lines[#lines + 1] = v .. " -> " .. k
if type(k) == "string" then
lines[#lines + 1] = v .. " -> " .. k
end
end

M.split({
Expand Down Expand Up @@ -490,7 +476,9 @@ M.export_opml = function(fp)
end

--- FIXME
M.dot = function() end
M.dot = function()
vim.notify("No operation defined for dot")
end

M.undo = function()
local act = table.remove(undo_history, #undo_history)
Expand Down
46 changes: 13 additions & 33 deletions lua/feed/ui/bar.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ut = require("feed.utils")
local DB = require("feed.db")
local Config = require("feed.config")
local db = require("feed.db")
local config = require("feed.config")
local state = require("feed.ui.state")

local M = {}
Expand All @@ -10,45 +10,35 @@ function _G._feed_bar_component(name)
return cmp[name]()
end

local name2width, name2hl = {}, {}

for name, v in pairs(Config.layout) do
name2width[name] = v.width
end

for name, v in pairs(Config.layout) do
name2hl[name] = v.color
end

local layout = config.layout
local hi_pattern = "%%#%s#%s%%*"

setmetatable(cmp, {
__index = function(_, k)
return function()
local width = name2width[k] or #k
local color = name2hl[k]
local width = layout[k].width or #k
local color = layout[k].color
return hi_pattern:format(color, ut.align(ut.capticalize(k), width))
end
end,
})

cmp.query = function()
return hi_pattern:format(name2hl["query"], vim.trim(state.query))
return hi_pattern:format(layout["query"].color, vim.trim(state.query))
end

cmp.last_updated = function()
return hi_pattern:format(name2hl["last_updated"], DB:lastUpdated())
end

cmp.progress = function()
local count = vim.api.nvim_buf_line_count(0) - 1
local cur = math.min(count, vim.api.nvim_win_get_cursor(0)[1])
return hi_pattern:format("FeedRead", ("[%d/%d]"):format(cur, count))
return hi_pattern:format(layout["last_updated"].color, db:last_updated())
end

-- cmp.progress = function()
-- local count = vim.api.nvim_buf_line_count(0) - 1
-- local cur = math.min(count, vim.api.nvim_win_get_cursor(0)[1])
-- return hi_pattern:format("FeedRead", ("[%d/%d]"):format(cur, count))
-- end
--
---@return string
function M.show_winbar()
local layout = Config.layout
local buf = {}

for _, name in ipairs(layout.order) do
Expand All @@ -64,14 +54,4 @@ function M.show_winbar()
return table.concat(buf, " ")
end

---@return string
function M.show_keyhints()
local buf = {}
for rhs, lhs in vim.spairs(Config.keys.entry) do
buf[#buf + 1] = ("%s:%s"):format(lhs, ut.capticalize(rhs))
end

return (Config.layout.padding.enabled and " " or "") .. "%#FeedRead#" .. table.concat(buf, " ") .. "%<"
end

return M
Loading

0 comments on commit 1cdcf37

Please sign in to comment.