mirror of
https://github.com/Ascyii/projects.yazi.git
synced 2026-01-01 03:24:24 -05:00
feat: add feature of loading last project after starting
This commit is contained in:
@@ -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.
|
||||
|
||||
> [!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
|
||||
|
||||
@@ -95,6 +95,7 @@ require("projects"):setup({
|
||||
last = {
|
||||
update_after_save = true,
|
||||
update_after_load = true,
|
||||
load_after_start = false,
|
||||
},
|
||||
merge = {
|
||||
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_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`
|
||||
|
||||
|
||||
84
init.lua
84
init.lua
@@ -542,6 +542,7 @@ local _load_config = ya.sync(function(state, args)
|
||||
state.last = {
|
||||
update_after_save = true,
|
||||
update_after_load = true,
|
||||
load_after_start = false,
|
||||
}
|
||||
if type(args.last) == "table" 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
|
||||
state.last.update_after_load = args.last.update_after_load
|
||||
end
|
||||
if type(args.last.load_after_start) == "boolean" then
|
||||
state.last.load_after_start = args.last.load_after_start
|
||||
end
|
||||
end
|
||||
|
||||
state.merge = {
|
||||
@@ -599,39 +603,6 @@ local _get_default_projects = ya.sync(function(state)
|
||||
}
|
||||
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)
|
||||
return not state.projects and _get_default_projects() or state.projects
|
||||
end)
|
||||
@@ -665,6 +636,21 @@ local _get_current_project = ya.sync(function(state)
|
||||
return project
|
||||
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 projects = _get_projects()
|
||||
|
||||
@@ -695,8 +681,11 @@ end)
|
||||
local load_project = ya.sync(function(state, project, desc)
|
||||
-- TODO: add more tab properties to restore
|
||||
|
||||
for _ = 1, #cx.tabs - 1 do
|
||||
ya.manager_emit("tab_close", { 0 })
|
||||
-- when cx is nil, it is called in setup
|
||||
if cx then
|
||||
for _ = 1, #cx.tabs - 1 do
|
||||
ya.manager_emit("tab_close", { 0 })
|
||||
end
|
||||
end
|
||||
|
||||
local sorted_tabs = {}
|
||||
@@ -727,6 +716,31 @@ local load_project = ya.sync(function(state, project, desc)
|
||||
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)
|
||||
_save_projects(nil)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user