1 -------------------------------------------------------------------------------
2 -- Tools to deal with the plan file
3 -------------------------------------------------------------------------------
6 local Configuration = require('configuration')
7 local Util = require('util')
8 -------------------------------------------------------------------------------
10 -------------------------------------------------------------------------------
11 local function open_or_create_from_template(file_path, force_template)
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')
18 template_contents = template_contents .. '[' .. date .. ']\n'
19 plan_file = io.open(file_path, 'w')
20 plan_file:write(template_contents)
23 vim.cmd('edit ' .. file_path)
26 local 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
36 -------------------------------------------------------------------------------
38 -------------------------------------------------------------------------------
40 --- Opens the active plan file
41 function 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)
48 --- Capture a new plan file and archive the current one
49 function Plan.capture()
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)
56 local archive_path = Configuration.path_for(Configuration.configuration.plan.archive)
57 Util.ensure_directory_exists(archive_path)
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))