Resumable intermediates #2

Merged
tangent merged 17 commits from resumable-intermediates into main 2026-08-23 23:59:39 -06:00
Showing only changes of commit 4820e904f0 - Show all commits
+47 -12
View File
@@ -32,7 +32,7 @@ else
} }
end end
utility.version = "1.4.0" utility.version = "1.5.2"
-- WARNING: This will return "./" if the original script is called locally instead of with an absolute path! -- WARNING: This will return "./" if the original script is called locally instead of with an absolute path!
if arg[0] ~= nil then if arg[0] ~= nil then
utility.path = (arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)")) -- inspired by discussion in https://stackoverflow.com/q/6380820 utility.path = (arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)")) -- inspired by discussion in https://stackoverflow.com/q/6380820
@@ -124,14 +124,6 @@ standard_library_addition(string, "enquote", function(s)
return "\"" .. s:gsub("\"", "\\\"") .. "\"" return "\"" .. s:gsub("\"", "\\\"") .. "\""
end) end)
standard_library_addition(string, "split", function(s, delimiter)
local result = {}
for item in s:gsplit(delimiter) do
result[#result + 1] = item
end
return result
end)
standard_library_addition(string, "gsplit", function(s, delimiter) standard_library_addition(string, "gsplit", function(s, delimiter)
local function escape_special_characters(s) local function escape_special_characters(s)
local special_characters = "[()%%.[^$%]*+%-?]" local special_characters = "[()%%.[^$%]*+%-?]"
@@ -144,6 +136,14 @@ standard_library_addition(string, "gsplit", function(s, delimiter)
return s:gmatch("(.-)" .. escape_special_characters(delimiter)) return s:gmatch("(.-)" .. escape_special_characters(delimiter))
end) end)
standard_library_addition(string, "split", function(s, delimiter)
local result = {}
for item in s:gsplit(delimiter) do
result[#result + 1] = item
end
return result
end)
-- modified from my fork of lume -- modified from my fork of lume
@@ -301,7 +301,7 @@ utility.release_lock = function(file_path, lock_uuid)
local lock_file_path = file_path .. ".lock" local lock_file_path = file_path .. ".lock"
if lock_uuid then if lock_uuid then
utility.open(lock_file_path, "r", function(file) utility.open(lock_file_path, "r", function(file)
if not file:read("*all") == lock_uuid then if not (file:read("*all") == lock_uuid) then
error("\n\n Lock UUID changed while lock was obtained. Data loss may have occurred. \n\n") error("\n\n Lock UUID changed while lock was obtained. Data loss may have occurred. \n\n")
end end
end) end)
@@ -348,12 +348,47 @@ utility.save_config = function()
end end
end end
local data_file_locations = {}
utility.load_data = function(file_path)
local data = utility.open(file_path, "r", function(data_file)
local json = utility.require("dkjson")
return json.decode(data_file:read("*all"))
end)
data_file_locations[data] = file_path
return data
end
utility.save_data = function(data, file_path)
local keys, loop = {}
loop = function(tab)
if type(tab) == "table" then
for k,v in pairs(tab) do
if not (type(k) == "number") then
keys[k] = true
end
loop(v)
end
end
end
loop(data)
local order = {}
for k in pairs(keys) do order[#order + 1] = k end
table.sort(order)
file_path = file_path or data_file_locations[data]
assert(file_path, "The object must have been loaded by utility.load_data or you must pass a path as the second argument.")
utility.open(file_path, "w", function(data_file)
local json = utility.require("dkjson")
data_file:write(json.encode(data, { indent = true, keyorder = order, }))
data_file:write("\n")
end)
end
utility.deepcopy = function(tab) utility.deepcopy = function(tab)
local _type = type(tab)
local copy local copy
if _type == "table" then if type(tab) == "table" then
copy = {} copy = {}
for key, value in next, tab, nil do for key, value in next, tab, nil do
copy[utility.deepcopy(key)] = utility.deepcopy(value) copy[utility.deepcopy(key)] = utility.deepcopy(value)