feat(notable): Add a function for listing existing notes

Change-Id: I23697b4798ee4d4e94d3f7c1a4e4e9abf5115345
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1982
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
Vincent Ambo 2020-09-15 18:51:09 +01:00 committed by tazjin
parent 6b16e5c1ef
commit 40aeba6281

View file

@ -51,14 +51,17 @@
(defvar notable--note-lock (make-mutex "notable-notes") (defvar notable--note-lock (make-mutex "notable-notes")
"Exclusive lock for note operations with shared state.") "Exclusive lock for note operations with shared state.")
(defvar notable--next-note (defvar notable--note-regexp
(let ((next 0) (rx "note-"
(note-regexp (rx "note-"
(group (one-or-more (any num))) (group (one-or-more (any num)))
".json"))) ".json")
"Regular expression to match note file names.")
(defvar notable--next-note
(let ((next 0))
(-each (f-entries notable-note-dir) (-each (f-entries notable-note-dir)
(lambda (file) (lambda (file)
(when-let* ((match (string-match note-regexp file)) (when-let* ((match (string-match notable--note-regexp file))
(id (string-to-number (id (string-to-number
(match-string 1 file))) (match-string 1 file)))
(larger (> id next))) (larger (> id next)))
@ -100,6 +103,14 @@
(f-write-text (notable--serialize-note note) 'utf-8 path) (f-write-text (notable--serialize-note note) 'utf-8 path)
(message "Saved note %d" id))) (message "Saved note %d" id)))
(defun notable--list-note-ids ()
"List all note IDs (not contents) from `notable-note-dir'"
(cl-loop for file in (f-entries notable-note-dir)
with res = nil
if (string-match notable--note-regexp file)
do (push (string-to-number (match-string 1 file)) res)
finally return res))
;; User-facing functions ;; User-facing functions
(defun notable-take-note (content) (defun notable-take-note (content)