]> git.r.bdr.sh - rbdr/nota.nvim/blob - lua/notes.lua
Add minimum workable functionality
[rbdr/nota.nvim] / lua / notes.lua
1 -------------------------------------------------------------------------------
2 -- Tools to deal with notes
3 -------------------------------------------------------------------------------
4 local Notes = {}
5
6 local Util = require('util')
7 local Configuration = require('configuration')
8 local api = vim.api
9 -------------------------------------------------------------------------------
10 -- Internal Functions
11 -------------------------------------------------------------------------------
12 local 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)
23 end
24
25 local 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)
32 end
33
34 -------------------------------------------------------------------------------
35 -- Public Interface
36 -------------------------------------------------------------------------------
37
38 --- Opens the daily note
39 function Notes.open_daily()
40 local filename = os.date('%Y-%m-%d') .. '.md'
41 open_periodic_note('daily', filename)
42 end
43
44 --- Opens the weekly note
45 function Notes.open_weekly()
46 local filename = os.date('%Y-w%V')
47 open_periodic_note('weekly', filename)
48 end
49
50 --- Opens the monthly note
51 function Notes.open_monthly()
52 local filename = os.date('%Y-%m') .. '.md'
53 open_periodic_note('monthly', filename)
54 end
55
56 --- Opens the seasonal note
57 function Notes.open_seasonal()
58 local year = os.date('%Y')
59 local month = tonumber(os.date('%m'))
60 local season = math.ceil(month / 3)
61
62 local filename = year .. '-s' .. season .. '.md'
63 open_periodic_note('seasonal', filename)
64 end
65
66 --- Opens the yearly note
67 function Notes.open_yearly()
68 local filename = os.date('%Y') .. '.md'
69 open_periodic_note('yearly', filename)
70 end
71
72 --- Opens an arbitrary note
73 function Notes.open()
74 local success, module = pcall(require, 'fzf-lua')
75 if success then
76 local notes_path = Configuration.path_for()
77 Util.ensure_directory_exists(notes_path)
78 module.files({ cwd = notes_path })
79 else
80 api.nvim_err_writeln('This feature requires optional dependency fzf-lua')
81 end
82 end
83
84 return Notes