Revert "refactor: use ya.sync to simplify the code"
This commit is contained in:
37
README.md
37
README.md
@@ -1,44 +1,35 @@
|
|||||||
# bookmarks.yazi
|
# Bookmarks.yazi
|
||||||
|
|
||||||
A [Yazi](https://github.com/sxyazi/yazi) plugin that adds the basic functionality of [vi-like marks](https://neovim.io/doc/user/motion.html#mark-motions).
|
A [Yazi](https://github.com/sxyazi/yazi) plugin that adds the basic functionality of [vi-like marks](https://neovim.io/doc/user/motion.html#mark-motions).
|
||||||
|
|
||||||
> [!NOTE]
|
|
||||||
> The latest main branch of Yazi is required at the moment.
|
|
||||||
|
|
||||||
https://github.com/dedukun/bookmarks.yazi/assets/25795432/9a9fe345-dd06-442e-99f1-8475ab22fad5
|
https://github.com/dedukun/bookmarks.yazi/assets/25795432/9a9fe345-dd06-442e-99f1-8475ab22fad5
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# Linux/macOS
|
|
||||||
git clone https://github.com/dedukun/bookmarks.yazi.git ~/.config/yazi/plugins/bookmarks.yazi
|
|
||||||
|
|
||||||
# Windows
|
|
||||||
git clone https://github.com/dedukun/bookmarks.yazi.git %AppData%\yazi\config\plugins\bookmarks.yazi
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Add this to your `keymap.toml`:
|
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[[manager.prepend_keymap]]
|
[[manager.prepend_keymap]]
|
||||||
on = [ "m" ]
|
on = [ "m" ]
|
||||||
exec = "plugin bookmarks --args=save"
|
exec = "plugin bookmarks --sync --args='set'"
|
||||||
desc = "Save current position as a bookmark"
|
desc = "Set a bookmark"
|
||||||
|
```
|
||||||
|
|
||||||
|
```toml
|
||||||
[[manager.prepend_keymap]]
|
[[manager.prepend_keymap]]
|
||||||
on = [ "'" ]
|
on = [ "'" ]
|
||||||
exec = "plugin bookmarks --args=jump"
|
exec = "plugin bookmarks --sync --args='jump'"
|
||||||
desc = "Jump to a bookmark"
|
desc = "Jump to a bookmark"
|
||||||
|
```
|
||||||
|
|
||||||
|
```toml
|
||||||
[[manager.prepend_keymap]]
|
[[manager.prepend_keymap]]
|
||||||
on = [ "b", "d" ]
|
on = [ "b", "d" ]
|
||||||
exec = "plugin bookmarks --args=delete"
|
exec = "plugin bookmarks --sync --args='delete'"
|
||||||
desc = "Delete a bookmark"
|
desc = "Jump to a bookmark"
|
||||||
|
```
|
||||||
|
|
||||||
|
```toml
|
||||||
[[manager.prepend_keymap]]
|
[[manager.prepend_keymap]]
|
||||||
on = [ "b", "D" ]
|
on = [ "b", "D" ]
|
||||||
exec = "plugin bookmarks --args=delete_all"
|
exec = "plugin bookmarks --sync --args='deleteall'"
|
||||||
desc = "Delete all bookmarks"
|
desc = "Jump to a bookmark"
|
||||||
```
|
```
|
||||||
|
|||||||
118
init.lua
118
init.lua
@@ -14,20 +14,41 @@ local SUPPORTED_KEYS = {
|
|||||||
{ on = "u"}, { on = "v"}, { on = "w"}, { on = "x"}, { on = "y"}, { on = "z"},
|
{ on = "u"}, { on = "v"}, { on = "w"}, { on = "x"}, { on = "y"}, { on = "z"},
|
||||||
}
|
}
|
||||||
|
|
||||||
local save_bookmark = ya.sync(function(idx)
|
local function save_bookmark(idx)
|
||||||
|
if idx == -1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local folder = Folder:by_kind(Folder.CURRENT)
|
||||||
|
|
||||||
|
local key = SUPPORTED_KEYS[idx].on
|
||||||
|
|
||||||
state.bookmarks = state.bookmarks or {}
|
state.bookmarks = state.bookmarks or {}
|
||||||
state.bookmarks[#state.bookmarks + 1] = {
|
state.bookmarks[key] = { cursor = folder.cursor, path = tostring(folder.cwd) }
|
||||||
on = SUPPORTED_KEYS[idx].on,
|
end
|
||||||
desc = tostring(folder.cwd),
|
|
||||||
cursor = Folder:by_kind(Folder.CURRENT).cursor,
|
|
||||||
}
|
|
||||||
end)
|
|
||||||
|
|
||||||
local all_bookmarks = ya.sync(function() return state.bookmarks or {} end)
|
local function jump_to_bookmark(bookmark)
|
||||||
|
state.bookmarks = state.bookmarks or {}
|
||||||
|
|
||||||
local delete_bookmark = ya.sync(function(idx) table.remove(state.bookmarks, idx) end)
|
local selected_bookmark = state.bookmarks[bookmark]
|
||||||
|
|
||||||
local delete_all_bookmarks = ya.sync(function() state.bookmarks = nil end)
|
ya.manager_emit("cd", { selected_bookmark.path })
|
||||||
|
ya.manager_emit("arrow", { -99999999 })
|
||||||
|
ya.manager_emit("arrow", { selected_bookmark.cursor })
|
||||||
|
end
|
||||||
|
|
||||||
|
local function delete_bookmark(bookmark)
|
||||||
|
state.bookmarks = state.bookmarks or {}
|
||||||
|
state.bookmarks[bookmark] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local function delete_all_bookmarks()
|
||||||
|
state.bookmarks = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local function next(sync, args)
|
||||||
|
ya.manager_emit("plugin", { "bookmarks", sync = sync, args = table.concat(args, " ") })
|
||||||
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
entry = function(_, args)
|
entry = function(_, args)
|
||||||
@@ -36,30 +57,67 @@ return {
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if action == "save" then
|
if action == "set" then
|
||||||
local key = ya.which { cands = SUPPORTED_KEYS, silent = true }
|
local key = args[2]
|
||||||
if key then
|
if not key then
|
||||||
save_bookmark(key)
|
next(false, { "_set" })
|
||||||
|
else
|
||||||
|
save_bookmark(tonumber(key))
|
||||||
end
|
end
|
||||||
return
|
elseif action == "_set" then
|
||||||
end
|
local key = ya.which({
|
||||||
|
cands = SUPPORTED_KEYS,
|
||||||
|
silent = true,
|
||||||
|
})
|
||||||
|
|
||||||
if action == "delete_all" then
|
if not key then
|
||||||
return delete_all_bookmarks()
|
-- selection was cancelled
|
||||||
end
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local bookmarks = all_bookmarks()
|
next(true, { "set", key })
|
||||||
local selected = #bookmarks > 0 and ya.which { cands = bookmarks }
|
elseif action == "jump" or action == "delete" then
|
||||||
if not selected then
|
local bookmark = args[2]
|
||||||
return
|
if not bookmark then
|
||||||
end
|
-- tried to use ya.sync but was unsuccessful, doing this way for the moment
|
||||||
|
if state.bookmarks then
|
||||||
|
local arguments = { "_" .. action }
|
||||||
|
for k, _ in pairs(state.bookmarks) do
|
||||||
|
table.insert(arguments, k)
|
||||||
|
table.insert(arguments, state.bookmarks[k].path)
|
||||||
|
end
|
||||||
|
next(false, arguments)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if action == "jump" then
|
||||||
|
jump_to_bookmark(bookmark)
|
||||||
|
elseif action == "delete" then
|
||||||
|
delete_bookmark(bookmark)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif action == "_jump" or action == "_delete" then
|
||||||
|
if #args == 1 then
|
||||||
|
-- Should never enter here, but just to be safe
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
if action == "jump" then
|
local marked_keys = {}
|
||||||
ya.manager_emit("cd", { bookmarks[selected].desc })
|
for i = 2, #args, 2 do
|
||||||
ya.manager_emit("arrow", { -99999999 })
|
table.insert(marked_keys, { on = args[i], desc = args[i + 1] })
|
||||||
ya.manager_emit("arrow", { bookmarks[selected].cursor })
|
end
|
||||||
elseif action == "delete" then
|
|
||||||
delete_bookmark(selected)
|
local selected_bookmark = ya.which({
|
||||||
|
cands = marked_keys,
|
||||||
|
})
|
||||||
|
|
||||||
|
if not selected_bookmark then
|
||||||
|
-- selection was cancelled
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
next(true, { string.sub(action, 2), marked_keys[selected_bookmark].on })
|
||||||
|
elseif action == "deleteall" then
|
||||||
|
delete_all_bookmarks()
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user