mirror of
https://github.com/Ascyii/projects.yazi.git
synced 2026-01-01 03:24:24 -05:00
feat: add corresponding event when executing each function
This commit is contained in:
25
README.md
25
README.md
@@ -100,8 +100,31 @@ require("projects"):setup({
|
||||
load_after_start = false,
|
||||
},
|
||||
merge = {
|
||||
event = "projects-merge",
|
||||
quit_after_merge = false,
|
||||
},
|
||||
event = {
|
||||
save = {
|
||||
enable = true,
|
||||
name = "project-saved",
|
||||
},
|
||||
load = {
|
||||
enable = true,
|
||||
name = "project-loaded",
|
||||
},
|
||||
delete = {
|
||||
enable = true,
|
||||
name = "project-deleted",
|
||||
},
|
||||
delete_all = {
|
||||
enable = true,
|
||||
name = "project-deleted-all",
|
||||
},
|
||||
merge = {
|
||||
enable = true,
|
||||
name = "project-merged",
|
||||
},
|
||||
},
|
||||
notify = {
|
||||
enable = true,
|
||||
title = "Projects",
|
||||
@@ -140,6 +163,8 @@ The last project is loaded by `load_last` command.
|
||||
|
||||
### `merge`
|
||||
|
||||
`event`: the name of event used by merge feature.
|
||||
|
||||
`quit_after_merge`: the merged project will be exited after merging.
|
||||
|
||||
### `notify`
|
||||
|
||||
113
main.lua
113
main.lua
@@ -556,14 +556,93 @@ local _load_config = ya.sync(function(state, opts)
|
||||
end
|
||||
|
||||
state.merge = {
|
||||
event = "projects-merge",
|
||||
quit_after_merge = false,
|
||||
}
|
||||
if type(opts.merge) == "table" then
|
||||
if type(opts.merge.event) == "string" then
|
||||
state.merge.event = opts.merge.event
|
||||
end
|
||||
if type(opts.merge.quit_after_merge) == "boolean" then
|
||||
state.merge.quit_after_merge = opts.merge.quit_after_merge
|
||||
end
|
||||
end
|
||||
|
||||
state.event = {
|
||||
save = {
|
||||
enable = true,
|
||||
name = "project-saved",
|
||||
},
|
||||
load = {
|
||||
enable = true,
|
||||
name = "project-loaded",
|
||||
},
|
||||
delete = {
|
||||
enable = true,
|
||||
name = "project-deleted",
|
||||
},
|
||||
delete_all = {
|
||||
enable = true,
|
||||
name = "project-deleted-all",
|
||||
},
|
||||
merge = {
|
||||
enable = true,
|
||||
name = "project-merged",
|
||||
},
|
||||
}
|
||||
if type(opts.event) == "table" then
|
||||
if type(opts.event.save) == "table" then
|
||||
if type(opts.event.save.enable) == "boolean" then
|
||||
state.event.save.enable = opts.event.save.enable
|
||||
end
|
||||
if type(opts.event.save.name) == "string" then
|
||||
state.event.save.name = opts.event.save.name
|
||||
end
|
||||
elseif type(opts.event.save) == "boolean" then
|
||||
state.event.save.enable = opts.event.save
|
||||
end
|
||||
if type(opts.event.load) == "table" then
|
||||
if type(opts.event.load.enable) == "boolean" then
|
||||
state.event.load.enable = opts.event.load.enable
|
||||
end
|
||||
if type(opts.event.load.name) == "string" then
|
||||
state.event.load.name = opts.event.load.name
|
||||
end
|
||||
elseif type(opts.event.load) == "boolean" then
|
||||
state.event.load.enable = opts.event.load
|
||||
end
|
||||
if type(opts.event.delete) == "table" then
|
||||
if type(opts.event.delete.enable) == "boolean" then
|
||||
state.event.delete.enable = opts.event.delete.enable
|
||||
end
|
||||
if type(opts.event.delete.name) == "string" then
|
||||
state.event.delete.name = opts.event.delete.name
|
||||
end
|
||||
elseif type(opts.event.delete) == "boolean" then
|
||||
state.event.delete.enable = opts.event.delete
|
||||
end
|
||||
if type(opts.event.delete_all) == "table" then
|
||||
if type(opts.event.delete_all.enable) == "boolean" then
|
||||
state.event.delete_all.enable = opts.event.delete_all.enable
|
||||
end
|
||||
if type(opts.event.delete_all.name) == "string" then
|
||||
state.event.delete_all.name = opts.event.delete_all.name
|
||||
end
|
||||
elseif type(opts.event.delete_all) == "boolean" then
|
||||
state.event.delete_all.enable = opts.event.delete_all
|
||||
end
|
||||
if type(opts.event.merge) == "table" then
|
||||
if type(opts.event.merge.enable) == "boolean" then
|
||||
state.event.merge.enable = opts.event.merge.enable
|
||||
end
|
||||
if type(opts.event.merge.name) == "string" then
|
||||
state.event.merge.name = opts.event.merge.name
|
||||
end
|
||||
elseif type(opts.event.merge) == "boolean" then
|
||||
state.event.merge.enable = opts.event.merge
|
||||
end
|
||||
end
|
||||
|
||||
state.notify = {
|
||||
enable = true,
|
||||
title = "Projects",
|
||||
@@ -671,6 +750,10 @@ local save_project = ya.sync(function(state, idx, desc)
|
||||
|
||||
_save_projects(projects)
|
||||
|
||||
if state.event.save.enable then
|
||||
ps.pub_to(0, state.event.save.name, project)
|
||||
end
|
||||
|
||||
if state.notify.enable then
|
||||
local message = string.format("Project saved to %s", state.projects.list[real_idx].on)
|
||||
_notify(message)
|
||||
@@ -704,6 +787,10 @@ local load_project = ya.sync(function(state, project, desc)
|
||||
_save_projects(projects)
|
||||
end
|
||||
|
||||
if state.event.load.enable then
|
||||
ps.pub_to(0, state.event.load.name, project)
|
||||
end
|
||||
|
||||
if state.notify.enable then
|
||||
local message
|
||||
if desc then
|
||||
@@ -713,8 +800,6 @@ local load_project = ya.sync(function(state, project, desc)
|
||||
end
|
||||
_notify(message)
|
||||
end
|
||||
|
||||
ps.pub_to(0, "project-loaded", project)
|
||||
end)
|
||||
|
||||
local _load_projects = ya.sync(function(state)
|
||||
@@ -743,11 +828,16 @@ local _load_projects = ya.sync(function(state)
|
||||
end)
|
||||
|
||||
local delete_all_projects = ya.sync(function(state)
|
||||
_save_projects(nil)
|
||||
_save_projects(_get_default_projects())
|
||||
|
||||
local msg = "All projects deleted"
|
||||
|
||||
if state.event.delete_all.enable then
|
||||
ps.pub_to(0, state.event.delete_all.name, msg)
|
||||
end
|
||||
|
||||
if state.notify.enable then
|
||||
local message = "All projects deleted"
|
||||
_notify(message)
|
||||
_notify(msg)
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -756,9 +846,14 @@ local delete_project = ya.sync(function(state, idx)
|
||||
|
||||
local message = string.format([["%s" deleted]], tostring(projects.list[idx].desc))
|
||||
|
||||
local deleted_project = projects.list[idx]
|
||||
table.remove(projects.list, idx)
|
||||
_save_projects(projects)
|
||||
|
||||
if state.event.delete.enable then
|
||||
ps.pub_to(0, state.event.delete.name, deleted_project)
|
||||
end
|
||||
|
||||
if state.notify.enable then
|
||||
_notify(message)
|
||||
end
|
||||
@@ -776,7 +871,11 @@ end)
|
||||
local merge_project = ya.sync(function(state, opt)
|
||||
local project = _get_current_project()
|
||||
project.opt = opt or "all"
|
||||
ps.pub_to(0, "projects-merge", project)
|
||||
ps.pub_to(0, state.merge.event, project)
|
||||
|
||||
if state.event.merge.enable then
|
||||
ps.pub_to(0, state.event.merge.name, project)
|
||||
end
|
||||
|
||||
if state.merge.quit_after_merge then
|
||||
ya.emit("quit", {})
|
||||
@@ -788,7 +887,7 @@ local _merge_tab = ya.sync(function(state, tab)
|
||||
end)
|
||||
|
||||
local _merge_event = ya.sync(function(state)
|
||||
ps.sub_remote("projects-merge", function(body)
|
||||
ps.sub_remote(state.merge.event, function(body)
|
||||
if body then
|
||||
local active_idx = tonumber(cx.tabs.idx)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user