X-Git-Url: https://git.r.bdr.sh/rbdr/nota.nvim/blobdiff_plain/56292c7915662bbf721ca8c3d2cee6a04134a9c6..503d09fc95a47c13141d097cf80dd243d1dce342:/lua/notes.lua diff --git a/lua/notes.lua b/lua/notes.lua index acffc25..008baac 100644 --- a/lua/notes.lua +++ b/lua/notes.lua @@ -1,46 +1,84 @@ ------------------------------------------------------------------------------- -- Tools to deal with notes ------------------------------------------------------------------------------- +local Notes = {} + +local Util = require('util') +local Configuration = require('configuration') +local api = vim.api +------------------------------------------------------------------------------- +-- Internal Functions +------------------------------------------------------------------------------- +local function open_or_create_from_template(type, file_path) + + local journal_file = io.open(file_path, 'r') + if not journal_file then + local template_contents = Configuration.load_template(type) + + journal_file = io.open(file_path, 'w') + journal_file:write(template_contents) + journal_file:close() + end + vim.cmd('edit ' .. file_path) +end + +local function open_periodic_note(type, filename) + local file_directory_path = Configuration.path_for(Configuration.configuration.periodic_locations[type]) + + Util.ensure_directory_exists(file_directory_path) + local file_path = Util.join(file_directory_path, filename) + + open_or_create_from_template(type, file_path) +end + ------------------------------------------------------------------------------- -- Public Interface ------------------------------------------------------------------------------- --- Opens the daily note --- @param configuration tNotaConfiguration the plugin configuration -function open_daily(configuration) - error("Not yet implemented") +function Notes.open_daily() + local filename = os.date('%Y-%m-%d') .. '.md' + open_periodic_note('daily', filename) end --- Opens the weekly note --- @param configuration tNotaConfiguration the plugin configuration -function open_weekly(configuration) - error("Not yet implemented") +function Notes.open_weekly() + local filename = os.date('%Y-w%V') + open_periodic_note('weekly', filename) end --- Opens the monthly note --- @param configuration tNotaConfiguration the plugin configuration -function open_monthly(configuration) - error("Not yet implemented") +function Notes.open_monthly() + local filename = os.date('%Y-%m') .. '.md' + open_periodic_note('monthly', filename) end --- Opens the seasonal note --- @param configuration tNotaConfiguration the plugin configuration -function open_seasonal(configuration) - error("Not yet implemented") +function Notes.open_seasonal() + local year = os.date('%Y') + local month = tonumber(os.date('%m')) + local season = math.ceil(month / 3) + + local filename = year .. '-s' .. season .. '.md' + open_periodic_note('seasonal', filename) end --- Opens the yearly note --- @param configuration tNotaConfiguration the plugin configuration -function open_yearly(configuration) - error("Not yet implemented") +function Notes.open_yearly() + local filename = os.date('%Y') .. '.md' + open_periodic_note('yearly', filename) end --- Opens an arbitrary note --- @param configuration tNotaConfiguration the plugin configuration -function open_note(configuration) - error("Not yet implemented") +function Notes.open() + local success, module = pcall(require, 'fzf-lua') + if success then + local notes_path = Configuration.path_for() + Util.ensure_directory_exists(notes_path) + module.files({ cwd = notes_path }) + else + api.nvim_err_writeln('This feature requires optional dependency fzf-lua') + end end -------------------------------------------------------------------------------- --- Internal Functions -------------------------------------------------------------------------------- +return Notes