diff options
Diffstat (limited to 'lib/data.lua')
| -rw-r--r-- | lib/data.lua | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/lib/data.lua b/lib/data.lua index dca039a..75dedcd 100644 --- a/lib/data.lua +++ b/lib/data.lua @@ -36,7 +36,8 @@ end -- The calendar cache written by bin/calendar-sample.sh. -- -- Returns { today = { y, m, d }, colors = { <calendar> = <khal colour name> }, --- events = { { y, m, d, start, finish, calendar, title }, ... } }, or nil when +-- events = { { y, m, d, start, finish, calendar, title }, ... }, +-- tasks = { { y, m, d, start, title, task = true }, ... } }, or nil when -- the cache is absent or carries no 'generated' key. nil means "the sampler -- never ran", which the card names; an empty events list means "nothing in the -- next week", which is a different and equally valid state. @@ -50,7 +51,7 @@ end function M.calendar_parse(text) if type(text) ~= 'string' then return nil end - local out = { colors = {}, events = {} } + local out = { colors = {}, events = {}, tasks = {} } local generated = false for line in text:gmatch('[^\n]+') do @@ -77,6 +78,16 @@ function M.calendar_parse(text) title = title, } end + elseif kind == 'task' then + -- Written by the sampler from the task vdir, so the date is ISO: no + -- positional guessing as for khal's events. + local y, m, d, st, title = rest:match('^(%d+)-(%d+)-(%d+)|([^|]*)|(.*)$') + if y then + out.tasks[#out.tasks + 1] = { + y = tonumber(y), m = tonumber(m), d = tonumber(d), + start = st ~= '' and st or nil, title = title, task = true, + } + end end end @@ -146,6 +157,25 @@ function M.calendar_upcoming(events, today, days) return out end +-- The card's table: the week's events and the open tasks due in it, as one +-- list by date. Overdue tasks pass calendar_upcoming's window (a negative day +-- offset is under any limit), so they lead the list, which is where they +-- belong. Within a day an all-day row ('' start) sorts before a timed one. +function M.calendar_agenda(cal, today, days) + local rows = M.calendar_upcoming(cal.events, today, days) + for _, t in ipairs(M.calendar_upcoming(cal.tasks, today, days)) do + rows[#rows + 1] = t + end + -- The index breaks ties, since table.sort is not stable. + local key = {} + for i, e in ipairs(rows) do + key[e] = string.format('%04d%02d%02d %-5s %04d', e.y, e.m, e.d, + e.start or '', i) + end + table.sort(rows, function(a, b) return key[a] < key[b] end) + return rows +end + -- Days in a month, and the weekday its 1st falls on. -- -- os.time/os.date rather than a leap-year rule: the C library already knows, |
