]> git.r.bdr.sh - rbdr/nota.nvim/blobdiff - lua/plan.lua
Add minimum workable functionality
[rbdr/nota.nvim] / lua / plan.lua
index e78ee00f9534c53e1eb9bca9f3c172a3fcf7c636..477ff9a0a35677bf16de33048ae17e0661aa7b37 100644 (file)
@@ -1,23 +1,67 @@
 -------------------------------------------------------------------------------
 -- Tools to deal with the plan file
 -------------------------------------------------------------------------------
 -------------------------------------------------------------------------------
 -- 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
 
 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
 
 -------------------------------------------------------------------------------
 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