From 023bde5a532b986e682bbf9b8f8f6e70cb58ac0a Mon Sep 17 00:00:00 2001 From: Diogo Duarte Date: Tue, 18 Feb 2025 11:07:42 +0000 Subject: [PATCH] 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