2023-10-18 04:39:03 +03:00
|
|
|
table.unpack = table.unpack or unpack
|
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
ya = ya or {}
|
2023-10-11 19:09:10 +03:00
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
function ya.clamp(min, x, max)
|
2023-12-01 16:49:02 +03:00
|
|
|
if x < min then
|
|
|
|
return min
|
|
|
|
elseif x > max then
|
|
|
|
return max
|
|
|
|
else
|
|
|
|
return x
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-15 13:30:50 +03:00
|
|
|
function ya.round(x) return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5) end
|
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
function ya.flat(t)
|
2023-10-17 05:49:52 +03:00
|
|
|
local r = {}
|
|
|
|
for _, v in ipairs(t) do
|
|
|
|
if type(v) == "table" then
|
2023-12-26 14:48:33 +03:00
|
|
|
for _, v2 in ipairs(ya.flat(v)) do
|
2023-10-17 05:49:52 +03:00
|
|
|
r[#r + 1] = v2
|
|
|
|
end
|
|
|
|
else
|
|
|
|
r[#r + 1] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
function ya.basename(str) return string.gsub(str, "(.*[/\\])(.*)", "%2") end
|
2023-10-11 19:09:10 +03:00
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
function ya.readable_size(size)
|
2024-01-26 11:35:36 +03:00
|
|
|
local units = { "B", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q" }
|
2023-10-11 19:09:10 +03:00
|
|
|
local i = 1
|
|
|
|
while size > 1024.0 and i < #units do
|
|
|
|
size = size / 1024.0
|
|
|
|
i = i + 1
|
|
|
|
end
|
2024-01-26 11:35:36 +03:00
|
|
|
return string.format("%.1f%s", size, units[i])
|
2023-10-11 19:09:10 +03:00
|
|
|
end
|
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
function ya.readable_path(path)
|
2023-11-02 14:22:05 +03:00
|
|
|
local home = os.getenv("HOME") or os.getenv("USERPROFILE")
|
2024-02-15 15:20:28 +03:00
|
|
|
if not home then
|
2023-10-11 19:09:10 +03:00
|
|
|
return path
|
|
|
|
elseif string.sub(path, 1, #home) == home then
|
|
|
|
return "~" .. string.sub(path, #home + 1)
|
|
|
|
else
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
end
|