Initial persistence implementation
This commit is contained in:
@@ -69,6 +69,11 @@ require("bookmarks"):setup({
|
|||||||
When enabled, a new bookmark is automatically created in `''` which allows the user to jump back to
|
When enabled, a new bookmark is automatically created in `''` which allows the user to jump back to
|
||||||
the last directory.
|
the last directory.
|
||||||
|
|
||||||
|
### `persist`
|
||||||
|
|
||||||
|
When enabled the bookmarks will persist, i.e. if you close and reopen Yazi they will still be
|
||||||
|
present.
|
||||||
|
|
||||||
### `notify`
|
### `notify`
|
||||||
|
|
||||||
When enabled, notifications will be shown when the user creates a new bookmark and deletes one or
|
When enabled, notifications will be shown when the user creates a new bookmark and deletes one or
|
||||||
|
|||||||
35
init.lua
35
init.lua
@@ -24,6 +24,24 @@ local _send_notification = ya.sync(
|
|||||||
end
|
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)
|
local _save_last_directory = ya.sync(function(state)
|
||||||
ps.sub("cd", function()
|
ps.sub("cd", function()
|
||||||
local folder = Folder:by_kind(Folder.CURRENT)
|
local folder = Folder:by_kind(Folder.CURRENT)
|
||||||
@@ -62,6 +80,10 @@ local save_bookmark = ya.sync(function(state, idx)
|
|||||||
cursor = folder.cursor,
|
cursor = folder.cursor,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if state.persist then
|
||||||
|
_save_bookmark(state.bookmarks)
|
||||||
|
end
|
||||||
|
|
||||||
if state.notify and state.notify.enable then
|
if state.notify and state.notify.enable then
|
||||||
local message = state.notify.message.new
|
local message = state.notify.message.new
|
||||||
message, _ = message:gsub("<key>", SUPPORTED_KEYS[idx].on)
|
message, _ = message:gsub("<key>", SUPPORTED_KEYS[idx].on)
|
||||||
@@ -95,11 +117,19 @@ local delete_bookmark = ya.sync(function(state, idx)
|
|||||||
end
|
end
|
||||||
|
|
||||||
table.remove(state.bookmarks, idx)
|
table.remove(state.bookmarks, idx)
|
||||||
|
|
||||||
|
if state.persist then
|
||||||
|
_save_bookmark(state.bookmarks)
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local delete_all_bookmarks = ya.sync(function(state)
|
local delete_all_bookmarks = ya.sync(function(state)
|
||||||
state.bookmarks = nil
|
state.bookmarks = nil
|
||||||
|
|
||||||
|
if state.persist then
|
||||||
|
_save_bookmark(nil)
|
||||||
|
end
|
||||||
|
|
||||||
if state.notify and state.notify.enable then
|
if state.notify and state.notify.enable then
|
||||||
_send_notification(state.notify.message.delete_all)
|
_send_notification(state.notify.message.delete_all)
|
||||||
end
|
end
|
||||||
@@ -147,6 +177,11 @@ return {
|
|||||||
_save_last_directory()
|
_save_last_directory()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if args.persist then
|
||||||
|
state.persist = true
|
||||||
|
_persist_bookmarks()
|
||||||
|
end
|
||||||
|
|
||||||
state.notify = {
|
state.notify = {
|
||||||
enable = false,
|
enable = false,
|
||||||
timeout = 1,
|
timeout = 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user