depctrl global config: use depctrl's config handler

better than finding a file that's probably the config, parsing the json, and writing it back in,
for obvious reasons

turns out no i wasnt thinning out the soup too much
twas just right

Reason for adding values one by one in key-pair loop instead of just config = new_config:

> [01:31] arch1t3cht: oh, I think you can't assign entire tables to DependencyControl.config.c
> [01:31] arch1t3cht: like, you can't do DependencyControl.config.c = <my new config>,
>                     you should do DependencyControl.config.myfield = <my new value> and whatnot
> [01:32] arch1t3cht: because behind the scenes it uses metatable magic to track what field changes
>                     so it can merge its own changes with ones that other scripts made in the meantime

Co-Authored-By: arch1t3cht <arch1t3cht@gmail.com>
This commit is contained in:
garret 2022-12-21 06:46:15 +00:00
parent d8a867c9ec
commit 771a31da35
1 changed files with 18 additions and 32 deletions

View File

@ -1,15 +1,11 @@
script_name="DepCtrl Global Config" script_name="DepCtrl Global Config"
script_description="the future is now" script_description="the future is now"
script_author = "garret" script_author = "garret"
script_version = "1.2.0" script_version = "1.3.0"
script_namespace = "garret.depctrl_config" script_namespace = "garret.depctrl_config"
local DependencyControl = require("l0.DependencyControl") local DependencyControl = require("l0.DependencyControl")
local depctrl = DependencyControl { local depctrl = DependencyControl {}
--feed="TODO",
{"json"}
}
local json = depctrl:requireModules()
local function get_bool(field, default) -- can't just do `field or default`, because the default might be true when field is false local function get_bool(field, default) -- can't just do `field or default`, because the default might be true when field is false
if field == nil then if field == nil then
@ -63,7 +59,7 @@ local function get_human_filesize(bytes)
end end
local function get_config(config) local function get_config(config)
local defaults = {updaterEnabled = true, updateInterval = 302400, traceLevel = 3, tryAllFeeds = false, dumpFeeds = true, configDir = "?user/config", logMaxFiles = 200, logMaxAge = 604800, logMaxSize = 10 * (10 ^ 6), updateWaitTimeout = 60, updateOrphanTimeout = 600, logDir = "?user/log", writeLogs = true} local defaults = DependencyControl.config.defaults
local dialog = { local dialog = {
{ class="checkbox", name="updaterEnabled", { class="checkbox", name="updaterEnabled",
x=0,y=0,width=2,height=1, x=0,y=0,width=2,height=1,
@ -144,7 +140,7 @@ local function get_config(config)
if pressed == "Cancel" then if pressed == "Cancel" then
aegisub.cancel() aegisub.cancel()
elseif pressed == "Reset" then elseif pressed == "Reset" then
return {} return {}
end end
res.traceLevel = tonumber(res.traceLevel:sub(1, 1)) res.traceLevel = tonumber(res.traceLevel:sub(1, 1))
res.updateInterval = human_to_seconds(res.updateInterval) res.updateInterval = human_to_seconds(res.updateInterval)
@ -184,33 +180,23 @@ local function get_feeds(config)
return config return config
end end
local function read_json(path) local function write_config(new)
local file = io.open(path, "r") for k, v in pairs(new) do
local json = json.decode(file:read()) if (v == nil) -- allow nil, so the reset to defaults button works
file:close() or v ~= (DependencyControl.config.c[k] or DependencyControl.config.defaults[k]) -- check it's not the current value anyway
return json then
end -- changed, save
DependencyControl.config.c[k] = v
local function write_json(path, table) end
local json = json.encode(table) end
local file = io.open(path, "w") DependencyControl.config:write(true)
file:write(json)
file:close()
aegisub.log(3, "Done. You'll need to rescan your automation directory or restart aegisub for the changes to take effect.") aegisub.log(3, "Done. You'll need to rescan your automation directory or restart aegisub for the changes to take effect.")
end end
local function get_config_path() local function change_config(modifier) -- i think i might be thinning out the soup a bit too much
local path = depctrl:getConfigFileName() DependencyControl.config:load()
path = path:gsub(script_namespace, "l0.DependencyControl") local new_config = modifier(DependencyControl.config.c)
aegisub.log(4, "config file: "..path.."\n") write_config(new_config)
return path
end
local function change_config(new) -- i think i might be thinning out the soup a bit too much
local config_path = get_config_path()
local data = read_json(config_path)
data.config = new(data.config)
write_json(config_path, data)
end end
local function global_config() local function global_config()