Merge pull request #24 from nralbrecht/bookmark_file_pick_mode

Add option to disable using hovered directory
This commit is contained in:
Diogo Duarte
2024-08-30 20:53:47 +01:00
committed by GitHub
2 changed files with 34 additions and 9 deletions

View File

@@ -85,6 +85,7 @@ require("bookmarks"):setup({
last_directory = { enable = false, persist = false }, last_directory = { enable = false, persist = false },
persist = "none", persist = "none",
desc_format = "full", desc_format = "full",
file_pick_mode = "hover",
notify = { notify = {
enable = false, enable = false,
timeout = 1, timeout = 1,
@@ -135,6 +136,17 @@ There are two possible values for this option:
| `full` | The default, it shows the full path of the bookmark, i.e., the parent folder + the hovered file | | `full` | The default, it shows the full path of the bookmark, i.e., the parent folder + the hovered file |
| `parent` | Only shows the parent folder of the bookmark | | `parent` | Only shows the parent folder of the bookmark |
### `file_pick_mode`
The mode for choosing which directory to bookmark.
There are two possible values for this option:
| Value | Description |
| -------- | ----------------------------------------------------------------------------------------------- |
| `hover` | The default, it uses the path of the hovered file for new bookmarks |
| `parent` | Uses the path of the parent folder for new bookmarks |
### `notify` ### `notify`
When enabled, notifications will be shown when the user creates a new bookmark and deletes one or When enabled, notifications will be shown when the user creates a new bookmark and deletes one or

View File

@@ -33,17 +33,18 @@ local _get_real_index = ya.sync(function(state, idx)
return nil return nil
end) end)
local _get_hovered_file = ya.sync(function() local _get_bookmark_file = ya.sync(function(state)
local folder = cx.active.current local folder = cx.active.current
if not folder.hovered then
return { url = folder.cwd, is_cwd = true } if state.file_pick_mode == "parent" or not folder.hovered then
return { url = folder.cwd, is_parent = true }
end end
return { url = folder.hovered.url, is_cwd = false } return { url = folder.hovered.url, is_parent = false }
end) end)
local _generate_description = ya.sync(function(state, file) local _generate_description = ya.sync(function(state, file)
-- if this is true, we don't have information about the folder, so just return the folder url -- if this is true, we don't have information about the folder, so just return the folder url
if file.is_cwd then if file.is_parent then
return tostring(file.url) return tostring(file.url)
end end
@@ -94,7 +95,7 @@ local _save_last_directory = ya.sync(function(state, persist)
end end
ps.sub("cd", function() ps.sub("cd", function()
local file = _get_hovered_file() local file = _get_bookmark_file()
state.last_dir = state.curr_dir state.last_dir = state.curr_dir
if persist and state.last_dir then if persist and state.last_dir then
@@ -105,11 +106,12 @@ local _save_last_directory = ya.sync(function(state, persist)
on = "'", on = "'",
desc = _generate_description(file), desc = _generate_description(file),
path = tostring(file.url), path = tostring(file.url),
is_parent = file.is_parent,
} }
end) end)
ps.sub("hover", function() ps.sub("hover", function()
local file = _get_hovered_file() local file = _get_bookmark_file()
state.curr_dir.desc = _generate_description(file) state.curr_dir.desc = _generate_description(file)
state.curr_dir.path = tostring(file.url) state.curr_dir.path = tostring(file.url)
end) end)
@@ -120,7 +122,7 @@ end)
-- *********************************************** -- ***********************************************
local save_bookmark = ya.sync(function(state, idx) local save_bookmark = ya.sync(function(state, idx)
local file = _get_hovered_file() local file = _get_bookmark_file()
state.bookmarks = state.bookmarks or {} state.bookmarks = state.bookmarks or {}
@@ -133,6 +135,7 @@ local save_bookmark = ya.sync(function(state, idx)
on = SUPPORTED_KEYS[idx].on, on = SUPPORTED_KEYS[idx].on,
desc = _generate_description(file), desc = _generate_description(file),
path = tostring(file.url), path = tostring(file.url),
is_parent = file.is_parent,
} }
if state.persist then if state.persist then
@@ -216,7 +219,11 @@ return {
end end
if action == "jump" then if action == "jump" then
ya.manager_emit("reveal", { bookmarks[selected].path }) if bookmarks[selected].is_parent then
ya.manager_emit("cd", { bookmarks[selected].path })
else
ya.manager_emit("reveal", { bookmarks[selected].path })
end
elseif action == "delete" then elseif action == "delete" then
delete_bookmark(selected) delete_bookmark(selected)
end end
@@ -246,6 +253,12 @@ return {
state.desc_format = "full" state.desc_format = "full"
end end
if args.file_pick_mode == "parent" then
state.file_pick_mode = "parent"
else
state.file_pick_mode = "hover"
end
state.notify = { state.notify = {
enable = false, enable = false,
timeout = 1, timeout = 1,