From 023bde5a532b986e682bbf9b8f8f6e70cb58ac0a Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Tue, 18 Feb 2025 11:07:42 +0000 Subject: [PATCH 1/4] Add last modes --- README.md | 14 +++++-- init.lua => main.lua | 87 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 81 insertions(+), 20 deletions(-) rename init.lua => main.lua (81%) diff --git a/README.md b/README.md index 6b6cc09..237659b 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,10 @@ ya pack -a dedukun/bookmarks ## Import/Export bookmarks -This plugin uses [Yazi's DDS](https://yazi-rs.github.io/docs/dds/) for bookmark persistence, as such, +This plugin uses [Yazi's DDS](https://yazi-rs.github.io/docs/dds/) for bookmark persistence, as such, the bookmarks are saved in DDS's state file (`~/.local/state/yazi/.dds` on Linux and `C:\Users\USERNAME\AppData\Roaming\yazi\state\.dds` on Windows) -***NOTE:*** This system may be used by other plugins that you have installed, so this file might have more data than just the bookmarks. +**_NOTE:_** This system may be used by other plugins that you have installed, so this file might have more data than just the bookmarks. ## Configuration @@ -62,7 +62,7 @@ The following are the default configurations: ```lua -- ~/.config/yazi/init.lua require("bookmarks"):setup({ - last_directory = { enable = false, persist = false }, + last_directory = { enable = false, persist = false, mode="jump" }, persist = "none", desc_format = "full", file_pick_mode = "hover", @@ -85,6 +85,14 @@ the last directory. There's also the option to enable persistence to this automatic bookmark. +Finally, there's a `mode` option with the following options: + +| Value | Description | +| ------ | --------------------------------------------------- | +| `jump` | Default, It saves the last used mark | +| `mark` | It saves the last created mark | +| `dir` | It saves the last visited directory (old behaviour) | + ### `persist` When enabled the bookmarks will persist, i.e. if you close and reopen Yazi they will still be diff --git a/init.lua b/main.lua similarity index 81% rename from init.lua rename to main.lua index f8c97f8..d0dc75f 100644 --- a/init.lua +++ b/main.lua @@ -89,25 +89,44 @@ local _save_state = ya.sync(function(state, bookmarks) ps.pub_to(0, "@bookmarks", save_state) end) -local _save_last_directory = ya.sync(function(state, persist) - if persist then - ps.sub_remote("@bookmarks-lastdir", function(body) state.curr_dir = body end) +local _load_last = ya.sync(function(state) + ps.sub_remote("@bookmarks-last", function(body) + state.last_dir = body + + if state.last_mode ~= "dir" then + ps.unsub_remote("@bookmarks-last") + end + end) +end) + +local _save_last = ya.sync(function(state, persist, imediate) + local file = _get_bookmark_file() + + local curr = { + on = "'", + desc = _generate_description(file), + path = tostring(file.url), + is_parent = file.is_parent, + } + + if imediate then + state.curr_dir = nil + state.last_dir = curr + else + state.last_dir = state.curr_dir + state.curr_dir = curr end + if persist and state.last_dir then + ps.pub_to(0, "@bookmarks-last", state.last_dir) + end +end) + +local get_last_mode = ya.sync(function(state) return state.last_mode end) + +local save_last_dir = ya.sync(function(state) ps.sub("cd", function() - local file = _get_bookmark_file() - state.last_dir = state.curr_dir - - if persist and state.last_dir then - ps.pub_to(0, "@bookmarks-lastdir", state.last_dir) - end - - state.curr_dir = { - on = "'", - desc = _generate_description(file), - path = tostring(file.url), - is_parent = file.is_parent, - } + _save_last(state.last_persist, false) end) ps.sub("hover", function() @@ -117,6 +136,14 @@ local _save_last_directory = ya.sync(function(state, persist) end) end) +local save_last_jump = ya.sync(function(state) + _save_last(state.last_persist, true) +end) + +local save_last_mark = ya.sync(function(state) + _save_last(state.last_persist, true) +end) + -- *********************************************** -- **============= C O M M A N D S =============** -- *********************************************** @@ -170,6 +197,10 @@ local save_bookmark = ya.sync(function(state, idx) message, _ = message:gsub("", state.bookmarks[_idx].desc) _send_notification(message) end + + if get_last_mode() == "mark" then + save_last_mark() + end end) local all_bookmarks = ya.sync(function(state, append_last_dir) @@ -246,6 +277,10 @@ return { else ya.manager_emit("reveal", { bookmarks[selected].path }) end + + if get_last_mode() == "jump" then + save_last_jump() + end elseif action == "delete" then delete_bookmark(selected) end @@ -257,7 +292,25 @@ return { if type(args.last_directory) == "table" then if args.last_directory.enable then - _save_last_directory(args.last_directory.persist) + if args.last_directory.mode == "mark" then + state.last_persist = args.last_directory.persist + state.last_mode = "mark" + elseif args.last_directory.mode == "jump" then + state.last_persist = args.last_directory.persist + state.last_mode = "jump" + elseif args.last_directory.mode == "jump" then + state.last_persist = args.last_directory.persist + state.last_mode = "dir" + save_last_dir() + else + -- default + state.last_persist = args.last_directory.persist + state.last_mode = "jump" + end + + if args.last_directory.persist then + _load_last() + end end end From e2614a18fca38f8254e2c1b05624c4bd897bfeb1 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Sun, 23 Feb 2025 22:30:33 +0000 Subject: [PATCH 2/4] Save last_dir before jump --- main.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.lua b/main.lua index d0dc75f..aefb136 100644 --- a/main.lua +++ b/main.lua @@ -272,15 +272,15 @@ return { end if action == "jump" then + if get_last_mode() == "jump" then + save_last_jump() + end + if bookmarks[selected].is_parent then ya.manager_emit("cd", { bookmarks[selected].path }) else ya.manager_emit("reveal", { bookmarks[selected].path }) end - - if get_last_mode() == "jump" then - save_last_jump() - end elseif action == "delete" then delete_bookmark(selected) end From 7a2f62fbb3a7217c382f41bf47d3ccc3559bb214 Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Wed, 26 Feb 2025 10:23:59 +0000 Subject: [PATCH 3/4] Change default mode to 'dir' --- README.md | 10 +++++----- main.lua | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 45e3d19..3f1fcb9 100644 --- a/README.md +++ b/README.md @@ -88,11 +88,11 @@ There's also the option to enable persistence to this automatic bookmark. Finally, there's a `mode` option with the following options: -| Value | Description | -| ------ | --------------------------------------------------- | -| `jump` | Default, It saves the last used mark | -| `mark` | It saves the last created mark | -| `dir` | It saves the last visited directory (old behaviour) | +| Value | Description | +| ------ | ------------------------------------------------------------ | +| `jump` | It saves the position before the last used mark | +| `mark` | It saves the last created mark | +| `dir` | Default, it saves the last visited directory (old behaviour) | ### `persist` diff --git a/main.lua b/main.lua index d5fde92..1b193b6 100644 --- a/main.lua +++ b/main.lua @@ -313,14 +313,15 @@ return { elseif args.last_directory.mode == "jump" then state.last_persist = args.last_directory.persist state.last_mode = "jump" - elseif args.last_directory.mode == "jump" then + elseif args.last_directory.mode == "dir" then state.last_persist = args.last_directory.persist state.last_mode = "dir" save_last_dir() else -- default state.last_persist = args.last_directory.persist - state.last_mode = "jump" + state.last_mode = "dir" + save_last_dir() end if args.last_directory.persist then From 430592ba57b1a31a1f5d272a8ce864292863ef8b Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Wed, 26 Feb 2025 10:25:33 +0000 Subject: [PATCH 4/4] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3f1fcb9..8b436ef 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ The following are the default configurations: ```lua -- ~/.config/yazi/init.lua require("bookmarks"):setup({ - last_directory = { enable = false, persist = false, mode="jump" }, + last_directory = { enable = false, persist = false, mode="dir" }, persist = "none", desc_format = "full", file_pick_mode = "hover",