Rebase
This commit is contained in:
116
init.lua
116
init.lua
@@ -14,41 +14,22 @@ 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 function save_bookmark(idx)
|
local save_bookmark = ya.sync(function(idx)
|
||||||
if idx == -1 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local folder = Folder:by_kind(Folder.CURRENT)
|
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[key] = { cursor = folder.cursor, path = tostring(folder.cwd) }
|
state.bookmarks[#state.bookmarks + 1] = {
|
||||||
end
|
on = SUPPORTED_KEYS[idx].on,
|
||||||
|
desc = tostring(folder.cwd),
|
||||||
|
cursor = folder.cursor,
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
|
||||||
local function jump_to_bookmark(bookmark)
|
local all_bookmarks = ya.sync(function() return state.bookmarks or {} end)
|
||||||
state.bookmarks = state.bookmarks or {}
|
|
||||||
|
|
||||||
local selected_bookmark = state.bookmarks[bookmark]
|
local delete_bookmark = ya.sync(function(idx) table.remove(state.bookmarks, idx) end)
|
||||||
|
|
||||||
ya.manager_emit("cd", { selected_bookmark.path })
|
local delete_all_bookmarks = ya.sync(function() state.bookmarks = nil end)
|
||||||
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)
|
||||||
@@ -57,67 +38,30 @@ return {
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if action == "set" then
|
if action == "save" then
|
||||||
local key = args[2]
|
local key = ya.which { cands = SUPPORTED_KEYS, silent = true }
|
||||||
if not key then
|
if key then
|
||||||
next(false, { "_set" })
|
save_bookmark(key)
|
||||||
else
|
|
||||||
save_bookmark(tonumber(key))
|
|
||||||
end
|
end
|
||||||
elseif action == "_set" then
|
return
|
||||||
local key = ya.which({
|
end
|
||||||
cands = SUPPORTED_KEYS,
|
|
||||||
silent = true,
|
|
||||||
})
|
|
||||||
|
|
||||||
if not key then
|
if action == "delete_all" then
|
||||||
-- selection was cancelled
|
return delete_all_bookmarks()
|
||||||
return
|
end
|
||||||
end
|
|
||||||
|
|
||||||
next(true, { "set", key })
|
local bookmarks = all_bookmarks()
|
||||||
elseif action == "jump" or action == "delete" then
|
local selected = #bookmarks > 0 and ya.which { cands = bookmarks }
|
||||||
local bookmark = args[2]
|
if not selected then
|
||||||
if not bookmark then
|
return
|
||||||
-- tried to use ya.sync but was unsuccessful, doing this way for the moment
|
end
|
||||||
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
|
|
||||||
|
|
||||||
local marked_keys = {}
|
if action == "jump" then
|
||||||
for i = 2, #args, 2 do
|
ya.manager_emit("cd", { bookmarks[selected].desc })
|
||||||
table.insert(marked_keys, { on = args[i], desc = args[i + 1] })
|
ya.manager_emit("arrow", { -99999999 })
|
||||||
end
|
ya.manager_emit("arrow", { bookmarks[selected].cursor })
|
||||||
|
elseif action == "delete" then
|
||||||
local selected_bookmark = ya.which({
|
delete_bookmark(selected)
|
||||||
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