--------------------------------------------------------------------------------
--- 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
-function Notes.open_daily()
- local filename = os.date('%Y-%m-%d') .. '.md'
- open_periodic_note('daily', filename)
-end
-
---- Opens the weekly note
-function Notes.open_weekly()
- local filename = os.date('%Y-w%V')
- open_periodic_note('weekly', filename)
-end
-
---- Opens the monthly note
-function Notes.open_monthly()
- local filename = os.date('%Y-%m') .. '.md'
- open_periodic_note('monthly', filename)
-end
-
---- Opens the seasonal note
-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
-function Notes.open_yearly()
- local filename = os.date('%Y') .. '.md'
- open_periodic_note('yearly', filename)
-end
-
---- Opens an arbitrary note
-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
-
-return Notes