Implements cmd_scheduler_list

This commit is contained in:
AliasAlreadyTaken 2024-04-12 23:22:16 +02:00
parent 90db695766
commit 822009dc92

View File

@ -24,7 +24,7 @@ end
local function is_uuid_duplicate(UUID)
for i, task in ipairs(yl_scheduler.tasks) do
say("action", "task.id=" .. dump(task.id) .. ", UUID=" .. dump(UUID))
say("is_uuid_duplicate task.id=" .. dump(task.id) .. ", UUID=" .. dump(UUID))
if task.id == UUID then return true end
end
return false
@ -66,6 +66,42 @@ function string_to_table(value)
return {}
end
-- taken from yl_cinema
-- TODO: Shoudl we API-fy this?
local function format_table(t)
-- Format of t must be {{row1,row2,row3, ...},{row1,row2,row3, ...},...}
local blanks_between_rows = 3
local max_row_length = {}
for linenumber = 1, #t do
for rownumber = 1, #t[linenumber] do
local row_length = #tostring(t[linenumber][rownumber])
if (max_row_length[rownumber] or 0) < row_length then
max_row_length[rownumber] = row_length
end
end
end
local ret = {}
for linenumber = 1, #t do
local line_s = ""
for rownumber = 1, #t[linenumber] do
local text = t[linenumber][rownumber]
local text_length = #tostring(text)
local add_blanks = max_row_length[rownumber] - text_length
local newtext = t[linenumber][rownumber]
for add = 1, (add_blanks + blanks_between_rows) do
newtext = newtext .. " "
end
line_s = line_s .. newtext
end
table.insert(ret,line_s)
end
return table.concat(ret, "\n")
end
--- ###
local function sort_by_timestamp(tasks)
@ -235,7 +271,7 @@ end
local function get_savepath()
-- TODO: Can we assume the path exists?
local savepath = yl_scheduler.worldpath .. yl_scheduler.settings.save_path
say("savepath : " .. dump(savepath))
say("get_savepath : " .. dump(savepath))
return savepath
end
@ -243,7 +279,7 @@ local function get_filepath(UUID)
local path_to_file = yl_scheduler.worldpath ..
yl_scheduler.settings.save_path .. DIR_DELIM ..
UUID .. ".json"
say(UUID .. ":" .. dump(path_to_file))
say("get_filepath : " .. dump(UUID) .. ":" .. dump(path_to_file))
return path_to_file
end
@ -419,8 +455,6 @@ local function cmd_scheduler_add(name, c_params)
local owner = name or "N/A"
local notes = t_parameters[5] or "" -- optional
say("string_params = " .. dump(string_params))
say("masks = " .. dump(masks))
local param_success, params = unmask_params(string_params, masks)
if (param_success == false) then return false, params end
@ -448,7 +482,6 @@ local function cmd_scheduler_remove(name, params)
local UUID = string.trim(params)
say("cmd_scheduler_remove = " .. dump(UUID))
local success, message = yl_scheduler.remove_task(UUID)
if success == true then
@ -467,6 +500,76 @@ end
local function cmd_scheduler_list(name, params)
-- Defense: Overlap with yl_scheduler.list_all_tasks() and yl_scheduler.list_task(UUID)
-- Display nicely. Steal from ... I mean INSPIRE ... by yl_cinema list? Or formspec output?
if not params or params == "help" then
return true, "Lists all existing tasks from the list.\n" ..
"Example: /scheduler_remove 11a47d48-4d0c-47ab-a5ef-4f56781cae03"
end
local tasks = {}
local success
local args = string.split(params, "=")
if params == "" then
-- return whole list
say("cmd_scheduler_list Whole list")
success, tasks = yl_scheduler.list_all_tasks()
elseif #args == 1 then
-- return UUID search
say("cmd_scheduler_list UUID search")
local UUID = string.trim(params)
success, tasks = yl_scheduler.find_task(UUID)
elseif #args == 2 then
-- search for key
say("cmd_scheduler_list Key search")
local key = args[1]
local value = args[2]
local s_success, all_tasks = yl_scheduler.list_all_tasks()
for _,task in ipairs(all_tasks) do
-- TODO: Should we exact match or string.find?
if task[key] == value then
table.insert(tasks, task)
end
end
success = s_success
else
return false, "Parameter unclear, please do /scheduler_remove help"
end
if (#tasks == 0) or (success == false) then
return true, "No match"
end
-- Formatting output
local f_tasks = {{"ID", "at/done", "func", "#params", "owner", "notes"}}
for _,task in ipairs(tasks) do
local id = task.id or "N/A"
local atdone
if not task.done or (task.done and (task.done == -1)) then
atdone = "at " .. os.date("!%c", task.at or 0)
elseif task.done and (task.done > 0) then
atdone = "done " .. os.date("!%c",task.done or 0)
else
atdone = "N/A"
end
local func = task.func or "N/A"
local f_params = tostring(#task.params or 0) or "N/A"
if task.params and (type(task.params) ~= "table") then
f_params = "N/A"
minetest.log("warning", "[MOD] yl_scheduler : UUID " .. dump(id) .." has string param instead of table")
end
local owner = task.owner or "N/A"
local notes = task.notes or ""
local t = {id, atdone, func, f_params, owner, notes}
table.insert(f_tasks, t)
end
return true, format_table(f_tasks)
end
function yl_scheduler.cmd_scheduler_list(name, params)