-------------------------------------------------------------------------------
-- Tools to deal with the plan file
-------------------------------------------------------------------------------
+local Plan = {}
+
+local Configuration = require('configuration')
+local Util = require('util')
-------------------------------------------------------------------------------
--- Public Interface
+-- Internal Functions
-------------------------------------------------------------------------------
+local function open_or_create_from_template(file_path, force_template)
---- Opens the active plan file
--- @param configuration tNotaConfiguration the plugin configuration
-function open_plan(configuration)
- error("Not yet implemented")
+ local plan_file = io.open(file_path, 'r')
+ if force_template or not plan_file then
+ local template_contents = Configuration.load_template('plan')
+ local date = os.date('%Y-%m-%d')
+
+ template_contents = template_contents .. '[' .. date .. ']\n'
+ plan_file = io.open(file_path, 'w')
+ plan_file:write(template_contents)
+ plan_file:close()
+ end
+ vim.cmd('edit ' .. file_path)
end
---- Capture a new plan file and archive the current one
--- @param configuration tNotaConfiguration the plugin configuration
-function capture_plan(configuration)
- error("Not yet implemented")
+local function copy(plan_path, archive_path)
+ local plan_file = io.open(plan_path, 'r')
+ local archive_file = io.open(archive_path, 'wb')
+ local plan_content = plan_file:read('*a')
+ archive_file:write(plan_content) -- Write the content to the target file
+
+ plan_file:close()
+ archive_file:close()
end
-------------------------------------------------------------------------------
--- Internal Functions
+-- Public Interface
-------------------------------------------------------------------------------
+--- Opens the active plan file
+function Plan.open(force_template)
+ local plan_path = vim.fn.expand(Configuration.configuration.plan.plan_file)
+ local plan_parent = Util.directory_name(plan_path)
+ Util.ensure_directory_exists(plan_parent)
+ open_or_create_from_template(plan_path, force_template)
+end
+
+--- Capture a new plan file and archive the current one
+function Plan.capture()
+ local fs = vim.loop
+
+ local plan_path = vim.fn.expand(Configuration.configuration.plan.plan_file)
+ local plan_parent = Util.directory_name(plan_path)
+ Util.ensure_directory_exists(plan_parent)
+
+ local archive_path = Configuration.path_for(Configuration.configuration.plan.archive)
+ Util.ensure_directory_exists(archive_path)
+
+ if fs.fs_stat(plan_path) then
+ local archive_filename = os.date('%Y-%m-%d') .. '.md'
+ copy(plan_path, Util.join(archive_path, archive_filename))
+ end
+
+ Plan.open(true)
+end
+
+return Plan