fix: add saved event for save_last_and_quit function

This commit is contained in:
MasouShizuka
2025-06-14 17:35:42 +08:00
parent 696214ca62
commit 4bd79b3f94
2 changed files with 15 additions and 4 deletions

View File

@@ -90,7 +90,9 @@ The following are the default configurations:
require("projects"):setup({
save = {
method = "yazi", -- yazi | lua
lua_save_path = "", -- comment out to get the default value
yazi_load_event = "projects-load", -- event name when loading projects in `yazi` method
lua_save_path = "", -- saved file path in `lua` method, comment out or assign explicitly
-- default value:
-- windows: "%APPDATA%/yazi/state/projects.json"
-- unix: "~/.local/state/yazi/projects.json"
},

View File

@@ -517,12 +517,16 @@ local SUPPORTED_KEYS = {
local _load_config = ya.sync(function(state, opts)
state.save = {
method = "yazi",
yazi_load_event = "projects-load",
lua_save_path = "",
}
if type(opts.save) == "table" then
if type(opts.save.method) == "string" then
state.save.method = opts.save.method
end
if type(opts.save.yazi_load_event) == "string" then
state.save.yazi_load_event = opts.save.yazi_load_event
end
if type(opts.save.lua_save_path) == "string" then
state.save.lua_save_path = opts.save.lua_save_path
else
@@ -718,7 +722,7 @@ local _save_projects = ya.sync(function(state, projects)
state.projects = projects
if state.save.method == "yazi" then
ps.pub_to(0, "@projects", projects)
ps.pub_to(0, state.save.yazi_load_event, projects)
elseif state.save.method == "lua" then
local f = io.open(state.save.lua_save_path, "w")
if not f then
@@ -804,7 +808,7 @@ end)
local _load_projects = ya.sync(function(state)
if state.save.method == "yazi" then
ps.sub_remote("@projects", function(body)
ps.sub_remote(state.save.yazi_load_event, function(body)
state.projects = body
end)
elseif state.save.method == "lua" then
@@ -861,10 +865,15 @@ end)
local save_last_and_quit = ya.sync(function(state)
local projects = _get_projects()
projects.last = _get_current_project()
local current_project = _get_current_project()
projects.last = current_project
_save_projects(projects)
if state.event.save.enable then
ps.pub_to(0, state.event.save.name, current_project)
end
ya.emit("quit", {})
end)