From a8cb81fc015d3b5bda4b5d9b37ca34debddc8f1c Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 04:34:58 +0100 Subject: [PATCH 01/13] Initial persistence implementation --- README.md | 5 +++++ init.lua | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) 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, From 8e7219011427b178c4ab784ea57b109e71f70e53 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 04:43:15 +0100 Subject: [PATCH 02/13] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dbc40fa..87c54f4 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ The following are the default configurations: -- ~/.config/yazi/init.lua require("bookmarks"):setup({ save_last_directory = false, + persist = false, notify = { enable = false, timeout = 1, From 411b8e9bf49f9fa93d49c52e3b6d943fd3ca7f53 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 05:22:20 +0100 Subject: [PATCH 03/13] added new persistent mode --- README.md | 10 +++++++++- init.lua | 26 ++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 87c54f4..088353f 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ The following are the default configurations: -- ~/.config/yazi/init.lua require("bookmarks"):setup({ save_last_directory = false, - persist = false, + persist = "none", notify = { enable = false, timeout = 1, @@ -75,6 +75,14 @@ the last directory. 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` 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 7f4239b..d11664c 100644 --- a/init.lua +++ b/init.lua @@ -37,9 +37,27 @@ local _persist_bookmarks = ya.sync(function(state) end) end) -local _save_bookmark = ya.sync(function(_, bookmarks) +local _save_bookmark = ya.sync(function(state, bookmarks) ya.err("Save Bookmark") - ps.pub_static(10, "bookmarks", bookmarks) + if not bookmarks then + ps.pub_static(10, "bookmarks", nil) + return + end + + if state.persist == "all" then + ps.pub_static(10, "bookmarks", bookmarks) + return + end + + -- VIM mode + local save_bookmarks = {} + for _, value in pairs(bookmarks) do + -- Only save bookmarks in upper case keys + if string.match(value.on, "%u") then + table.insert(save_bookmarks, value) + end + end + ps.pub_static(10, "bookmarks", save_bookmarks) end) local _save_last_directory = ya.sync(function(state) @@ -177,8 +195,8 @@ return { _save_last_directory() end - if args.persist then - state.persist = true + if args.persist == "all" or args.persist == "vim" then + state.persist = args.persist _persist_bookmarks() end From 94c661afe32a43b5f9297dcc920760615fbe0639 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 05:27:10 +0100 Subject: [PATCH 04/13] Renamed _persist_bookmarks -> _load_bookmarks --- init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index d11664c..da68edc 100644 --- a/init.lua +++ b/init.lua @@ -24,7 +24,7 @@ local _send_notification = ya.sync( end ) -local _persist_bookmarks = ya.sync(function(state) +local _load_bookmarks = ya.sync(function(state) ya.err("Persist Bookmarks") ps.sub_remote("bookmarks", function(body) if not state.bookmarks and body then @@ -197,7 +197,7 @@ return { if args.persist == "all" or args.persist == "vim" then state.persist = args.persist - _persist_bookmarks() + _load_bookmarks() end state.notify = { From be82c3072ee1d30ea6f41478a40f522844377f9f Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 13:42:20 +0100 Subject: [PATCH 05/13] Also save the indexes --- init.lua | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/init.lua b/init.lua index da68edc..0b8860e 100644 --- a/init.lua +++ b/init.lua @@ -24,40 +24,48 @@ local _send_notification = ya.sync( end ) -local _load_bookmarks = ya.sync(function(state) - ya.err("Persist Bookmarks") +local _load_state = ya.sync(function(state) + ya.err("Persist State") ps.sub_remote("bookmarks", function(body) if not state.bookmarks and body then + state.indexes = {} state.bookmarks = {} - - for _, value in pairs(body) do - table.insert(state.bookmarks, value) + for key, value in pairs(body.indexes) do + state.indexes[tonumber(key)] = value + end + for key, value in pairs(body.bookmarks) do + state.bookmarks[tonumber(key)] = value end end end) end) -local _save_bookmark = ya.sync(function(state, bookmarks) - ya.err("Save Bookmark") +local _save_state = ya.sync(function(state, bookmarks, indexes) + ya.err("Save State") if not bookmarks then ps.pub_static(10, "bookmarks", nil) return end + local save_state = {} if state.persist == "all" then - ps.pub_static(10, "bookmarks", bookmarks) - return + save_state = { bookmarks = bookmarks, indexes = indexes } + else -- VIM mode + local save_bookmarks = {} + local save_indexes = {} + for key, value in pairs(bookmarks) do + -- Only save bookmarks in upper case keys + if string.match(value.on, "%u") then + table.insert(save_bookmarks, value) + table.insert(save_indexes, indexes[key]) + end + end + + save_state = { bookmarks = save_bookmarks, indexes = save_indexes } end - -- VIM mode - local save_bookmarks = {} - for _, value in pairs(bookmarks) do - -- Only save bookmarks in upper case keys - if string.match(value.on, "%u") then - table.insert(save_bookmarks, value) - end - end - ps.pub_static(10, "bookmarks", save_bookmarks) + ya.err("SAVE STATE: " .. serialize(save_state)) + ps.pub_static(10, "bookmarks", save_state) end) local _save_last_directory = ya.sync(function(state) @@ -98,8 +106,11 @@ local save_bookmark = ya.sync(function(state, idx) cursor = folder.cursor, } + ya.err("INDEXES " .. serialize(state.indexes)) + ya.err("BOOKMARKS " .. serialize(state.bookmarks)) + if state.persist then - _save_bookmark(state.bookmarks) + _save_state(state.bookmarks, state.indexes) end if state.notify and state.notify.enable then @@ -137,7 +148,7 @@ local delete_bookmark = ya.sync(function(state, idx) table.remove(state.bookmarks, idx) if state.persist then - _save_bookmark(state.bookmarks) + _save_state(state.bookmarks, state.indexes) end end) @@ -145,7 +156,7 @@ local delete_all_bookmarks = ya.sync(function(state) state.bookmarks = nil if state.persist then - _save_bookmark(nil) + _save_state(nil) end if state.notify and state.notify.enable then @@ -197,7 +208,7 @@ return { if args.persist == "all" or args.persist == "vim" then state.persist = args.persist - _load_bookmarks() + _load_state() end state.notify = { From 61171cbc8aa00688b20e16a7354329a50552889a Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 13:58:52 +0100 Subject: [PATCH 06/13] remove the indexes entry when deleting bookmarks --- init.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 0b8860e..53236f2 100644 --- a/init.lua +++ b/init.lua @@ -145,7 +145,14 @@ local delete_bookmark = ya.sync(function(state, idx) _send_notification(message) end - table.remove(state.bookmarks, idx) + state.bookmarks[idx] = nil + -- remove the indexes entry for the bookmark + for key, value in pairs(state.indexes) do + if value == idx then + state.indexes[key] = nil + break + end + end if state.persist then _save_state(state.bookmarks, state.indexes) @@ -154,9 +161,10 @@ end) local delete_all_bookmarks = ya.sync(function(state) state.bookmarks = nil + state.indexes = nil if state.persist then - _save_state(nil) + _save_state(nil, nil) end if state.notify and state.notify.enable then From 9c75f7d8cba32675980d2ffa7f0dec0e71a9f00b Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 14:08:44 +0100 Subject: [PATCH 07/13] vim mode also working now --- init.lua | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/init.lua b/init.lua index 53236f2..44b7472 100644 --- a/init.lua +++ b/init.lua @@ -24,8 +24,17 @@ local _send_notification = ya.sync( end ) +local _get_real_index = ya.sync(function(state, idx) + for key, value in pairs(state.indexes) do + if value == idx then + return key + end + end + + return nil +end) + local _load_state = ya.sync(function(state) - ya.err("Persist State") ps.sub_remote("bookmarks", function(body) if not state.bookmarks and body then state.indexes = {} @@ -41,7 +50,6 @@ local _load_state = ya.sync(function(state) end) local _save_state = ya.sync(function(state, bookmarks, indexes) - ya.err("Save State") if not bookmarks then ps.pub_static(10, "bookmarks", nil) return @@ -56,15 +64,15 @@ local _save_state = ya.sync(function(state, bookmarks, indexes) for key, value in pairs(bookmarks) do -- Only save bookmarks in upper case keys if string.match(value.on, "%u") then - table.insert(save_bookmarks, value) - table.insert(save_indexes, indexes[key]) + save_bookmarks[key] = value + local real_index = _get_real_index(key) + save_indexes[real_index] = indexes[real_index] end end save_state = { bookmarks = save_bookmarks, indexes = save_indexes } end - ya.err("SAVE STATE: " .. serialize(save_state)) ps.pub_static(10, "bookmarks", save_state) end) @@ -106,9 +114,6 @@ local save_bookmark = ya.sync(function(state, idx) cursor = folder.cursor, } - ya.err("INDEXES " .. serialize(state.indexes)) - ya.err("BOOKMARKS " .. serialize(state.bookmarks)) - if state.persist then _save_state(state.bookmarks, state.indexes) end @@ -147,11 +152,9 @@ local delete_bookmark = ya.sync(function(state, idx) state.bookmarks[idx] = nil -- remove the indexes entry for the bookmark - for key, value in pairs(state.indexes) do - if value == idx then - state.indexes[key] = nil - break - end + local real_index = _get_real_index(idx) + if real_index then + state.indexes[real_index] = nil end if state.persist then From 98f8e1e283127968a30a2e598bec5b3ea54032e7 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 14:32:49 +0100 Subject: [PATCH 08/13] Fix jump --- init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 44b7472..d0bddd7 100644 --- a/init.lua +++ b/init.lua @@ -202,8 +202,8 @@ return { if action == "jump" then ya.manager_emit("cd", { bookmarks[selected].desc }) - ya.manager_emit("arrow", { -99999999 }) - ya.manager_emit("arrow", { bookmarks[selected].cursor }) + ya.manager_emit("arrow", { tostring(-99999999) }) + ya.manager_emit("arrow", { tostring(bookmarks[selected].cursor) }) elseif action == "delete" then delete_bookmark(selected) end From bea4f92b75ca905c932c55936d4837fde3d78154 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 19 Apr 2024 14:47:37 +0100 Subject: [PATCH 09/13] revert "Fix jump" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 三咲雅 · Misaki Masa --- init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index d0bddd7..44b7472 100644 --- a/init.lua +++ b/init.lua @@ -202,8 +202,8 @@ return { if action == "jump" then ya.manager_emit("cd", { bookmarks[selected].desc }) - ya.manager_emit("arrow", { tostring(-99999999) }) - ya.manager_emit("arrow", { tostring(bookmarks[selected].cursor) }) + ya.manager_emit("arrow", { -99999999 }) + ya.manager_emit("arrow", { bookmarks[selected].cursor }) elseif action == "delete" then delete_bookmark(selected) end From 60bfe6f160837f143663a0e4aa1918851b8e1ccb Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Mon, 22 Apr 2024 13:26:19 +0100 Subject: [PATCH 10/13] Fix delete bookmarks --- init.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/init.lua b/init.lua index 44b7472..ccd973d 100644 --- a/init.lua +++ b/init.lua @@ -61,12 +61,14 @@ local _save_state = ya.sync(function(state, bookmarks, indexes) else -- VIM mode local save_bookmarks = {} local save_indexes = {} + local idx = 1 for key, value in pairs(bookmarks) do -- Only save bookmarks in upper case keys if string.match(value.on, "%u") then - save_bookmarks[key] = value + save_bookmarks[idx] = value local real_index = _get_real_index(key) - save_indexes[real_index] = indexes[real_index] + save_indexes[real_index] = idx + idx = idx + 1 end end @@ -150,11 +152,11 @@ local delete_bookmark = ya.sync(function(state, idx) _send_notification(message) end - state.bookmarks[idx] = nil + table.remove(state.bookmarks, idx) -- remove the indexes entry for the bookmark local real_index = _get_real_index(idx) if real_index then - state.indexes[real_index] = nil + table.remove(state.indexes, real_index) end if state.persist then From c52de13eb0f1c232f7675ab7dab226a92e0f0b55 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 26 Apr 2024 15:14:57 +0100 Subject: [PATCH 11/13] removed 'state.indexes' + fixed vim mode bookmark deletion --- init.lua | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/init.lua b/init.lua index ccd973d..17fc54f 100644 --- a/init.lua +++ b/init.lua @@ -25,31 +25,26 @@ local _send_notification = ya.sync( ) local _get_real_index = ya.sync(function(state, idx) - for key, value in pairs(state.indexes) do - if value == idx then + for key, value in pairs(state.bookmarks) do + if value.on == SUPPORTED_KEYS[idx].on then return key end end - return nil end) local _load_state = ya.sync(function(state) ps.sub_remote("bookmarks", function(body) if not state.bookmarks and body then - state.indexes = {} state.bookmarks = {} - for key, value in pairs(body.indexes) do - state.indexes[tonumber(key)] = value - end - for key, value in pairs(body.bookmarks) do - state.bookmarks[tonumber(key)] = value + for _, value in pairs(body) do + table.insert(state.bookmarks, value) end end end) end) -local _save_state = ya.sync(function(state, bookmarks, indexes) +local _save_state = ya.sync(function(state, bookmarks) if not bookmarks then ps.pub_static(10, "bookmarks", nil) return @@ -57,22 +52,16 @@ local _save_state = ya.sync(function(state, bookmarks, indexes) local save_state = {} if state.persist == "all" then - save_state = { bookmarks = bookmarks, indexes = indexes } + save_state = bookmarks else -- VIM mode - local save_bookmarks = {} - local save_indexes = {} local idx = 1 - for key, value in pairs(bookmarks) do + for _, value in pairs(bookmarks) do -- Only save bookmarks in upper case keys if string.match(value.on, "%u") then - save_bookmarks[idx] = value - local real_index = _get_real_index(key) - save_indexes[real_index] = idx + save_state[idx] = value idx = idx + 1 end end - - save_state = { bookmarks = save_bookmarks, indexes = save_indexes } end ps.pub_static(10, "bookmarks", save_state) @@ -103,11 +92,10 @@ local save_bookmark = ya.sync(function(state, idx) local folder = Folder:by_kind(Folder.CURRENT) state.bookmarks = state.bookmarks or {} - state.indexes = state.indexes or {} - local _idx = state.indexes[idx] - if _idx == nil then + + local _idx = _get_real_index(idx) + if not _idx then _idx = #state.bookmarks + 1 - state.indexes[idx] = _idx end state.bookmarks[_idx] = { @@ -117,7 +105,7 @@ local save_bookmark = ya.sync(function(state, idx) } if state.persist then - _save_state(state.bookmarks, state.indexes) + _save_state(state.bookmarks) end if state.notify and state.notify.enable then @@ -153,23 +141,17 @@ local delete_bookmark = ya.sync(function(state, idx) end table.remove(state.bookmarks, idx) - -- remove the indexes entry for the bookmark - local real_index = _get_real_index(idx) - if real_index then - table.remove(state.indexes, real_index) - end if state.persist then - _save_state(state.bookmarks, state.indexes) + _save_state(state.bookmarks) end end) local delete_all_bookmarks = ya.sync(function(state) state.bookmarks = nil - state.indexes = nil if state.persist then - _save_state(nil, nil) + _save_state(nil) end if state.notify and state.notify.enable then From 2d96d05a326396d7e350b01569e7fb871cd6bdd4 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 26 Apr 2024 15:31:37 +0100 Subject: [PATCH 12/13] Not saving cursor anymore --- init.lua | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/init.lua b/init.lua index 17fc54f..8527066 100644 --- a/init.lua +++ b/init.lua @@ -33,6 +33,11 @@ local _get_real_index = ya.sync(function(state, idx) return nil end) +local _get_hovered_file = ya.sync(function () + local folder = Folder:by_kind(Folder.CURRENT) + return folder.window[folder.cursor - folder.offset + 1].url +end) + local _load_state = ya.sync(function(state) ps.sub_remote("bookmarks", function(body) if not state.bookmarks and body then @@ -69,18 +74,17 @@ end) local _save_last_directory = ya.sync(function(state) ps.sub("cd", function() - local folder = Folder:by_kind(Folder.CURRENT) + local file_url = _get_hovered_file() state.last_dir = state.curr_dir state.curr_dir = { on = "'", - desc = tostring(folder.cwd), - cursor = folder.cursor, + desc = tostring(file_url), } end) ps.sub("hover", function() - local folder = Folder:by_kind(Folder.CURRENT) - state.curr_dir.cursor = folder.cursor + local file_url = _get_hovered_file() + state.curr_dir.desc = tostring(file_url) end) end) @@ -89,7 +93,7 @@ end) -- ***********************************************/ 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 {} @@ -100,8 +104,7 @@ local save_bookmark = ya.sync(function(state, idx) state.bookmarks[_idx] = { on = SUPPORTED_KEYS[idx].on, - desc = tostring(folder.cwd), - cursor = folder.cursor, + desc = tostring(file_url), } if state.persist then @@ -110,8 +113,8 @@ local save_bookmark = ya.sync(function(state, idx) if state.notify and state.notify.enable then local message = state.notify.message.new - message, _ = message:gsub("", SUPPORTED_KEYS[idx].on) - message, _ = message:gsub("", tostring(folder.cwd)) + message, _ = message:gsub("", state.bookmarks[_idx].on) + message, _ = message:gsub("", state.bookmarks[_idx].desc) _send_notification(message) end end) @@ -185,9 +188,7 @@ return { end if action == "jump" then - ya.manager_emit("cd", { bookmarks[selected].desc }) - ya.manager_emit("arrow", { -99999999 }) - ya.manager_emit("arrow", { bookmarks[selected].cursor }) + ya.manager_emit("reveal", { bookmarks[selected].desc }) elseif action == "delete" then delete_bookmark(selected) end From 3663e13b199f00eac522be13c84a11ccf95c3b55 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Fri, 26 Apr 2024 15:42:32 +0100 Subject: [PATCH 13/13] Fix for when folder.window is nil --- init.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 8527066..aa3196f 100644 --- a/init.lua +++ b/init.lua @@ -33,9 +33,13 @@ local _get_real_index = ya.sync(function(state, idx) return nil end) -local _get_hovered_file = ya.sync(function () +local _get_hovered_file = ya.sync(function() local folder = Folder:by_kind(Folder.CURRENT) - return folder.window[folder.cursor - folder.offset + 1].url + 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)