feat: statusline API, counts, and PendingStatusChanged event (#40)
Problem: no way to know about overdue or due-today tasks without opening :Pending. No ambient awareness for statusline plugins. Solution: add counts(), statusline(), and has_due() public API functions backed by a module-local cache that recomputes after every store.save() and store.load(). Fire a User PendingStatusChanged event on every recompute. Extract is_overdue() and is_today() from duplicate locals into parse.lua as public functions. Refactor views.lua and init.lua to use the shared date logic. Add vimdoc API section and integration recipes for lualine, heirline, manual statusline, startup notification, and event-driven refresh.
This commit is contained in:
parent
302bf8126f
commit
e62e09f609
6 changed files with 507 additions and 69 deletions
|
|
@ -3,11 +3,97 @@ local diff = require('pending.diff')
|
|||
local parse = require('pending.parse')
|
||||
local store = require('pending.store')
|
||||
|
||||
---@class pending.Counts
|
||||
---@field overdue integer
|
||||
---@field today integer
|
||||
---@field pending integer
|
||||
---@field priority integer
|
||||
---@field next_due? string
|
||||
|
||||
---@class pending.init
|
||||
local M = {}
|
||||
|
||||
local UNDO_MAX = 20
|
||||
|
||||
---@type pending.Counts?
|
||||
local _counts = nil
|
||||
|
||||
---@return nil
|
||||
function M._recompute_counts()
|
||||
local cfg = require('pending.config').get()
|
||||
local someday = cfg.someday_date
|
||||
local overdue = 0
|
||||
local today = 0
|
||||
local pending = 0
|
||||
local priority = 0
|
||||
local next_due = nil ---@type string?
|
||||
local today_str = os.date('%Y-%m-%d') --[[@as string]]
|
||||
|
||||
for _, task in ipairs(store.active_tasks()) do
|
||||
if task.status == 'pending' then
|
||||
pending = pending + 1
|
||||
if task.priority > 0 then
|
||||
priority = priority + 1
|
||||
end
|
||||
if task.due and task.due ~= someday then
|
||||
if parse.is_overdue(task.due) then
|
||||
overdue = overdue + 1
|
||||
elseif parse.is_today(task.due) then
|
||||
today = today + 1
|
||||
end
|
||||
local date_part = task.due:match('^(%d%d%d%d%-%d%d%-%d%d)') or task.due
|
||||
if date_part >= today_str and (not next_due or task.due < next_due) then
|
||||
next_due = task.due
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
_counts = {
|
||||
overdue = overdue,
|
||||
today = today,
|
||||
pending = pending,
|
||||
priority = priority,
|
||||
next_due = next_due,
|
||||
}
|
||||
|
||||
vim.api.nvim_exec_autocmds('User', { pattern = 'PendingStatusChanged' })
|
||||
end
|
||||
|
||||
---@return nil
|
||||
local function _save_and_notify()
|
||||
store.save()
|
||||
M._recompute_counts()
|
||||
end
|
||||
|
||||
---@return pending.Counts
|
||||
function M.counts()
|
||||
if not _counts then
|
||||
store.load()
|
||||
M._recompute_counts()
|
||||
end
|
||||
return _counts --[[@as pending.Counts]]
|
||||
end
|
||||
|
||||
---@return string
|
||||
function M.statusline()
|
||||
local c = M.counts()
|
||||
if c.overdue > 0 and c.today > 0 then
|
||||
return c.overdue .. ' overdue, ' .. c.today .. ' today'
|
||||
elseif c.overdue > 0 then
|
||||
return c.overdue .. ' overdue'
|
||||
elseif c.today > 0 then
|
||||
return c.today .. ' today'
|
||||
end
|
||||
return ''
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function M.has_due()
|
||||
local c = M.counts()
|
||||
return c.overdue > 0 or c.today > 0
|
||||
end
|
||||
|
||||
---@return integer bufnr
|
||||
function M.open()
|
||||
local bufnr = buffer.open()
|
||||
|
|
@ -167,6 +253,7 @@ function M._on_write(bufnr)
|
|||
table.remove(stack, 1)
|
||||
end
|
||||
diff.apply(lines)
|
||||
M._recompute_counts()
|
||||
buffer.render(bufnr)
|
||||
end
|
||||
|
||||
|
|
@ -179,7 +266,7 @@ function M.undo_write()
|
|||
end
|
||||
local state = table.remove(stack)
|
||||
store.replace_tasks(state)
|
||||
store.save()
|
||||
_save_and_notify()
|
||||
buffer.render(buffer.bufnr())
|
||||
end
|
||||
|
||||
|
|
@ -220,7 +307,7 @@ function M.toggle_complete()
|
|||
end
|
||||
store.update(id, { status = 'done' })
|
||||
end
|
||||
store.save()
|
||||
_save_and_notify()
|
||||
buffer.render(bufnr)
|
||||
for lnum, m in ipairs(buffer.meta()) do
|
||||
if m.id == id then
|
||||
|
|
@ -251,7 +338,7 @@ function M.toggle_priority()
|
|||
end
|
||||
local new_priority = task.priority > 0 and 0 or 1
|
||||
store.update(id, { priority = new_priority })
|
||||
store.save()
|
||||
_save_and_notify()
|
||||
buffer.render(bufnr)
|
||||
for lnum, m in ipairs(buffer.meta()) do
|
||||
if m.id == id then
|
||||
|
|
@ -294,7 +381,7 @@ function M.prompt_date()
|
|||
end
|
||||
end
|
||||
store.update(id, { due = due })
|
||||
store.save()
|
||||
_save_and_notify()
|
||||
buffer.render(bufnr)
|
||||
end)
|
||||
end
|
||||
|
|
@ -319,7 +406,7 @@ function M.add(text)
|
|||
recur = metadata.rec,
|
||||
recur_mode = metadata.rec_mode,
|
||||
})
|
||||
store.save()
|
||||
_save_and_notify()
|
||||
local bufnr = buffer.bufnr()
|
||||
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
|
||||
buffer.render(bufnr)
|
||||
|
|
@ -367,7 +454,7 @@ function M.archive(days)
|
|||
::skip::
|
||||
end
|
||||
store.replace_tasks(kept)
|
||||
store.save()
|
||||
_save_and_notify()
|
||||
vim.notify('Archived ' .. archived .. ' tasks.')
|
||||
local bufnr = buffer.bufnr()
|
||||
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
|
||||
|
|
@ -375,44 +462,6 @@ function M.archive(days)
|
|||
end
|
||||
end
|
||||
|
||||
---@param due string
|
||||
---@return boolean
|
||||
local function is_due_or_overdue(due)
|
||||
local now = os.date('*t') --[[@as osdate]]
|
||||
local today = os.date('%Y-%m-%d') --[[@as string]]
|
||||
local date_part, time_part = due:match('^(.+)T(.+)$')
|
||||
if not date_part then
|
||||
return due <= today
|
||||
end
|
||||
if date_part < today then
|
||||
return true
|
||||
end
|
||||
if date_part > today then
|
||||
return false
|
||||
end
|
||||
local current_time = string.format('%02d:%02d', now.hour, now.min)
|
||||
return time_part <= current_time
|
||||
end
|
||||
|
||||
---@param due string
|
||||
---@return boolean
|
||||
local function is_overdue(due)
|
||||
local now = os.date('*t') --[[@as osdate]]
|
||||
local today = os.date('%Y-%m-%d') --[[@as string]]
|
||||
local date_part, time_part = due:match('^(.+)T(.+)$')
|
||||
if not date_part then
|
||||
return due < today
|
||||
end
|
||||
if date_part < today then
|
||||
return true
|
||||
end
|
||||
if date_part > today then
|
||||
return false
|
||||
end
|
||||
local current_time = string.format('%02d:%02d', now.hour, now.min)
|
||||
return time_part < current_time
|
||||
end
|
||||
|
||||
---@return nil
|
||||
function M.due()
|
||||
local bufnr = buffer.bufnr()
|
||||
|
|
@ -422,9 +471,14 @@ function M.due()
|
|||
|
||||
if meta and bufnr then
|
||||
for lnum, m in ipairs(meta) do
|
||||
if m.type == 'task' and m.raw_due and m.status ~= 'done' and is_due_or_overdue(m.raw_due) then
|
||||
if
|
||||
m.type == 'task'
|
||||
and m.raw_due
|
||||
and m.status ~= 'done'
|
||||
and (parse.is_overdue(m.raw_due) or parse.is_today(m.raw_due))
|
||||
then
|
||||
local task = store.get(m.id or 0)
|
||||
local label = is_overdue(m.raw_due) and '[OVERDUE] ' or '[DUE] '
|
||||
local label = parse.is_overdue(m.raw_due) and '[OVERDUE] ' or '[DUE] '
|
||||
table.insert(qf_items, {
|
||||
bufnr = bufnr,
|
||||
lnum = lnum,
|
||||
|
|
@ -436,8 +490,12 @@ function M.due()
|
|||
else
|
||||
store.load()
|
||||
for _, task in ipairs(store.active_tasks()) do
|
||||
if task.status == 'pending' and task.due and is_due_or_overdue(task.due) then
|
||||
local label = is_overdue(task.due) and '[OVERDUE] ' or '[DUE] '
|
||||
if
|
||||
task.status == 'pending'
|
||||
and task.due
|
||||
and (parse.is_overdue(task.due) or parse.is_today(task.due))
|
||||
then
|
||||
local label = parse.is_overdue(task.due) and '[OVERDUE] ' or '[DUE] '
|
||||
local text = label .. task.description
|
||||
if task.category then
|
||||
text = text .. ' [' .. task.category .. ']'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue