]> git.r.bdr.sh - rbdr/nota.nvim/blame - lua/plan.lua
Add minimum workable functionality
[rbdr/nota.nvim] / lua / plan.lua
CommitLineData
56292c79
RBR
1-------------------------------------------------------------------------------
2-- Tools to deal with the plan file
3-------------------------------------------------------------------------------
503d09fc
RBR
4local Plan = {}
5
6local Configuration = require('configuration')
7local Util = require('util')
56292c79 8-------------------------------------------------------------------------------
503d09fc 9-- Internal Functions
56292c79 10-------------------------------------------------------------------------------
503d09fc 11local function open_or_create_from_template(file_path, force_template)
56292c79 12
503d09fc
RBR
13 local plan_file = io.open(file_path, 'r')
14 if force_template or not plan_file then
15 local template_contents = Configuration.load_template('plan')
16 local date = os.date('%Y-%m-%d')
17
18 template_contents = template_contents .. '[' .. date .. ']\n'
19 plan_file = io.open(file_path, 'w')
20 plan_file:write(template_contents)
21 plan_file:close()
22 end
23 vim.cmd('edit ' .. file_path)
56292c79
RBR
24end
25
503d09fc
RBR
26local function copy(plan_path, archive_path)
27 local plan_file = io.open(plan_path, 'r')
28 local archive_file = io.open(archive_path, 'wb')
29 local plan_content = plan_file:read('*a')
30 archive_file:write(plan_content) -- Write the content to the target file
31
32 plan_file:close()
33 archive_file:close()
56292c79
RBR
34end
35
36-------------------------------------------------------------------------------
503d09fc 37-- Public Interface
56292c79
RBR
38-------------------------------------------------------------------------------
39
503d09fc
RBR
40--- Opens the active plan file
41function Plan.open(force_template)
42 local plan_path = vim.fn.expand(Configuration.configuration.plan.plan_file)
43 local plan_parent = Util.directory_name(plan_path)
44 Util.ensure_directory_exists(plan_parent)
45 open_or_create_from_template(plan_path, force_template)
46end
47
48--- Capture a new plan file and archive the current one
49function Plan.capture()
50 local fs = vim.loop
51
52 local plan_path = vim.fn.expand(Configuration.configuration.plan.plan_file)
53 local plan_parent = Util.directory_name(plan_path)
54 Util.ensure_directory_exists(plan_parent)
55
56 local archive_path = Configuration.path_for(Configuration.configuration.plan.archive)
57 Util.ensure_directory_exists(archive_path)
58
59 if fs.fs_stat(plan_path) then
60 local archive_filename = os.date('%Y-%m-%d') .. '.md'
61 copy(plan_path, Util.join(archive_path, archive_filename))
62 end
63
64 Plan.open(true)
65end
66
67return Plan