From: Ruben Beltran del Rio Date: Thu, 29 Feb 2024 22:49:36 +0000 (+0100) Subject: Add agenda X-Git-Url: https://git.r.bdr.sh/rbdr/dotfiles/commitdiff_plain/66b09f6c9388eedff178645956c97aaeae8d3b27?ds=inline;hp=7d35a7fcdeb0fc7e47dcd401b4c91cec9e998799 Add agenda --- diff --git a/config/nvim/lua/brain.lua b/config/nvim/lua/brain.lua index 4dfd231..23556c6 100644 --- a/config/nvim/lua/brain.lua +++ b/config/nvim/lua/brain.lua @@ -4,32 +4,50 @@ local templates_path = brain_home .. '/1 periodic/99 templates' local function load_template(template_name) local template_path = brain_home .. templates_path .. '/' .. template_name - local template_file = io.open(template_path, "r") + local template_file = io.open(template_path, 'r') if not template_file then - print("Template file not found") + print('Template file not found') return end - local content = template_file:read("*a") + local content = template_file:read('*a') template_file:close() end local function open_or_create_from_template(template_name, file) - local journal_file = io.open(file, "r") + local journal_file = io.open(file, 'r') if not journal_file then local template_contents = load_template(template_name) - journal_file = io.open(file, "w") + journal_file = io.open(file, 'w') journal_file:write(template_contents) journal_file:close() end - vim.cmd("edit " .. file) + vim.cmd('edit ' .. file) end +local function get_this_weeks_files() + local today = os.time() + local day_of_week = os.date('*t', today).wday + local week_start = today - (day_of_week - 2) * 86400 + local filenames = {} + + for i = 0, 6 do + local date = os.date('*t', week_start + i * 86400) + table.insert(filenames, string.format('%04d-%02d-%02d.md', date.year, date.month, date.day)) + end + + return filenames +end + +------------------------------------------------------------------------------- +-- Periodic Notes +------------------------------------------------------------------------------- + function open_daily_note() local filename_format = os.date('%Y-%m-%d') local template_name = '001 daily.md' - local daily_note = brain_home .. "/1 periodic/01 journal/" .. filename_format .. ".md" + local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename_format .. '.md' open_or_create_from_template(template_name, daily_note) end @@ -37,10 +55,14 @@ function open_weekly_note() local filename_format = os.date('%Y-w%V') local template_name = '002 weekly.md' - local weekly_note = brain_home .. "/1 periodic/02 weeks/" .. filename_format .. ".md" + local weekly_note = brain_home .. '/1 periodic/02 weeks/' .. filename_format .. '.md' open_or_create_from_template(template_name, weekly_note) end +------------------------------------------------------------------------------- +-- Task Utilities +------------------------------------------------------------------------------- + function toggle_task() local line_num = vim.api.nvim_win_get_cursor(0)[1] local line = vim.api.nvim_get_current_line() @@ -57,7 +79,49 @@ function toggle_task() vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, false, {line}) end --- Keybinds +------------------------------------------------------------------------------- +-- Agenda View +------------------------------------------------------------------------------- + +function open_agenda() + local uv = vim.loop + local week_filenames = get_this_weeks_files() + local items = {} + local pattern = '^%s*%- %[[ ]?x?%]' + print(week_filenames[1]) + + for _, filename in ipairs(week_filenames) do + local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename + local stat = uv.fs_stat(daily_note) + + if stat then -- File exists + local file, err = io.open(daily_note, 'r') + if file then + local set_header = 0 + local line_number = 0 + for line in file:lines() do + line_number = line_number + 1 + if line:match(pattern) then + if set_header == 0 then + local header = filename:match('([^/\\]+)$') + table.insert(items, {filename = '', lnum = 0, text = header}) + set_header = 1 + end + table.insert(items, {filename = daily_note, text = line, lnum = line_number}) + end + end + file:close() + end + end + end + vim.fn.setloclist(0, {}, ' ', {title = 'Weekly Tasks', items = items}) + vim.cmd('lopen') +end + +------------------------------------------------------------------------------- +-- Key Bindings +------------------------------------------------------------------------------- vim.api.nvim_set_keymap('n', 'od', 'lua open_daily_note()', { noremap = true, silent = true }) vim.api.nvim_set_keymap('n', 'ow', 'lua open_weekly_note()', { noremap = true, silent = true }) +vim.api.nvim_set_keymap('n', 'oa', 'lua open_agenda()', { noremap = true, silent = true }) vim.api.nvim_set_keymap('n', 't', 'lua toggle_task()', { noremap = true, silent = true })