diff --git a/README.md b/README.md index 4728c82..dbc40fa 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,11 @@ require("bookmarks"):setup({ When enabled, a new bookmark is automatically created in `''` which allows the user to jump back to the last directory. +### `persist` + +When enabled the bookmarks will persist, i.e. if you close and reopen Yazi they will still be +present. + ### `notify` When enabled, notifications will be shown when the user creates a new bookmark and deletes one or diff --git a/init.lua b/init.lua index 203b200..7f4239b 100644 --- a/init.lua +++ b/init.lua @@ -24,6 +24,24 @@ local _send_notification = ya.sync( end ) +local _persist_bookmarks = ya.sync(function(state) + ya.err("Persist Bookmarks") + ps.sub_remote("bookmarks", function(body) + if not state.bookmarks and body then + state.bookmarks = {} + + for _, value in pairs(body) do + table.insert(state.bookmarks, value) + end + end + end) +end) + +local _save_bookmark = ya.sync(function(_, bookmarks) + ya.err("Save Bookmark") + ps.pub_static(10, "bookmarks", bookmarks) +end) + local _save_last_directory = ya.sync(function(state) ps.sub("cd", function() local folder = Folder:by_kind(Folder.CURRENT) @@ -62,6 +80,10 @@ local save_bookmark = ya.sync(function(state, idx) cursor = folder.cursor, } + if state.persist then + _save_bookmark(state.bookmarks) + end + if state.notify and state.notify.enable then local message = state.notify.message.new message, _ = message:gsub("", SUPPORTED_KEYS[idx].on) @@ -95,11 +117,19 @@ local delete_bookmark = ya.sync(function(state, idx) end table.remove(state.bookmarks, idx) + + if state.persist then + _save_bookmark(state.bookmarks) + end end) local delete_all_bookmarks = ya.sync(function(state) state.bookmarks = nil + if state.persist then + _save_bookmark(nil) + end + if state.notify and state.notify.enable then _send_notification(state.notify.message.delete_all) end @@ -147,6 +177,11 @@ return { _save_last_directory() end + if args.persist then + state.persist = true + _persist_bookmarks() + end + state.notify = { enable = false, timeout = 1,