Merge pull request #14 from dedukun/pub_static

Add Persistence support
This commit is contained in:
Diogo Duarte
2024-04-26 15:44:22 +01:00
committed by GitHub
2 changed files with 95 additions and 17 deletions

View File

@@ -52,6 +52,7 @@ The following are the default configurations:
-- ~/.config/yazi/init.lua -- ~/.config/yazi/init.lua
require("bookmarks"):setup({ require("bookmarks"):setup({
save_last_directory = false, save_last_directory = false,
persist = "none",
notify = { notify = {
enable = false, enable = false,
timeout = 1, timeout = 1,
@@ -69,6 +70,19 @@ 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.
There are three possible values for this option:
| Value | Description |
| ------ | ---------------------------------------------------------------------------------------------------------------------- |
| `none` | The default value, i.e., no persistance |
| `all` | All the bookmarks are saved in persistent memory |
| `vim` | This mode emulates the vim global marks, i.e., only the bookmarks in upper case (A-Z) are saved to persistent memory |
### `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

View File

@@ -24,20 +24,71 @@ local _send_notification = ya.sync(
end end
) )
local _get_real_index = ya.sync(function(state, idx)
for key, value in pairs(state.bookmarks) do
if value.on == SUPPORTED_KEYS[idx].on then
return key
end
end
return nil
end)
local _get_hovered_file = ya.sync(function()
local folder = Folder:by_kind(Folder.CURRENT)
local file_hovered = folder.window[folder.cursor - folder.offset + 1]
if not file_hovered then
return folder.cwd
end
return file_hovered.url
end)
local _load_state = ya.sync(function(state)
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_state = ya.sync(function(state, bookmarks)
if not bookmarks then
ps.pub_static(10, "bookmarks", nil)
return
end
local save_state = {}
if state.persist == "all" then
save_state = bookmarks
else -- VIM mode
local idx = 1
for _, value in pairs(bookmarks) do
-- Only save bookmarks in upper case keys
if string.match(value.on, "%u") then
save_state[idx] = value
idx = idx + 1
end
end
end
ps.pub_static(10, "bookmarks", save_state)
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 file_url = _get_hovered_file()
state.last_dir = state.curr_dir state.last_dir = state.curr_dir
state.curr_dir = { state.curr_dir = {
on = "'", on = "'",
desc = tostring(folder.cwd), desc = tostring(file_url),
cursor = folder.cursor,
} }
end) end)
ps.sub("hover", function() ps.sub("hover", function()
local folder = Folder:by_kind(Folder.CURRENT) local file_url = _get_hovered_file()
state.curr_dir.cursor = folder.cursor state.curr_dir.desc = tostring(file_url)
end) end)
end) end)
@@ -46,26 +97,28 @@ end)
-- ***********************************************/ -- ***********************************************/
local save_bookmark = ya.sync(function(state, idx) local save_bookmark = ya.sync(function(state, idx)
local folder = Folder:by_kind(Folder.CURRENT) local file_url = _get_hovered_file()
state.bookmarks = state.bookmarks or {} state.bookmarks = state.bookmarks or {}
state.indexes = state.indexes or {}
local _idx = state.indexes[idx] local _idx = _get_real_index(idx)
if _idx == nil then if not _idx then
_idx = #state.bookmarks + 1 _idx = #state.bookmarks + 1
state.indexes[idx] = _idx
end end
state.bookmarks[_idx] = { state.bookmarks[_idx] = {
on = SUPPORTED_KEYS[idx].on, on = SUPPORTED_KEYS[idx].on,
desc = tostring(folder.cwd), desc = tostring(file_url),
cursor = folder.cursor,
} }
if state.persist then
_save_state(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>", state.bookmarks[_idx].on)
message, _ = message:gsub("<folder>", tostring(folder.cwd)) message, _ = message:gsub("<folder>", state.bookmarks[_idx].desc)
_send_notification(message) _send_notification(message)
end end
end) end)
@@ -95,11 +148,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_state(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_state(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
@@ -131,9 +192,7 @@ return {
end end
if action == "jump" then if action == "jump" then
ya.manager_emit("cd", { bookmarks[selected].desc }) ya.manager_emit("reveal", { bookmarks[selected].desc })
ya.manager_emit("arrow", { -99999999 })
ya.manager_emit("arrow", { bookmarks[selected].cursor })
elseif action == "delete" then elseif action == "delete" then
delete_bookmark(selected) delete_bookmark(selected)
end end
@@ -147,6 +206,11 @@ return {
_save_last_directory() _save_last_directory()
end end
if args.persist == "all" or args.persist == "vim" then
state.persist = args.persist
_load_state()
end
state.notify = { state.notify = {
enable = false, enable = false,
timeout = 1, timeout = 1,