Merge pull request #11 from dedukun/save_last_dir

Add `save_last_directory` which allows users to use `''` to go to last visited directory
This commit is contained in:
Diogo Duarte
2024-04-01 20:40:42 +01:00
committed by GitHub
2 changed files with 59 additions and 4 deletions

View File

@@ -45,12 +45,13 @@ desc = "Delete all bookmarks"
--- ---
Additionally you can enable notifications by calling the plugin's `setup` function in Yazi's `init.lua`, i.e. `~/.config/yazi/init.lua`. Additionally there are configurations that can be done using the plugin's `setup` function in Yazi's `init.lua`, i.e. `~/.config/yazi/init.lua`.
The following are the default configurations: The following are the default configurations:
```lua ```lua
-- ~/.config/yazi/init.lua -- ~/.config/yazi/init.lua
require("bookmarks"):setup({ require("bookmarks"):setup({
save_last_directory = false,
notify = { notify = {
enable = false, enable = false,
timeout = 1, timeout = 1,
@@ -63,4 +64,17 @@ require("bookmarks"):setup({
}) })
``` ```
For the `new` and `delete` messages, you can use `<key>` and `<folder>`, which will be replaced by the respective new/deleted bookmark's associated key and folder. ### `save_last_directory`
When enabled, a new bookmark is automatically created in `''` which allows the user to jump back to
the last directory.
### `notify`
When enabled, notifications will be shown when the user creates a new bookmark and deletes one or
all saved bookmarks.
By default the notification has a 1 second timeout that can be changed with `notify.timeout`.
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.

View File

@@ -43,7 +43,21 @@ local save_bookmark = ya.sync(function(state, idx)
end end
end) end)
local all_bookmarks = ya.sync(function(state) return state.bookmarks or {} end) local all_bookmarks = ya.sync(function(state, append_last_dir)
local bookmarks = {}
if state.bookmarks then
for _, value in pairs(state.bookmarks) do
table.insert(bookmarks, value)
end
end
if append_last_dir and state.last_dir then
table.insert(bookmarks, state.last_dir)
end
return bookmarks
end)
local delete_bookmark = ya.sync(function(state, idx) local delete_bookmark = ya.sync(function(state, idx)
if state.notify and state.notify.enable then if state.notify and state.notify.enable then
@@ -72,6 +86,29 @@ local delete_all_bookmarks = ya.sync(function(state)
end end
end) end)
local _save_last_directory = ya.sync(function(state)
state.curr_dir = {
on = "'",
desc = "~",
cursor = 0,
}
ps.sub("cd", function()
local folder = Folder:by_kind(Folder.CURRENT)
state.last_dir = state.curr_dir
state.curr_dir = {
on = "'",
desc = tostring(folder.cwd),
cursor = folder.cursor,
}
end)
ps.sub("hover", function()
local folder = Folder:by_kind(Folder.CURRENT)
state.curr_dir.cursor = folder.cursor
end)
end)
return { return {
entry = function(_, args) entry = function(_, args)
local action = args[1] local action = args[1]
@@ -91,7 +128,7 @@ return {
return delete_all_bookmarks() return delete_all_bookmarks()
end end
local bookmarks = all_bookmarks() local bookmarks = all_bookmarks(action == "jump")
local selected = #bookmarks > 0 and ya.which { cands = bookmarks } local selected = #bookmarks > 0 and ya.which { cands = bookmarks }
if not selected then if not selected then
return return
@@ -110,6 +147,10 @@ return {
return return
end end
if args.save_last_directory then
_save_last_directory()
end
state.notify = { state.notify = {
enable = false, enable = false,
timeout = 1, timeout = 1,