aboutsummaryrefslogtreecommitdiffstats
path: root/macros/garret.depctrl_config.lua
blob: df9596a7653fed1c12d42ccc4080949a7f7d8e88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
script_name="DepCtrl Global Config"
script_description="the future is now"
script_author = "garret"
script_version = "1.3.0"
script_namespace = "garret.depctrl_config"

local DependencyControl = require("l0.DependencyControl")
local depctrl = DependencyControl {}

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
        return default
    else
        return field
    end
end

local function get_log_level(num)
    if num == 0 then return "0: Fatal"
    elseif num == 1 then return "1: Error"
    elseif num == 2 then return "2: Warning"
    elseif num == 3 then return "3: Hint"
    elseif num == 4 then return "4: Debug"
    elseif num == 5 then return "5: Trace" end
    return nil
end

local function seconds_to_human(seconds)
    -- adapted from https://stackoverflow.com/a/8211778
    local years = math.floor(seconds / 31536000)
    local days = math.floor((seconds % 31536000) / 86400)
    local hours = math.floor(((seconds % 31536000) % 86400) / 3600)
    local minutes = math.floor((((seconds % 31536000) % 86400) % 3600) / 60)
    seconds = (((seconds % 31536000) % 86400) % 3600) % 60
    --return years, days, hours, minutes, seconds
    local timestamp = ""
    if years ~= 0 then timestamp = timestamp..years.."y" end
    if days ~= 0 then timestamp = timestamp..days.."d" end
    if hours ~= 0 then timestamp = timestamp..hours.."h" end
    if minutes ~= 0 then timestamp = timestamp..minutes.."m" end
    if seconds ~= 0 then timestamp = timestamp..seconds.."s" end
    return timestamp
end

local function human_to_seconds(human)
    -- im sure this is hideously inefficient
    local years = (tonumber(human:match("(%d*)y")) or 0) * 31536000
    local days = (tonumber(human:match("(%d*)d")) or 0) * 86400
    local hours = (tonumber(human:match("(%d*)h")) or 0) * 3600
    local minutes = (tonumber(human:match("(%d*)m")) or 0) * 60
    local seconds = (tonumber(human:match("(%d*)s")) or 0)
    local res = years + days + hours + minutes + seconds
    return res
end

local function get_human_filesize(bytes)
    -- TODO
    return bytes
end

local function get_config(config)
    local defaults = DependencyControl.config.defaults
    local dialog = {
        {   class="checkbox", name="updaterEnabled",
            x=0,y=0,width=2,height=1,
            label = "Enable Auto-updater",
            value=get_bool(config.updaterEnabled, defaults.updaterEnabled),
        },
        {   class="label", label="Update interval:",
            x=0,y=1,width=1,height=1,
        },
        {   class="edit", name = "updateInterval",
            x=1,y=1,width=1,height=1,
            value = seconds_to_human(config.updateInterval or defaults.updateInterval)
        },
        {   class="label", label="Log Level:",
            x=0,y=2,width=1,height=1,
        },
        {   class="dropdown", name="traceLevel",
            x=1,y=2,width=1,height=1,
            items={"0: Fatal", "1: Error", "2: Warning", "3: Hint", "4: Debug", "5: Trace"},
            value=get_log_level(config.traceLevel) or "3: Hint",
        },
        {   class="checkbox", name="dumpFeeds",
            x=0,y=3,width=2,height=1,
            label = "Dump updater feeds to your Aegsiub folder (debug)",
            value=get_bool(config.dumpFeeds, defaults.dumpFeeds)
        },
        {   class="checkbox", name="tryAllFeeds",
            x=0,y=4,width=2,height=1,
            label = "Check all available feeds for an update",
            value=get_bool(config.tryAllFeeds, defaults.tryAllFeeds)
        },
        {   class="label", label="Config directory to offer automation scripts:",
            x=0,y=5,width=1,height=1
        },
        {   class="edit", name="configDir",
            text=config.configDir or defaults.configDir,
            x=1,y=5,width=1,height=1
        },
        {   class="checkbox", name="writeLogs",
            x=0,y=6,width=1,height=1,
            label = "Write log messages to:",
            value=get_bool(config.writeLogs, true)
        },
        {   class="edit", name="logDir",
            text=config.logDir or defaults.logDir,
            x=1,y=6,width=1,height=1
        },
        {   class="label", label= "?user means " .. aegisub.decode_path("?user"),
            x=1,y=7,width=2,height=1
        },
        {   class="label", label="Purge old updater log files when:",
            x=0,y=8,width=2,height=1
        },
        {   class="label", label="File count reaches:",
            x=0,y=9,width=1,height=1
        },
        {   class="intedit", name="logMaxFiles",
            x=1,y=9,width=1,height=1,
            value = config.logMaxFiles or defaults.logMaxFiles
        },
        {   class="label", label="File age reaches:",
            x=0,y=10,width=1,height=1,
        },
        {   class="edit", name="logMaxAge",
            x=1,y=10,width=1,height=1,
            value = seconds_to_human(config.logMaxAge or defaults.logMaxAge)
        },
        {   class="label", label="Cumulative file size (in bytes) reaches:",
            x=0,y=11,width=1,height=1,
        },
        {   class="intedit", name="logMaxSize",
            x=1,y=11,width=1,height=1,
            --value = get_human_filesize(config.logMaxSize or 10*(10^6))
            value = config.logMaxSize or defaults.logMaxSize
        },
    }
    local pressed, res = aegisub.dialog.display(dialog, {"Cancel", "Reset", "OK"})
    if pressed == "Cancel" then
        aegisub.cancel()
    elseif pressed == "Reset" then
        return {}
    end
    res.traceLevel = tonumber(res.traceLevel:sub(1, 1))
    res.updateInterval = human_to_seconds(res.updateInterval)
    res.logMaxAge = human_to_seconds(res.logMaxAge)
    res.extraFeeds = config.extraFeeds
    return res
end

local function split_by_newline(list)
    local strs = {}
    for i in list:gmatch("[^\n]+") do
        table.insert(strs, i)
    end
    return strs
end

local function get_feeds(config)
    local extraFeeds = config.extraFeeds or {}

    local feed_edit_string = table.concat(extraFeeds, "\n") or ""

    local dialog = {
        {   class="label",
            x=0,y=0,width=1,height=1,
            label = "Extra Feeds:",
        },
        {   class="textbox", name = "feeds",
            x=0,y=1,width=20,height=15,
            text = feed_edit_string
        },
    }

    local pressed, res = aegisub.dialog.display(dialog)
    if not pressed then aegisub.cancel() end

    config.extraFeeds = split_by_newline(res.feeds)
    return config
end

local function write_config(new)
    for k, v in pairs(new) do
        if (v == nil) -- allow nil, so the reset to defaults button works
        or v ~= (DependencyControl.config.c[k] or DependencyControl.config.defaults[k]) -- check it's not the current value anyway
        then
            -- changed, save
            DependencyControl.config.c[k] = v
        end
    end
    DependencyControl.config:write(true)
    aegisub.log(3, "Done. You'll need to rescan your automation directory or restart aegisub for the changes to take effect.")
end

local function change_config(modifier) -- i think i might be thinning out the soup a bit too much
    DependencyControl.config:load()
    local new_config = modifier(DependencyControl.config.c)
    write_config(new_config)
end

local function global_config()
    change_config(get_config)
end

local function extra_feeds()
    change_config(get_feeds)
end

depctrl:registerMacro("DependencyControl/Global Configuration", "Lets you change DependencyControl settings.", global_config)
depctrl:registerMacro("DependencyControl/Extra Feeds", "Lets you provide additional update feeds.", extra_feeds)