2019-12-28 12:16:32 +01:00
|
|
|
local common = {}
|
|
|
|
|
|
|
|
|
|
|
|
function common.is_utf8_cont(char)
|
|
|
|
local byte = char:byte()
|
|
|
|
return byte >= 0x80 and byte < 0xc0
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function common.utf8_chars(text)
|
|
|
|
return text:gmatch("[\0-\x7f\xc2-\xf4][\x80-\xbf]*")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function common.clamp(n, lo, hi)
|
|
|
|
return math.max(math.min(n, hi), lo)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function common.round(n)
|
|
|
|
return n >= 0 and math.floor(n + 0.5) or math.ceil(n - 0.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-14 03:28:29 +02:00
|
|
|
function common.find_index(tbl, prop)
|
|
|
|
for i, o in ipairs(tbl) do
|
|
|
|
if o[prop] then return i end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function common.lerp(a, b, t)
|
|
|
|
if type(a) ~= "table" then
|
|
|
|
return a + (b - a) * t
|
|
|
|
end
|
|
|
|
local res = {}
|
|
|
|
for k, v in pairs(b) do
|
|
|
|
res[k] = common.lerp(a[k], v, t)
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function common.color(str)
|
|
|
|
local r, g, b, a = str:match("#(%x%x)(%x%x)(%x%x)")
|
|
|
|
if r then
|
|
|
|
r = tonumber(r, 16)
|
|
|
|
g = tonumber(g, 16)
|
|
|
|
b = tonumber(b, 16)
|
|
|
|
a = 1
|
|
|
|
elseif str:match("rgba?%s*%([%d%s%.,]+%)") then
|
|
|
|
local f = str:gmatch("[%d.]+")
|
|
|
|
r = (f() or 0)
|
|
|
|
g = (f() or 0)
|
|
|
|
b = (f() or 0)
|
|
|
|
a = f() or 1
|
|
|
|
else
|
|
|
|
error(string.format("bad color string '%s'", str))
|
|
|
|
end
|
|
|
|
return r, g, b, a * 0xff
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-07 02:30:09 +02:00
|
|
|
function common.splice(t, at, remove, insert)
|
|
|
|
insert = insert or {}
|
|
|
|
local offset = #insert - remove
|
|
|
|
local old_len = #t
|
|
|
|
if offset < 0 then
|
|
|
|
for i = at - offset, old_len - offset do
|
|
|
|
t[i + offset] = t[i]
|
|
|
|
end
|
|
|
|
elseif offset > 0 then
|
|
|
|
for i = old_len, at, -1 do
|
|
|
|
t[i + offset] = t[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for i, item in ipairs(insert) do
|
|
|
|
t[at + i - 1] = item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
local function compare_score(a, b)
|
|
|
|
return a.score > b.score
|
|
|
|
end
|
|
|
|
|
2021-05-16 19:23:17 +02:00
|
|
|
local function fuzzy_match_items(items, needle, files)
|
2019-12-28 12:16:32 +01:00
|
|
|
local res = {}
|
2020-05-07 23:40:34 +02:00
|
|
|
for _, item in ipairs(items) do
|
2021-05-16 19:23:17 +02:00
|
|
|
local score = system.fuzzy_match(tostring(item), needle, files)
|
2019-12-28 12:16:32 +01:00
|
|
|
if score then
|
|
|
|
table.insert(res, { text = item, score = score })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(res, compare_score)
|
|
|
|
for i, item in ipairs(res) do
|
|
|
|
res[i] = item.text
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-05-16 19:23:17 +02:00
|
|
|
function common.fuzzy_match(haystack, needle, files)
|
2019-12-28 12:16:32 +01:00
|
|
|
if type(haystack) == "table" then
|
2021-05-16 19:23:17 +02:00
|
|
|
return fuzzy_match_items(haystack, needle, files)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-05-16 19:23:17 +02:00
|
|
|
return system.fuzzy_match(haystack, needle, files)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-01-03 15:22:47 +01:00
|
|
|
function common.fuzzy_match_with_recents(haystack, recents, needle)
|
|
|
|
if needle == "" then
|
|
|
|
local recents_ext = {}
|
|
|
|
for i = 2, #recents do
|
2021-05-16 19:23:17 +02:00
|
|
|
table.insert(recents_ext, recents[i])
|
2021-01-03 15:22:47 +01:00
|
|
|
end
|
|
|
|
table.insert(recents_ext, recents[1])
|
2021-05-16 19:23:17 +02:00
|
|
|
local others = common.fuzzy_match(haystack, "", true)
|
2021-01-03 15:22:47 +01:00
|
|
|
for i = 1, #others do
|
|
|
|
table.insert(recents_ext, others[i])
|
|
|
|
end
|
|
|
|
return recents_ext
|
|
|
|
else
|
2021-05-16 19:23:17 +02:00
|
|
|
return fuzzy_match_items(haystack, needle, true)
|
2021-01-03 15:22:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function common.path_suggest(text)
|
|
|
|
local path, name = text:match("^(.-)([^/\\]*)$")
|
2020-04-22 00:45:34 +02:00
|
|
|
local files = system.list_dir(path == "" and "." or path) or {}
|
2019-12-28 12:16:32 +01:00
|
|
|
local res = {}
|
|
|
|
for _, file in ipairs(files) do
|
|
|
|
file = path .. file
|
|
|
|
local info = system.get_file_info(file)
|
|
|
|
if info then
|
|
|
|
if info.type == "dir" then
|
2020-04-25 10:57:35 +02:00
|
|
|
file = file .. PATHSEP
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
if file:lower():find(text:lower(), nil, true) == 1 then
|
|
|
|
table.insert(res, file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-07 18:06:58 +01:00
|
|
|
function common.dir_path_suggest(text)
|
|
|
|
local path, name = text:match("^(.-)([^/\\]*)$")
|
|
|
|
local files = system.list_dir(path == "" and "." or path) or {}
|
|
|
|
local res = {}
|
|
|
|
for _, file in ipairs(files) do
|
|
|
|
file = path .. file
|
|
|
|
local info = system.get_file_info(file)
|
|
|
|
if info and info.type == "dir" and file:lower():find(text:lower(), nil, true) == 1 then
|
|
|
|
table.insert(res, file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-28 15:51:16 +01:00
|
|
|
function common.dir_list_suggest(text, dir_list)
|
|
|
|
local path, name = text:match("^(.-)([^/\\]*)$")
|
|
|
|
local res = {}
|
|
|
|
for _, dir_path in ipairs(dir_list) do
|
|
|
|
if dir_path:lower():find(text:lower(), nil, true) == 1 then
|
|
|
|
table.insert(res, dir_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-07 15:18:46 +02:00
|
|
|
function common.match_pattern(text, pattern, ...)
|
2020-05-07 11:27:37 +02:00
|
|
|
if type(pattern) == "string" then
|
|
|
|
return text:find(pattern, ...)
|
|
|
|
end
|
|
|
|
for _, p in ipairs(pattern) do
|
2020-05-07 15:18:46 +02:00
|
|
|
local s, e = common.match_pattern(text, p, ...)
|
2020-05-07 11:27:37 +02:00
|
|
|
if s then return s, e end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function common.draw_text(font, color, text, align, x,y,w,h)
|
|
|
|
local tw, th = font:get_width(text), font:get_height(text)
|
|
|
|
if align == "center" then
|
|
|
|
x = x + (w - tw) / 2
|
|
|
|
elseif align == "right" then
|
|
|
|
x = x + (w - tw)
|
|
|
|
end
|
2020-05-03 17:48:35 +02:00
|
|
|
y = common.round(y + (h - th) / 2)
|
2019-12-28 12:16:32 +01:00
|
|
|
return renderer.draw_text(font, text, x, y, color), y + th
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function common.bench(name, fn, ...)
|
|
|
|
local start = system.get_time()
|
|
|
|
local res = fn(...)
|
|
|
|
local t = system.get_time() - start
|
|
|
|
local ms = t * 1000
|
|
|
|
local per = (t / (1 / 60)) * 100
|
|
|
|
print(string.format("*** %-16s : %8.3fms %6.2f%%", name, ms, per))
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-30 13:32:25 +01:00
|
|
|
function common.serialize(val)
|
|
|
|
if type(val) == "string" then
|
|
|
|
return string.format("%q", val)
|
|
|
|
elseif type(val) == "table" then
|
|
|
|
local t = {}
|
|
|
|
for k, v in pairs(val) do
|
|
|
|
table.insert(t, "[" .. common.serialize(k) .. "]=" .. common.serialize(v))
|
|
|
|
end
|
|
|
|
return "{" .. table.concat(t, ",") .. "}"
|
|
|
|
end
|
|
|
|
return tostring(val)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-01-10 12:56:15 +01:00
|
|
|
function common.basename(path)
|
|
|
|
-- a path should never end by / or \ except if it is '/' (unix root) or
|
|
|
|
-- 'X:\' (windows drive)
|
|
|
|
return path:match("[^\\/]+$") or path
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-20 20:39:50 +02:00
|
|
|
-- can return nil if there is no directory part in the path
|
|
|
|
function common.dirname(path)
|
|
|
|
return path:match("(.+)[\\/][^\\/]+$")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-19 23:53:29 +01:00
|
|
|
function common.home_encode(text)
|
2021-05-05 10:34:48 +02:00
|
|
|
if HOME and string.find(text, HOME, 1, true) == 1 then
|
|
|
|
local dir_pos = #HOME + 1
|
2021-05-24 15:57:02 +02:00
|
|
|
-- ensure we don't replace if the text is just "$HOME" or "$HOME/" so
|
|
|
|
-- it must have a "/" following the $HOME and some characters following.
|
|
|
|
if string.find(text, PATHSEP, dir_pos, true) == dir_pos and #text > dir_pos then
|
2021-05-05 10:34:48 +02:00
|
|
|
return "~" .. text:sub(dir_pos)
|
2020-12-20 00:31:49 +01:00
|
|
|
end
|
2020-12-19 23:53:29 +01:00
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-16 22:52:55 +01:00
|
|
|
function common.home_encode_list(paths)
|
|
|
|
local t = {}
|
|
|
|
for i = 1, #paths do
|
|
|
|
t[i] = common.home_encode(paths[i])
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-19 23:53:29 +01:00
|
|
|
function common.home_expand(text)
|
2020-12-20 00:31:49 +01:00
|
|
|
return HOME and text:gsub("^~", HOME) or text
|
2020-12-19 23:53:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-03-06 23:36:05 +01:00
|
|
|
local function split_on_slash(s, sep_pattern)
|
|
|
|
local t = {}
|
2021-05-10 17:01:14 +02:00
|
|
|
if s:match("^[/\\]") then
|
|
|
|
t[#t + 1] = ""
|
|
|
|
end
|
2021-03-06 23:36:05 +01:00
|
|
|
for fragment in string.gmatch(s, "([^/\\]+)") do
|
|
|
|
t[#t + 1] = fragment
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-20 20:39:50 +02:00
|
|
|
function common.normalize_path(filename)
|
2021-09-07 14:09:26 +02:00
|
|
|
if not filename then return end
|
2021-07-20 20:39:50 +02:00
|
|
|
if PATHSEP == '\\' then
|
|
|
|
filename = filename:gsub('[/\\]', '\\')
|
|
|
|
local drive, rem = filename:match('^([a-zA-Z])(:.*)')
|
|
|
|
filename = drive and drive:upper() .. rem or filename
|
|
|
|
end
|
|
|
|
local parts = split_on_slash(filename, PATHSEP)
|
|
|
|
local accu = {}
|
|
|
|
for _, part in ipairs(parts) do
|
2021-08-12 02:54:03 +02:00
|
|
|
if part == '..' and #accu > 0 and accu[#accu] ~= ".." then
|
2021-07-20 20:39:50 +02:00
|
|
|
table.remove(accu)
|
|
|
|
elseif part ~= '.' then
|
|
|
|
table.insert(accu, part)
|
|
|
|
end
|
|
|
|
end
|
2021-09-07 14:09:26 +02:00
|
|
|
local npath = table.concat(accu, PATHSEP)
|
|
|
|
return npath == "" and PATHSEP or npath
|
2021-07-20 20:39:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-05-01 19:18:39 +02:00
|
|
|
function common.path_belongs_to(filename, path)
|
2021-07-20 20:39:50 +02:00
|
|
|
return string.find(filename, path .. PATHSEP, 1, true) == 1
|
2021-05-01 19:18:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-03-06 23:36:05 +01:00
|
|
|
function common.relative_path(ref_dir, dir)
|
2021-07-26 05:22:56 +02:00
|
|
|
local drive_pattern = "^(%a):\\"
|
|
|
|
local drive, ref_drive = dir:match(drive_pattern), ref_dir:match(drive_pattern)
|
|
|
|
if drive and ref_drive and drive ~= ref_drive then
|
2021-07-22 10:32:22 +02:00
|
|
|
-- Windows, different drives, system.absolute_path fails for C:\..\D:\
|
|
|
|
return dir
|
|
|
|
end
|
2021-03-06 23:36:05 +01:00
|
|
|
local ref_ls = split_on_slash(ref_dir)
|
|
|
|
local dir_ls = split_on_slash(dir)
|
|
|
|
local i = 1
|
|
|
|
while i <= #ref_ls do
|
|
|
|
if dir_ls[i] ~= ref_ls[i] then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
local ups = ""
|
|
|
|
for k = i, #ref_ls do
|
2021-05-01 19:18:39 +02:00
|
|
|
ups = ups .. ".." .. PATHSEP
|
2021-03-06 23:36:05 +01:00
|
|
|
end
|
2021-05-01 19:18:39 +02:00
|
|
|
local rel_path = ups .. table.concat(dir_ls, PATHSEP, i)
|
2021-03-06 23:36:05 +01:00
|
|
|
return rel_path ~= "" and rel_path or "."
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-13 19:50:42 +02:00
|
|
|
function common.mkdirp(path)
|
|
|
|
local stat = system.get_file_info(path)
|
|
|
|
if stat and stat.type then
|
|
|
|
return false, "path exists", path
|
|
|
|
end
|
|
|
|
local subdirs = {}
|
|
|
|
while path and path ~= "" do
|
|
|
|
local success_mkdir = system.mkdir(path)
|
|
|
|
if success_mkdir then break end
|
|
|
|
local updir, basedir = path:match("(.*)[/\\](.+)$")
|
|
|
|
table.insert(subdirs, 1, basedir or path)
|
|
|
|
path = updir
|
|
|
|
end
|
|
|
|
for _, dirname in ipairs(subdirs) do
|
|
|
|
path = path and path .. PATHSEP .. dirname or dirname
|
|
|
|
if not system.mkdir(path) then
|
|
|
|
return false, "cannot create directory", path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-06-24 19:54:22 +02:00
|
|
|
function common.rm(path, recursively)
|
|
|
|
local stat = system.get_file_info(path)
|
|
|
|
if not stat or (stat.type ~= "file" and stat.type ~= "dir") then
|
|
|
|
return false, "invalid path given", path
|
|
|
|
end
|
|
|
|
|
|
|
|
if stat.type == "file" then
|
|
|
|
local removed, error = os.remove(path)
|
|
|
|
if not removed then
|
|
|
|
return false, error, path
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local contents = system.list_dir(path)
|
|
|
|
if #contents > 0 and not recursively then
|
|
|
|
return false, "directory is not empty", path
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, item in pairs(contents) do
|
|
|
|
local item_path = path .. PATHSEP .. item
|
|
|
|
local item_stat = system.get_file_info(item_path)
|
|
|
|
|
|
|
|
if not item_stat then
|
|
|
|
return false, "invalid file encountered", item_path
|
|
|
|
end
|
|
|
|
|
|
|
|
if item_stat.type == "dir" then
|
|
|
|
local deleted, error, ipath = common.rm(item_path, recursively)
|
|
|
|
if not deleted then
|
|
|
|
return false, error, ipath
|
|
|
|
end
|
|
|
|
elseif item_stat.type == "file" then
|
|
|
|
local removed, error = os.remove(item_path)
|
|
|
|
if not removed then
|
|
|
|
return false, error, item_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local removed, error = system.rmdir(path)
|
|
|
|
if not removed then
|
|
|
|
return false, error, path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
return common
|