feat: add feature of loading last project after starting

This commit is contained in:
MasouShizuka
2024-11-19 08:34:19 +08:00
parent 7a1dc3729f
commit 5994bc9862
2 changed files with 52 additions and 36 deletions

View File

@@ -4,7 +4,7 @@ A [Yazi](https://github.com/sxyazi/yazi) plugin that adds the functionality to s
A project means all `tabs` and their status, including `cwd` and so on. A project means all `tabs` and their status, including `cwd` and so on.
> [!NOTE] > [!NOTE]
> The latest main branch of Yazi is required at the moment. > The latest release of Yazi is required at the moment.
https://github.com/MasouShizuka/projects.yazi/assets/44764707/79c3559a-7776-48cd-8317-dd1478314eed https://github.com/MasouShizuka/projects.yazi/assets/44764707/79c3559a-7776-48cd-8317-dd1478314eed
@@ -95,6 +95,7 @@ require("projects"):setup({
last = { last = {
update_after_save = true, update_after_save = true,
update_after_load = true, update_after_load = true,
load_after_start = false,
}, },
merge = { merge = {
quit_after_merge = false, quit_after_merge = false,
@@ -127,6 +128,7 @@ The last project is loaded by `load_last` command.
When `update_after_save` enabled, the saved project will be saved to last project. When `update_after_save` enabled, the saved project will be saved to last project.
When `update_after_load` enabled, the loaded project will be saved to last project. When `update_after_load` enabled, the loaded project will be saved to last project.
When `load_after_start` enabled, the last project will be loaded after starting.
### `merge` ### `merge`

View File

@@ -542,6 +542,7 @@ local _load_config = ya.sync(function(state, args)
state.last = { state.last = {
update_after_save = true, update_after_save = true,
update_after_load = true, update_after_load = true,
load_after_start = false,
} }
if type(args.last) == "table" then if type(args.last) == "table" then
if type(args.last.update_after_save) == "boolean" then if type(args.last.update_after_save) == "boolean" then
@@ -550,6 +551,9 @@ local _load_config = ya.sync(function(state, args)
if type(args.last.update_after_load) == "boolean" then if type(args.last.update_after_load) == "boolean" then
state.last.update_after_load = args.last.update_after_load state.last.update_after_load = args.last.update_after_load
end end
if type(args.last.load_after_start) == "boolean" then
state.last.load_after_start = args.last.load_after_start
end
end end
state.merge = { state.merge = {
@@ -599,39 +603,6 @@ local _get_default_projects = ya.sync(function(state)
} }
end) end)
local _save_projects = ya.sync(function(state, projects)
state.projects = projects
if state.save.method == "yazi" then
ps.pub_to(0, "@projects", projects)
elseif state.save.method == "lua" then
local f = io.open(state.save.lua_save_path, "w")
if not f then
return
end
f:write(json.encode(projects))
io.close(f)
end
end)
local _load_projects = ya.sync(function(state)
if state.save.method == "yazi" then
ps.sub_remote("@projects", function(body)
state.projects = body
end)
elseif state.save.method == "lua" then
local f = io.open(state.save.lua_save_path, "r")
if f then
state.projects = json.decode(f:read("*a"))
io.close(f)
end
end
if not state.projects then
state.projects = _get_default_projects()
end
end)
local _get_projects = ya.sync(function(state) local _get_projects = ya.sync(function(state)
return not state.projects and _get_default_projects() or state.projects return not state.projects and _get_default_projects() or state.projects
end) end)
@@ -665,6 +636,21 @@ local _get_current_project = ya.sync(function(state)
return project return project
end) end)
local _save_projects = ya.sync(function(state, projects)
state.projects = projects
if state.save.method == "yazi" then
ps.pub_to(0, "@projects", projects)
elseif state.save.method == "lua" then
local f = io.open(state.save.lua_save_path, "w")
if not f then
return
end
f:write(json.encode(projects))
io.close(f)
end
end)
local save_project = ya.sync(function(state, idx, desc) local save_project = ya.sync(function(state, idx, desc)
local projects = _get_projects() local projects = _get_projects()
@@ -695,8 +681,11 @@ end)
local load_project = ya.sync(function(state, project, desc) local load_project = ya.sync(function(state, project, desc)
-- TODO: add more tab properties to restore -- TODO: add more tab properties to restore
for _ = 1, #cx.tabs - 1 do -- when cx is nil, it is called in setup
ya.manager_emit("tab_close", { 0 }) if cx then
for _ = 1, #cx.tabs - 1 do
ya.manager_emit("tab_close", { 0 })
end
end end
local sorted_tabs = {} local sorted_tabs = {}
@@ -727,6 +716,31 @@ local load_project = ya.sync(function(state, project, desc)
end end
end) end)
local _load_projects = ya.sync(function(state)
if state.save.method == "yazi" then
ps.sub_remote("@projects", function(body)
state.projects = body
end)
elseif state.save.method == "lua" then
local f = io.open(state.save.lua_save_path, "r")
if f then
state.projects = json.decode(f:read("*a"))
io.close(f)
end
end
if not state.projects then
state.projects = _get_default_projects()
end
if state.last.load_after_start then
local last_project = _get_projects().last
if last_project then
load_project(last_project)
end
end
end)
local delete_all_projects = ya.sync(function(state) local delete_all_projects = ya.sync(function(state)
_save_projects(nil) _save_projects(nil)