X-Git-Url: https://git.r.bdr.sh/rbdr/nota.nvim/blobdiff_plain/56292c7915662bbf721ca8c3d2cee6a04134a9c6..ca10d2a36e3e7d8d28f15f36f0812384606c238d:/lua/plan.lua?ds=inline diff --git a/lua/plan.lua b/lua/plan.lua index e78ee00..477ff9a 100644 --- a/lua/plan.lua +++ b/lua/plan.lua @@ -1,23 +1,67 @@ ------------------------------------------------------------------------------- -- 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