|
| 1 | +-- Dependencies: os.date, vim.fn, vim.cmd |
| 2 | + |
| 3 | +local M = {} |
| 4 | + |
| 5 | +function M.check_and_open_daily_note() |
| 6 | + local daily_dir = "pages/daily" |
| 7 | + local today = os.date("%Y-%m-%d") |
| 8 | + local pattern = string.format("%s/%s*-notes.md", daily_dir, today) |
| 9 | + |
| 10 | + -- Check if today's note exists |
| 11 | + local files = vim.fn.glob(pattern, false, true) |
| 12 | + |
| 13 | + if vim.tbl_isempty(files) then |
| 14 | + -- Create new note via copier |
| 15 | + os.execute("copier copy ~/.copier-templates/daily .") |
| 16 | + |
| 17 | + -- Re-glob to get the new file (may need to wait a moment for creation) |
| 18 | + vim.wait(500, function() |
| 19 | + return not vim.tbl_isempty(vim.fn.glob(pattern, false, true)) |
| 20 | + end, 10) |
| 21 | + |
| 22 | + files = vim.fn.glob(pattern, false, true) |
| 23 | + end |
| 24 | + |
| 25 | + -- Open the note (if multiple, just pick the first) |
| 26 | + if not vim.tbl_isempty(files) then |
| 27 | + vim.cmd("edit " .. files[1]) |
| 28 | + else |
| 29 | + print("Failed to find or create today's note.") |
| 30 | + end |
| 31 | + vim.cmd("mode") |
| 32 | +end |
| 33 | + |
| 34 | +vim.api.nvim_create_user_command("DailyNote", function() |
| 35 | + M.check_and_open_daily_note() |
| 36 | +end, {}) |
| 37 | + |
| 38 | +vim.api.nvim_create_user_command("Daily", function() |
| 39 | + M.check_and_open_daily_note() |
| 40 | +end, {}) |
| 41 | + |
| 42 | +vim.api.nvim_create_user_command("DailyFiles", function() |
| 43 | + require("telescope.builtin").find_files({ |
| 44 | + cwd = "pages/daily", |
| 45 | + sorting_strategy = "ascending", |
| 46 | + }) |
| 47 | +end, {}) |
| 48 | + |
| 49 | +vim.keymap.set("n", "<leader>dn", M.check_and_open_daily_note) |
| 50 | +vim.keymap.set("n", "<leader>df", function() |
| 51 | + require("telescope.builtin").find_files({ |
| 52 | + cwd = "pages/daily", |
| 53 | + sorting_strategy = "ascending", |
| 54 | + }) |
| 55 | +end) |
| 56 | + |
| 57 | +return M |
0 commit comments