2 Commits

Author SHA1 Message Date
Diogo Duarte
e45dffc27a Update README 2025-02-18 23:13:40 +00:00
Diogo Duarte
0e772b951b Add custom keybinding for last + added 'jump' command 2025-02-18 23:03:00 +00:00
2 changed files with 38 additions and 107 deletions

View File

@@ -62,11 +62,10 @@ The following are the default configurations:
```lua ```lua
-- ~/.config/yazi/init.lua -- ~/.config/yazi/init.lua
require("bookmarks"):setup({ require("bookmarks"):setup({
last_directory = { enable = false, persist = false, mode="dir" }, last_directory = { enable = false, persist = false, key= "'" },
persist = "none", persist = "none",
desc_format = "full", desc_format = "full",
file_pick_mode = "hover", file_pick_mode = "hover",
custom_desc_input = false,
notify = { notify = {
enable = false, enable = false,
timeout = 1, timeout = 1,
@@ -86,13 +85,7 @@ the last directory.
There's also the option to enable persistence to this automatic bookmark. There's also the option to enable persistence to this automatic bookmark.
Finally, there's a `mode` option with the following options: It's also possible to change the trigger key.
| 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` ### `persist`
@@ -138,9 +131,3 @@ By default the notification has a 1 second timeout that can be changed with `not
Furthermore, you can customize the notification messages with `notify.message`. Furthermore, you can customize the notification messages with `notify.message`.
For the `new` and `delete` messages, the `<key>` and `<folder>` keywords can be used, which will be replaced by the respective new/deleted bookmark's associated key and folder. For the `new` and `delete` messages, the `<key>` and `<folder>` keywords can be used, which will be replaced by the respective new/deleted bookmark's associated key and folder.
### `custom_desc_input`
When enabled, user can change description for new bookmark before it is saved.
By default the custom description input is filled with path.

128
main.lua
View File

@@ -90,43 +90,29 @@ local _save_state = ya.sync(function(state, bookmarks)
ps.pub_to(0, "@bookmarks", save_state) ps.pub_to(0, "@bookmarks", save_state)
end) end)
local _load_last = ya.sync(function(state) local _save_last_directory = ya.sync(function(state, persist)
ps.sub_remote("@bookmarks-last", function(body) if persist then
state.last_dir = body ps.sub_remote("@bookmarks-lastdir", function(body)
state.curr_dir = body
state.curr_dir.on = state.last_directory_key
end)
end
if state.last_mode ~= "dir" then ps.sub("cd", function()
ps.unsub_remote("@bookmarks-last") local file = _get_bookmark_file()
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.last_dir = state.curr_dir
state.curr_dir = curr
end
if persist and state.last_dir then if persist and state.last_dir then
ps.pub_to(0, "@bookmarks-last", state.last_dir) ps.pub_to(0, "@bookmarks-lastdir", state.last_dir)
end end
end)
local get_last_mode = ya.sync(function(state) return state.last_mode end) state.curr_dir = {
on = state.last_directory_key,
local save_last_dir = ya.sync(function(state) desc = _generate_description(file),
ps.sub("cd", function() _save_last(state.last_persist, false) end) path = tostring(file.url),
is_parent = file.is_parent,
}
end)
ps.sub("hover", function() ps.sub("hover", function()
local file = _get_bookmark_file() local file = _get_bookmark_file()
@@ -135,17 +121,11 @@ local save_last_dir = ya.sync(function(state)
end) end)
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)
local _is_custom_desc_input_enabled = ya.sync(function(state) return state.custom_desc_input end)
-- *********************************************** -- ***********************************************
-- **============= C O M M A N D S =============** -- **============= C O M M A N D S =============**
-- *********************************************** -- ***********************************************
local save_bookmark = ya.sync(function(state, idx, custom_desc) local save_bookmark = ya.sync(function(state, idx)
local file = _get_bookmark_file() local file = _get_bookmark_file()
state.bookmarks = state.bookmarks or {} state.bookmarks = state.bookmarks or {}
@@ -155,14 +135,9 @@ local save_bookmark = ya.sync(function(state, idx, custom_desc)
_idx = #state.bookmarks + 1 _idx = #state.bookmarks + 1
end end
local bookmark_desc = tostring(file.url)
if custom_desc then
bookmark_desc = tostring(custom_desc)
end
state.bookmarks[_idx] = { state.bookmarks[_idx] = {
on = SUPPORTED_KEYS[idx].on, on = SUPPORTED_KEYS[idx].on,
desc = bookmark_desc, desc = _generate_description(file),
path = tostring(file.url), path = tostring(file.url),
is_parent = file.is_parent, is_parent = file.is_parent,
} }
@@ -199,10 +174,6 @@ local save_bookmark = ya.sync(function(state, idx, custom_desc)
message, _ = message:gsub("<folder>", state.bookmarks[_idx].desc) message, _ = message:gsub("<folder>", state.bookmarks[_idx].desc)
_send_notification(message) _send_notification(message)
end end
if get_last_mode() == "mark" then
save_last_mark()
end
end) end)
local all_bookmarks = ya.sync(function(state, append_last_dir) local all_bookmarks = ya.sync(function(state, append_last_dir)
@@ -248,6 +219,14 @@ local delete_all_bookmarks = ya.sync(function(state)
end end
end) end)
local jump_to_last = ya.sync(function(state)
if state.last_dir.is_parent then
ya.manager_emit("cd", { state.last_dir.is_parent })
else
ya.manager_emit("reveal", { state.last_dir.is_parent })
end
end)
return { return {
entry = function(_, job) entry = function(_, job)
local action = job.args[1] local action = job.args[1]
@@ -258,26 +237,13 @@ return {
if action == "save" then if action == "save" then
local key = ya.which { cands = SUPPORTED_KEYS, silent = true } local key = ya.which { cands = SUPPORTED_KEYS, silent = true }
if key then if key then
if _is_custom_desc_input_enabled() then
local value, event = ya.input {
title = "Save with custom description:",
position = { "top-center", y = 3, w = 60 },
value = tostring(_get_bookmark_file().url),
}
if event ~= 1 then
return
end
save_bookmark(key, value)
return
end
save_bookmark(key) save_bookmark(key)
end end
return return
end elseif action == "delete_all" then
if action == "delete_all" then
return delete_all_bookmarks() return delete_all_bookmarks()
elseif action == "jump_last" then
return jump_to_last()
end end
local bookmarks = all_bookmarks(action == "jump") local bookmarks = all_bookmarks(action == "jump")
@@ -287,10 +253,6 @@ return {
end end
if action == "jump" then if action == "jump" then
if get_last_mode() == "jump" then
save_last_jump()
end
if bookmarks[selected].is_parent then if bookmarks[selected].is_parent then
ya.manager_emit("cd", { bookmarks[selected].path }) ya.manager_emit("cd", { bookmarks[selected].path })
else else
@@ -307,26 +269,12 @@ return {
if type(args.last_directory) == "table" then if type(args.last_directory) == "table" then
if args.last_directory.enable then if args.last_directory.enable then
if args.last_directory.mode == "mark" then state.last_directory_key = "'"
state.last_persist = args.last_directory.persist if type(args.last_directory.key) == "string" then
state.last_mode = "mark" state.last_directory_key = args.last_directory.key
elseif args.last_directory.mode == "jump" then
state.last_persist = args.last_directory.persist
state.last_mode = "jump"
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 = "dir"
save_last_dir()
end end
if args.last_directory.persist then _save_last_directory(args.last_directory.persist)
_load_last()
end
end end
end end
@@ -347,10 +295,6 @@ return {
state.file_pick_mode = "hover" state.file_pick_mode = "hover"
end end
if type(args.custom_desc_input) == "boolean" then
state.custom_desc_input = args.custom_desc_input
end
state.notify = { state.notify = {
enable = false, enable = false,
timeout = 1, timeout = 1,