Files
bookmarks.yazi/init.lua
2024-03-12 14:07:53 +00:00

97 lines
2.9 KiB
Lua

-- stylua: ignore
local SUPPORTED_KEYS = {
{ 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 save_bookmark = ya.sync(function(state, idx)
local folder = Folder:by_kind(Folder.CURRENT)
state.bookmarks = state.bookmarks or {}
state.bookmarks[#state.bookmarks + 1] = {
on = SUPPORTED_KEYS[idx].on,
desc = tostring(folder.cwd),
cursor = folder.cursor,
}
if state.notify.enable then
local description = state.notify.format
description, _ = description:gsub("<key>", SUPPORTED_KEYS[idx].on)
description, _ = description:gsub("<folder>", tostring(folder.cwd))
ya.notify {
title = "Bookmarks",
content = description,
timeout = state.notify.timeout,
}
end
end)
local all_bookmarks = ya.sync(function(state) return state.bookmarks or {} end)
local delete_bookmark = ya.sync(function(state, idx) table.remove(state.bookmarks, idx) end)
local delete_all_bookmarks = ya.sync(function(state) state.bookmarks = nil end)
return {
entry = function(_, args)
local action = args[1]
if not action then
return
end
if action == "save" then
local key = ya.which { cands = SUPPORTED_KEYS, silent = true }
if key then
save_bookmark(key)
end
return
end
if action == "delete_all" then
return delete_all_bookmarks()
end
local bookmarks = all_bookmarks()
local selected = #bookmarks > 0 and ya.which { cands = bookmarks }
if not selected then
return
end
if action == "jump" then
ya.manager_emit("cd", { bookmarks[selected].desc })
ya.manager_emit("arrow", { -99999999 })
ya.manager_emit("arrow", { bookmarks[selected].cursor })
elseif action == "delete" then
delete_bookmark(selected)
end
end,
setup = function(state, args)
if not args then
return
end
state.notify = { enable = false, timeout = 2, format = "New bookmark in '<key>' -> '<folder>'" }
if args.notify ~= nil then
if type(args.notify.enable) == "boolean" then
state.notify.enable = args.notify.enable
end
if type(args.notify.timeout) == "number" then
state.notify.timeout = args.notify.timeout
end
if type(args.notify.format) == "string" then
state.notify.format = args.notify.format
end
end
end,
}