about summary refs log tree commit diff
path: root/lua/sketchbook/select_template.lua
diff options
context:
space:
mode:
authoriurii plugatarov <[email protected]>2024-08-02 23:19:11 +0200
committeriurii plugatarov <[email protected]>2024-08-02 23:19:11 +0200
commitf24a3ab9744a3f50eb8cacc06d4ef4c5f96875db (patch)
treee53c36d600f9ded45036d3489411cb82f26cb192 /lua/sketchbook/select_template.lua
parentdfee9611c0134def92d3c254ca4b067338ae7720 (diff)
downloadsketchbook.nvim-f24a3ab9744a3f50eb8cacc06d4ef4c5f96875db.tar.gz
fix
Diffstat (limited to 'lua/sketchbook/select_template.lua')
-rw-r--r--lua/sketchbook/select_template.lua71
1 files changed, 71 insertions, 0 deletions
diff --git a/lua/sketchbook/select_template.lua b/lua/sketchbook/select_template.lua
new file mode 100644
index 0000000..e0c9f7e
--- /dev/null
+++ b/lua/sketchbook/select_template.lua
@@ -0,0 +1,71 @@
+local M = {}
+
+local templates_dir = vim.g.notes_templates_dir or "~/notes/templates/"
+
+function M.select_template()
+  templates_dir = vim.fn.expand(templates_dir)
+  local templates = vim.fn.readdir(templates_dir, function(name)
+    return name:match "%.md$"
+  end)
+
+  if vim.tbl_isempty(templates) then
+    M.create_note_from_template(nil)
+  else
+    local template_files = {}
+    for _, template in ipairs(templates) do
+      table.insert(template_files, templates_dir .. template)
+    end
+
+    require("telescope.pickers")
+      .new({}, {
+        prompt_title = "Select Template",
+        finder = require("telescope.finders").new_table {
+          results = template_files,
+          entry_maker = function(entry)
+            return {
+              value = entry,
+              display = vim.fn.fnamemodify(entry, ":t"),
+              ordinal = entry,
+            }
+          end,
+        },
+        sorter = require("telescope.config").values.generic_sorter {},
+        attach_mappings = function(prompt_bufnr, map)
+          local actions = require "telescope.actions"
+          local action_state = require "telescope.actions.state"
+
+          actions.select_default:replace(function()
+            actions.close(prompt_bufnr)
+            local selection = action_state.get_selected_entry()
+            M.create_note_from_template(selection.value)
+          end)
+
+          return true
+        end,
+      })
+      :find()
+  end
+end
+
+function M.create_note_from_template(template_path)
+  local notes_dir = vim.g.notes_directory or "~/notes/"
+  local filename = os.date "%Y-%m-%d_%H-%M-%S" .. ".md"
+  local filepath = vim.fn.expand(notes_dir) .. filename
+  vim.cmd("edit " .. filepath)
+  if template_path then
+    vim.cmd("0r " .. template_path)
+  else
+    local title = "Title " .. os.date "%Y-%m-%d %H:%M:%S"
+    local template = {
+      "# " .. title,
+      "",
+    }
+    local file = io.open(filepath, "w")
+    for _, line in ipairs(template) do
+      file:write(line .. "\n")
+    end
+    file:close()
+  end
+end
+
+return M