]> git.r.bdr.sh - rbdr/dotfiles/blame - config/nvim/lua/brain.lua
Add agenda
[rbdr/dotfiles] / config / nvim / lua / brain.lua
CommitLineData
7d35a7fc
RBR
1local brain_home = os.getenv('HOME') .. '/brain'
2local templates_path = brain_home .. '/1 periodic/99 templates'
3
4local function load_template(template_name)
5 local template_path = brain_home .. templates_path .. '/' .. template_name
6
66b09f6c 7 local template_file = io.open(template_path, 'r')
7d35a7fc 8 if not template_file then
66b09f6c 9 print('Template file not found')
7d35a7fc
RBR
10 return
11 end
66b09f6c 12 local content = template_file:read('*a')
7d35a7fc
RBR
13 template_file:close()
14end
15
16local function open_or_create_from_template(template_name, file)
66b09f6c 17 local journal_file = io.open(file, 'r')
7d35a7fc
RBR
18 if not journal_file then
19 local template_contents = load_template(template_name)
20
66b09f6c 21 journal_file = io.open(file, 'w')
7d35a7fc
RBR
22 journal_file:write(template_contents)
23 journal_file:close()
24 end
66b09f6c 25 vim.cmd('edit ' .. file)
7d35a7fc
RBR
26end
27
66b09f6c
RBR
28local function get_this_weeks_files()
29 local today = os.time()
30 local day_of_week = os.date('*t', today).wday
31 local week_start = today - (day_of_week - 2) * 86400
32 local filenames = {}
33
34 for i = 0, 6 do
35 local date = os.date('*t', week_start + i * 86400)
36 table.insert(filenames, string.format('%04d-%02d-%02d.md', date.year, date.month, date.day))
37 end
38
39 return filenames
40end
41
42-------------------------------------------------------------------------------
43-- Periodic Notes
44-------------------------------------------------------------------------------
45
7d35a7fc
RBR
46function open_daily_note()
47 local filename_format = os.date('%Y-%m-%d')
48 local template_name = '001 daily.md'
49
66b09f6c 50 local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename_format .. '.md'
7d35a7fc
RBR
51 open_or_create_from_template(template_name, daily_note)
52end
53
54function open_weekly_note()
55 local filename_format = os.date('%Y-w%V')
56 local template_name = '002 weekly.md'
57
66b09f6c 58 local weekly_note = brain_home .. '/1 periodic/02 weeks/' .. filename_format .. '.md'
7d35a7fc
RBR
59 open_or_create_from_template(template_name, weekly_note)
60end
61
66b09f6c
RBR
62-------------------------------------------------------------------------------
63-- Task Utilities
64-------------------------------------------------------------------------------
65
7d35a7fc
RBR
66function toggle_task()
67 local line_num = vim.api.nvim_win_get_cursor(0)[1]
68 local line = vim.api.nvim_get_current_line()
69
70 local unchecked_pattern = '^%s*%- %[ %]'
71 local checked_pattern = '^%s*%- %[x%]'
72
73 if line:match(unchecked_pattern) then
74 line = line:gsub(unchecked_pattern, '- [x]', 1)
75 elseif line:match(checked_pattern) then
76 line = line:gsub(checked_pattern, '- [ ]', 1)
77 end
78
79 vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, false, {line})
80end
81
66b09f6c
RBR
82-------------------------------------------------------------------------------
83-- Agenda View
84-------------------------------------------------------------------------------
85
86function open_agenda()
87 local uv = vim.loop
88 local week_filenames = get_this_weeks_files()
89 local items = {}
90 local pattern = '^%s*%- %[[ ]?x?%]'
91 print(week_filenames[1])
92
93 for _, filename in ipairs(week_filenames) do
94 local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename
95 local stat = uv.fs_stat(daily_note)
96
97 if stat then -- File exists
98 local file, err = io.open(daily_note, 'r')
99 if file then
100 local set_header = 0
101 local line_number = 0
102 for line in file:lines() do
103 line_number = line_number + 1
104 if line:match(pattern) then
105 if set_header == 0 then
106 local header = filename:match('([^/\\]+)$')
107 table.insert(items, {filename = '', lnum = 0, text = header})
108 set_header = 1
109 end
110 table.insert(items, {filename = daily_note, text = line, lnum = line_number})
111 end
112 end
113 file:close()
114 end
115 end
116 end
117 vim.fn.setloclist(0, {}, ' ', {title = 'Weekly Tasks', items = items})
118 vim.cmd('lopen')
119end
120
121-------------------------------------------------------------------------------
122-- Key Bindings
123-------------------------------------------------------------------------------
7d35a7fc
RBR
124vim.api.nvim_set_keymap('n', '<leader>od', '<cmd>lua open_daily_note()<CR>', { noremap = true, silent = true })
125vim.api.nvim_set_keymap('n', '<leader>ow', '<cmd>lua open_weekly_note()<CR>', { noremap = true, silent = true })
66b09f6c 126vim.api.nvim_set_keymap('n', '<leader>oa', '<cmd>lua open_agenda()<CR>', { noremap = true, silent = true })
7d35a7fc 127vim.api.nvim_set_keymap('n', '<leader>t', '<cmd>lua toggle_task()<CR>', { noremap = true, silent = true })