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 61 additions and 164 deletions

View File

@@ -6,7 +6,7 @@ https://github.com/dedukun/bookmarks.yazi/assets/25795432/9a9fe345-dd06-442e-99f
## Requirements
- [Yazi](https://github.com/sxyazi/yazi) v25.4.8+
- [Yazi](https://github.com/sxyazi/yazi) v25.2.7+
## Features
@@ -18,7 +18,7 @@ https://github.com/dedukun/bookmarks.yazi/assets/25795432/9a9fe345-dd06-442e-99f
## Installation
```sh
ya pkg add dedukun/bookmarks
ya pack -a dedukun/bookmarks
```
## Import/Export bookmarks
@@ -33,23 +33,22 @@ the bookmarks are saved in DDS's state file (`~/.local/state/yazi/.dds` on Linux
Add this to your `keymap.toml`:
```toml
# If your yazi version is lower than v25.5.28, repleace "mgr" by "manager".
[[mgr.prepend_keymap]]
[[manager.prepend_keymap]]
on = [ "m" ]
run = "plugin bookmarks save"
desc = "Save current position as a bookmark"
[[mgr.prepend_keymap]]
[[manager.prepend_keymap]]
on = [ "'" ]
run = "plugin bookmarks jump"
desc = "Jump to a bookmark"
[[mgr.prepend_keymap]]
[[manager.prepend_keymap]]
on = [ "b", "d" ]
run = "plugin bookmarks delete"
desc = "Delete a bookmark"
[[mgr.prepend_keymap]]
[[manager.prepend_keymap]]
on = [ "b", "D" ]
run = "plugin bookmarks delete_all"
desc = "Delete all bookmarks"
@@ -63,12 +62,10 @@ The following are the default configurations:
```lua
-- ~/.config/yazi/init.lua
require("bookmarks"):setup({
last_directory = { enable = false, persist = false, mode="dir" },
last_directory = { enable = false, persist = false, key= "'" },
persist = "none",
desc_format = "full",
file_pick_mode = "hover",
custom_desc_input = false,
show_keys = false,
notify = {
enable = false,
timeout = 1,
@@ -88,13 +85,7 @@ 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` | 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) |
It's also possible to change the trigger key.
### `persist`
@@ -140,19 +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`.
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.
### `show_keys`
When enabled, saving a new bookmark will display a list of all available keys.
If a key already has a saved bookmark, its description will be shown.
This helps prevent accidental overwriting of existing bookmarks.
By default no information is shown.

184
main.lua
View File

@@ -1,18 +1,18 @@
--- @since 25.4.8
--- @since 25.2.7
-- stylua: ignore
local SUPPORTED_KEYS = {
{ on = "0", desc = "Free" }, { on = "1", desc = "Free" }, { on = "2", desc = "Free" }, { on = "3", desc = "Free" }, { on = "4", desc = "Free"},
{ on = "5", desc = "Free" }, { on = "6", desc = "Free" }, { on = "7", desc = "Free" }, { on = "8", desc = "Free" }, { on = "9", desc = "Free"},
{ on = "A", desc = "Free" }, { on = "B", desc = "Free" }, { on = "C", desc = "Free" }, { on = "D", desc = "Free" }, { on = "E", desc = "Free"},
{ on = "F", desc = "Free" }, { on = "G", desc = "Free" }, { on = "H", desc = "Free" }, { on = "I", desc = "Free" }, { on = "J", desc = "Free"},
{ on = "K", desc = "Free" }, { on = "L", desc = "Free" }, { on = "M", desc = "Free" }, { on = "N", desc = "Free" }, { on = "O", desc = "Free"},
{ on = "P", desc = "Free" }, { on = "Q", desc = "Free" }, { on = "R", desc = "Free" }, { on = "S", desc = "Free" }, { on = "T", desc = "Free"},
{ on = "U", desc = "Free" }, { on = "V", desc = "Free" }, { on = "W", desc = "Free" }, { on = "X", desc = "Free" }, { on = "Y", desc = "Free"}, { on = "Z", desc = "Free" },
{ on = "a", desc = "Free" }, { on = "b", desc = "Free" }, { on = "c", desc = "Free" }, { on = "d", desc = "Free" }, { on = "e", desc = "Free"},
{ on = "f", desc = "Free" }, { on = "g", desc = "Free" }, { on = "h", desc = "Free" }, { on = "i", desc = "Free" }, { on = "j", desc = "Free"},
{ on = "k", desc = "Free" }, { on = "l", desc = "Free" }, { on = "m", desc = "Free" }, { on = "n", desc = "Free" }, { on = "o", desc = "Free"},
{ on = "p", desc = "Free" }, { on = "q", desc = "Free" }, { on = "r", desc = "Free" }, { on = "s", desc = "Free" }, { on = "t", desc = "Free"},
{ on = "u", desc = "Free" }, { on = "v", desc = "Free" }, { on = "w", desc = "Free" }, { on = "x", desc = "Free" }, { on = "y", desc = "Free"}, { on = "z", desc = "Free" },
{ on = "0"}, { on = "1"}, { on = "2"}, { on = "3"}, { on = "4"},
{ on = "5"}, { on = "6"}, { on = "7"}, { on = "8"}, { on = "9"},
{ on = "A"}, { on = "B"}, { on = "C"}, { on = "D"}, { on = "E"},
{ on = "F"}, { on = "G"}, { on = "H"}, { on = "I"}, { on = "J"},
{ on = "K"}, { on = "L"}, { on = "M"}, { on = "N"}, { on = "O"},
{ on = "P"}, { on = "Q"}, { on = "R"}, { on = "S"}, { on = "T"},
{ on = "U"}, { on = "V"}, { on = "W"}, { on = "X"}, { on = "Y"}, { on = "Z"},
{ on = "a"}, { on = "b"}, { on = "c"}, { on = "d"}, { on = "e"},
{ on = "f"}, { on = "g"}, { on = "h"}, { on = "i"}, { on = "j"},
{ on = "k"}, { on = "l"}, { on = "m"}, { on = "n"}, { on = "o"},
{ on = "p"}, { on = "q"}, { on = "r"}, { on = "s"}, { on = "t"},
{ on = "u"}, { on = "v"}, { on = "w"}, { on = "x"}, { on = "y"}, { on = "z"},
}
local _send_notification = ya.sync(
@@ -50,7 +50,7 @@ local _generate_description = ya.sync(function(state, file)
end
if state.desc_format == "parent" then
return tostring(file.url.parent)
return tostring(file.url:parent())
end
-- full description
return tostring(file.url)
@@ -90,43 +90,29 @@ local _save_state = ya.sync(function(state, bookmarks)
ps.pub_to(0, "@bookmarks", save_state)
end)
local _load_last = ya.sync(function(state)
ps.sub_remote("@bookmarks-last", function(body)
state.last_dir = body
local _save_last_directory = ya.sync(function(state, persist)
if persist then
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.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
ps.sub("cd", function()
local file = _get_bookmark_file()
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)
if persist and state.last_dir then
ps.pub_to(0, "@bookmarks-lastdir", state.last_dir)
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() _save_last(state.last_persist, false) end)
state.curr_dir = {
on = state.last_directory_key,
desc = _generate_description(file),
path = tostring(file.url),
is_parent = file.is_parent,
}
end)
ps.sub("hover", function()
local file = _get_bookmark_file()
@@ -135,32 +121,11 @@ local save_last_dir = ya.sync(function(state)
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_show_keys_enabled = ya.sync(function(state) return state.show_keys 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 =============**
-- ***********************************************
local get_updated_keys = ya.sync(function(state, keys)
if state.bookmarks then
for _, bookmarks_value in pairs(state.bookmarks) do
for _, keys_value in pairs(keys) do
if keys_value.on == bookmarks_value.on then
keys_value.desc = bookmarks_value.desc
end
end
end
end
return keys
end)
local save_bookmark = ya.sync(function(state, idx, custom_desc)
local save_bookmark = ya.sync(function(state, idx)
local file = _get_bookmark_file()
state.bookmarks = state.bookmarks or {}
@@ -170,14 +135,9 @@ local save_bookmark = ya.sync(function(state, idx, custom_desc)
_idx = #state.bookmarks + 1
end
local bookmark_desc = tostring(_generate_description(file))
if custom_desc then
bookmark_desc = tostring(custom_desc)
end
state.bookmarks[_idx] = {
on = SUPPORTED_KEYS[idx].on,
desc = bookmark_desc,
desc = _generate_description(file),
path = tostring(file.url),
is_parent = file.is_parent,
}
@@ -214,10 +174,6 @@ local save_bookmark = ya.sync(function(state, idx, custom_desc)
message, _ = message:gsub("<folder>", 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)
@@ -263,6 +219,14 @@ local delete_all_bookmarks = ya.sync(function(state)
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 {
entry = function(_, job)
local action = job.args[1]
@@ -271,31 +235,15 @@ return {
end
if action == "save" then
if _is_show_keys_enabled() then
SUPPORTED_KEYS = get_updated_keys(SUPPORTED_KEYS)
end
local key = ya.which { cands = SUPPORTED_KEYS, silent = not _is_show_keys_enabled() }
local key = ya.which { cands = SUPPORTED_KEYS, silent = true }
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)
end
return
end
if action == "delete_all" then
elseif action == "delete_all" then
return delete_all_bookmarks()
elseif action == "jump_last" then
return jump_to_last()
end
local bookmarks = all_bookmarks(action == "jump")
@@ -305,14 +253,10 @@ return {
end
if action == "jump" then
if get_last_mode() == "jump" then
save_last_jump()
end
if bookmarks[selected].is_parent then
ya.mgr_emit("cd", { bookmarks[selected].path })
ya.manager_emit("cd", { bookmarks[selected].path })
else
ya.mgr_emit("reveal", { bookmarks[selected].path })
ya.manager_emit("reveal", { bookmarks[selected].path })
end
elseif action == "delete" then
delete_bookmark(selected)
@@ -325,26 +269,12 @@ return {
if type(args.last_directory) == "table" then
if args.last_directory.enable then
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 == "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()
state.last_directory_key = "'"
if type(args.last_directory.key) == "string" then
state.last_directory_key = args.last_directory.key
end
if args.last_directory.persist then
_load_last()
end
_save_last_directory(args.last_directory.persist)
end
end
@@ -365,14 +295,6 @@ return {
state.file_pick_mode = "hover"
end
if type(args.custom_desc_input) == "boolean" then
state.custom_desc_input = args.custom_desc_input
end
if type(args.show_keys) == "boolean" then
state.show_keys = args.show_keys
end
state.notify = {
enable = false,
timeout = 1,