]> git.r.bdr.sh - rbdr/dotfiles/blob - config/nvim/lua/brain.lua
4dfd2314c204c8cc3a0ad742aef4bb8c7ff8ed4a
[rbdr/dotfiles] / config / nvim / lua / brain.lua
1 local brain_home = os.getenv('HOME') .. '/brain'
2 local templates_path = brain_home .. '/1 periodic/99 templates'
3
4 local function load_template(template_name)
5 local template_path = brain_home .. templates_path .. '/' .. template_name
6
7 local template_file = io.open(template_path, "r")
8 if not template_file then
9 print("Template file not found")
10 return
11 end
12 local content = template_file:read("*a")
13 template_file:close()
14 end
15
16 local function open_or_create_from_template(template_name, file)
17 local journal_file = io.open(file, "r")
18 if not journal_file then
19 local template_contents = load_template(template_name)
20
21 journal_file = io.open(file, "w")
22 journal_file:write(template_contents)
23 journal_file:close()
24 end
25 vim.cmd("edit " .. file)
26 end
27
28 function open_daily_note()
29 local filename_format = os.date('%Y-%m-%d')
30 local template_name = '001 daily.md'
31
32 local daily_note = brain_home .. "/1 periodic/01 journal/" .. filename_format .. ".md"
33 open_or_create_from_template(template_name, daily_note)
34 end
35
36 function open_weekly_note()
37 local filename_format = os.date('%Y-w%V')
38 local template_name = '002 weekly.md'
39
40 local weekly_note = brain_home .. "/1 periodic/02 weeks/" .. filename_format .. ".md"
41 open_or_create_from_template(template_name, weekly_note)
42 end
43
44 function toggle_task()
45 local line_num = vim.api.nvim_win_get_cursor(0)[1]
46 local line = vim.api.nvim_get_current_line()
47
48 local unchecked_pattern = '^%s*%- %[ %]'
49 local checked_pattern = '^%s*%- %[x%]'
50
51 if line:match(unchecked_pattern) then
52 line = line:gsub(unchecked_pattern, '- [x]', 1)
53 elseif line:match(checked_pattern) then
54 line = line:gsub(checked_pattern, '- [ ]', 1)
55 end
56
57 vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, false, {line})
58 end
59
60 -- Keybinds
61 vim.api.nvim_set_keymap('n', '<leader>od', '<cmd>lua open_daily_note()<CR>', { noremap = true, silent = true })
62 vim.api.nvim_set_keymap('n', '<leader>ow', '<cmd>lua open_weekly_note()<CR>', { noremap = true, silent = true })
63 vim.api.nvim_set_keymap('n', '<leader>t', '<cmd>lua toggle_task()<CR>', { noremap = true, silent = true })