You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
buku-fzf/cache.janet

54 lines
2.2 KiB

#!/usr/bin/env janet
(import sh)
(defn- format-bookmarks
[raw-bookmarks]
(if-let [parsed-bookmarks (peg/match
'{:column (some (if-not (+ "\t" "\n") 1))
:main (some (group (* ':column "\t"
':column "\t"
(at-most 1 ':column)
"\n")))}
raw-bookmarks)]
(let [digits (inc (math/floor (math/log10 (length parsed-bookmarks))))]
(string/join (map |(let [[index title tags] $]
(string/format
(string "%-" digits
(if tags
"s %s (%s)"
"s %s"))
index title tags))
parsed-bookmarks)
"\n"))
(error "Failed to parse bookmarks.")))
(defn- get-cached-bookmarks
[]
(when-let [home (os/getenv "HOME")
db-modified
(if-let [xdg-data-home (os/getenv "XDG_DATA_HOME")
db (os/stat (string xdg-data-home "/buku/bookmarks.db"))]
(db :modified)
(get (os/stat (string home "/.local/share/buku/bookmarks.db"))
:modified))]
(let [cache-dir (string home "/.cache")
cache (string cache-dir "/buku-fzf")
update-cache |(let [bookmarks (format-bookmarks
(sh/$< buku --nostdin -p -f 5))]
(when (not (os/stat cache-dir))
(os/mkdir cache-dir))
(if-with [f (file/open cache :w)]
(file/write f bookmarks)
(error (string "Cannot write to " cache)))
bookmarks)]
(if-let [cache-modified (get (os/stat cache) :modified)]
(if (< cache-modified db-modified)
(update-cache)
(if-with [f (file/open cache :r)]
(file/read f :all)
(error (string "Cannot read " cache))))
(update-cache)))))
(defn main
[&]
(print (get-cached-bookmarks)))