]> git.r.bdr.sh - rbdr/nota.nvim/blame - lua/notes.lua
Add rescheduling tasks
[rbdr/nota.nvim] / lua / notes.lua
CommitLineData
56292c79
RBR
1-------------------------------------------------------------------------------
2-- Tools to deal with notes
3-------------------------------------------------------------------------------
503d09fc
RBR
4local Notes = {}
5
6local Util = require('util')
7local Configuration = require('configuration')
8local api = vim.api
9-------------------------------------------------------------------------------
10-- Internal Functions
11-------------------------------------------------------------------------------
12local function open_or_create_from_template(type, file_path)
13
14 local journal_file = io.open(file_path, 'r')
15 if not journal_file then
16 local template_contents = Configuration.load_template(type)
17
18 journal_file = io.open(file_path, 'w')
19 journal_file:write(template_contents)
20 journal_file:close()
21 end
22 vim.cmd('edit ' .. file_path)
23end
24
25local function open_periodic_note(type, filename)
26 local file_directory_path = Configuration.path_for(Configuration.configuration.periodic_locations[type])
27
28 Util.ensure_directory_exists(file_directory_path)
29 local file_path = Util.join(file_directory_path, filename)
30
31 open_or_create_from_template(type, file_path)
32end
33
56292c79
RBR
34-------------------------------------------------------------------------------
35-- Public Interface
36-------------------------------------------------------------------------------
37
38--- Opens the daily note
2cc29448
RBR
39function Notes.open_daily(date)
40 date = date or os.date('%Y-%m-%d')
41 local filename = date .. '.md'
503d09fc 42 open_periodic_note('daily', filename)
56292c79
RBR
43end
44
45--- Opens the weekly note
503d09fc
RBR
46function Notes.open_weekly()
47 local filename = os.date('%Y-w%V')
48 open_periodic_note('weekly', filename)
56292c79
RBR
49end
50
51--- Opens the monthly note
503d09fc
RBR
52function Notes.open_monthly()
53 local filename = os.date('%Y-%m') .. '.md'
54 open_periodic_note('monthly', filename)
56292c79
RBR
55end
56
57--- Opens the seasonal note
503d09fc
RBR
58function Notes.open_seasonal()
59 local year = os.date('%Y')
60 local month = tonumber(os.date('%m'))
61 local season = math.ceil(month / 3)
62
63 local filename = year .. '-s' .. season .. '.md'
64 open_periodic_note('seasonal', filename)
56292c79
RBR
65end
66
67--- Opens the yearly note
503d09fc
RBR
68function Notes.open_yearly()
69 local filename = os.date('%Y') .. '.md'
70 open_periodic_note('yearly', filename)
56292c79
RBR
71end
72
73--- Opens an arbitrary note
503d09fc
RBR
74function Notes.open()
75 local success, module = pcall(require, 'fzf-lua')
76 if success then
77 local notes_path = Configuration.path_for()
78 Util.ensure_directory_exists(notes_path)
79 module.files({ cwd = notes_path })
80 else
81 api.nvim_err_writeln('This feature requires optional dependency fzf-lua')
82 end
56292c79
RBR
83end
84
503d09fc 85return Notes