From ebca541f572cafb01a2f6daf8237492791f33891 Mon Sep 17 00:00:00 2001 From: HumanEntity Date: Sun, 24 Dec 2023 23:34:26 +0100 Subject: Little restructurization --- lua/denote/api.lua | 74 +++++++++++++++++++++++++++++++++ lua/denote/init.lua | 88 +-------------------------------------- lua/denote/internal.lua | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ lua/denote/util.lua | 107 +++-------------------------------------------- 4 files changed, 188 insertions(+), 189 deletions(-) create mode 100644 lua/denote/api.lua create mode 100644 lua/denote/internal.lua diff --git a/lua/denote/api.lua b/lua/denote/api.lua new file mode 100644 index 0000000..cf26042 --- /dev/null +++ b/lua/denote/api.lua @@ -0,0 +1,74 @@ +local M = {} +local internal = require("denote.internal") +local util = require("denote.util") + +function M.note() + local name = "" + local tags = nil + + vim.ui.input({ prompt = "Note name: " }, function(input) + name = input + end) + + vim.ui.input({ prompt = "Enter tags: " }, function(input) + if input ~= "" and input then + tags = splitspace(input) + end + end) + + if name == "" then + error("Didn't specify name") + end + + internal.note(name, tags) +end + +function M.search() + local date = nil + local name = nil + + vim.ui.input({ prompt = "Date: " }, function(input) + local split = util.splitspace(input) + if split[0] then + date = {} + date.year = split[0] + if split[1] then + date.month = split[1] + if split[2] then + date.day = split[2] + end + end + end + end) + vim.ui.input({ prompt = "Name: " }, function(input) + name = input + end) + + local status = internal.search(date, name, function(input) + if input then + vim.cmd("e " .. internal.config.vault.dir .. "/" .. input) + end + if date then + print(date.year, " ", date.month, " ", date.day) + print("FSD") + end + end) + -- print(date) + -- if date then + -- print(date.year, " ", date.month, " ", date.day) + -- print("FSD") + -- end + + if not status then + print("Error opening file") + end +end + +---@param config DenoteConfig +function M.setup(config) + if config then + internal.config = config + end +end + +return M diff --git a/lua/denote/init.lua b/lua/denote/init.lua index b96704b..942c8d8 100644 --- a/lua/denote/init.lua +++ b/lua/denote/init.lua @@ -1,89 +1,3 @@ -local M = {} -local U = require("denote.util") - --- local function splitlines(str) --- local result = {} --- for line in string.gmatch(str .. "\n", "(.-)\n") do --- table.insert(result, line) --- end --- return result --- end - -local function splitspace(str) - local chunks = {} - for substring in str:gmatch("%S+") do - table.insert(chunks, #chunks, substring) - end - return chunks -end - -function M.note() - local name = "" - local tags = nil - - vim.ui.input({ prompt = "Note name: " }, function(input) - name = input - end) - - vim.ui.input({ prompt = "Enter tags: " }, function(input) - if input ~= "" and input then - tags = splitspace(input) - end - end) - - if name == "" then - error("Didn't specify name") - end - - U.note(name, tags) -end - -function M.search() - local date = nil - local name = nil - - vim.ui.input({ prompt = "Date: " }, function(input) - local split = splitspace(input) - if split[0] then - date = {} - date.year = split[0] - if split[1] then - date.month = split[1] - if split[2] then - date.day = split[2] - end - end - end - end) - vim.ui.input({ prompt = "Name: " }, function(input) - name = input - end) - - local status = U.search(date, name, function(input) - if input then - vim.cmd("e " .. U.config.vault.dir .. "/" .. input) - end - if date then - print(date.year, " ", date.month, " ", date.day) - print("FSD") - end - end) - -- print(date) - -- if date then - -- print(date.year, " ", date.month, " ", date.day) - -- print("FSD") - -- end - - if not status then - print("Error opening file") - end -end - ----@param config DenoteConfig -function M.setup(config) - if config then - U.config = config - end -end +local M = require("denote.api") return M diff --git a/lua/denote/internal.lua b/lua/denote/internal.lua new file mode 100644 index 0000000..95ee7dc --- /dev/null +++ b/lua/denote/internal.lua @@ -0,0 +1,108 @@ +local M = {} +M.config = require("denote.config") + +---@class DenoteDate +---@field year string +---@field month string +---@field day string + +---@param name string +---@param tags table|nil +---@return string filename format of files in denote.nvim +function M.file(name, tags) + local file = "" + + local date = tostring(os.date("%Y%m%d")) + + local tags_str = "" + + if tags then + for i, tag in pairs(tags) do + tags_str = tags_str .. tag + if i < #tags then + tags_str = tags_str .. M.config.filename.tag_sep + end + end + end + + file = date .. M.config.filename.date_sep .. M.config.filename.date_sep .. name + if tags then + file = file .. M.config.filename.name_sep .. M.config.filename.name_sep .. tags_str + end + + return file .. "." .. M.config.filename.ext +end + +---@param name string +---@param tags table|nil +--- Opens your note +function M.note(name, tags) + local filename = M.config.vault.dir .. M.file(name, tags) + + -- Echo template + + vim.cmd("!mkdir -p " .. M.config.vault.dir) + vim.cmd("e " .. filename) +end + +---@param date DenoteDate|nil +---@param name string|nil +---@param func function +function M.search(date, name, func) + local items = {} + local matcher = "" + + if date then + if date.year then + matcher = date.year + else + matcher = "%d%d%d%d" + end + if date.month then + matcher = matcher .. date.month + else + matcher = "%d%d" + end + if date.day then + matcher = matcher .. date.day + else + matcher = "%d%d" + end + + matcher = matcher .. M.config.filename.date_sep .. M.config.filename.date_sep + else + matcher = "%d+" .. M.config.filename.date_sep .. M.config.filename.date_sep + end + + if name then + matcher = matcher .. name + else + matcher = matcher .. ".+" + end + + -- matcher = matcher .. M.config.filename.name_sep .. "?" .. M.config.filename.name_sep .. "?" .. ".-" + + for file in io.popen("ls " .. M.config.vault.dir):lines() do + local matched = file:match(matcher) + + if matched then + print("file " .. file) + items[#items + 1] = file + end + end + + if not items or #items == 0 then + return nil + end + + vim.ui.select(items, { + prompt = "Select note", + -- format_item = function(item) + -- return "" .. item + -- end, + }, func) + + return true +end + +return M diff --git a/lua/denote/util.lua b/lua/denote/util.lua index 95ee7dc..ce998f0 100644 --- a/lua/denote/util.lua +++ b/lua/denote/util.lua @@ -1,108 +1,11 @@ local M = {} -M.config = require("denote.config") ----@class DenoteDate ----@field year string ----@field month string ----@field day string - ----@param name string ----@param tags table|nil ----@return string filename format of files in denote.nvim -function M.file(name, tags) - local file = "" - - local date = tostring(os.date("%Y%m%d")) - - local tags_str = "" - - if tags then - for i, tag in pairs(tags) do - tags_str = tags_str .. tag - if i < #tags then - tags_str = tags_str .. M.config.filename.tag_sep - end - end - end - - file = date .. M.config.filename.date_sep .. M.config.filename.date_sep .. name - if tags then - file = file .. M.config.filename.name_sep .. M.config.filename.name_sep .. tags_str - end - - return file .. "." .. M.config.filename.ext -end - ----@param name string ----@param tags table|nil ---- Opens your note -function M.note(name, tags) - local filename = M.config.vault.dir .. M.file(name, tags) - - -- Echo template - - vim.cmd("!mkdir -p " .. M.config.vault.dir) - vim.cmd("e " .. filename) -end - ----@param date DenoteDate|nil ----@param name string|nil ----@param func function -function M.search(date, name, func) - local items = {} - local matcher = "" - - if date then - if date.year then - matcher = date.year - else - matcher = "%d%d%d%d" - end - if date.month then - matcher = matcher .. date.month - else - matcher = "%d%d" - end - if date.day then - matcher = matcher .. date.day - else - matcher = "%d%d" - end - - matcher = matcher .. M.config.filename.date_sep .. M.config.filename.date_sep - else - matcher = "%d+" .. M.config.filename.date_sep .. M.config.filename.date_sep +function M.splitspace(str) + local chunks = {} + for substring in str:gmatch("%S+") do + table.insert(chunks, #chunks, substring) end - - if name then - matcher = matcher .. name - else - matcher = matcher .. ".+" - end - - -- matcher = matcher .. M.config.filename.name_sep .. "?" .. M.config.filename.name_sep .. "?" .. ".-" - - for file in io.popen("ls " .. M.config.vault.dir):lines() do - local matched = file:match(matcher) - - if matched then - print("file " .. file) - items[#items + 1] = file - end - end - - if not items or #items == 0 then - return nil - end - - vim.ui.select(items, { - prompt = "Select note", - -- format_item = function(item) - -- return "" .. item - -- end, - }, func) - - return true + return chunks end return M -- cgit v1.2.3