-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.lua
80 lines (70 loc) · 1.6 KB
/
utils.lua
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
local conf = require("config")
local M = {}
function M.in_array(t, value)
for k,v in ipairs(t) do
if v == value then
return true
end
end
end
function M.read_file(file)
local f = assert(io.open(file))
local file = f:read("*all")
f:close()
return file
end
function M.write_file(file, string)
file = assert(io.open(file, "w"))
file:write(string)
file:close()
end
----
-- convert array to list
function M.to_list(t)
local res = {}
for k,v in ipairs(t) do
res[v] = k
end
return res
end
----
-- table iterator which sorts on keys
function M.kpairs(t, f)
local keys = {}
for k in pairs(t) do keys[#keys + 1] = k end
table.sort(keys,f)
local i = 0
return function()
i = i + 1
return keys[i], t[keys[i]]
end
end
----
-- branch sort function for kpairs
function M.sort_branch(a,b)
if b == 'edge' then return true end
if a == 'edge' then return false end
local major_a, minor_a = a:match("(%d+)%.(%d+)")
local major_b, minor_b = b:match("(%d+)%.(%d+)")
if tonumber(major_a) < tonumber(major_b) then return true end
if major_a == major_b then
if tonumber(minor_a) < tonumber(minor_b) then return true end
end
end
----
-- repo sort function for kpairs
function M.sort_repo(a,b)
local repos = M.to_list(conf.repos)
if type(repos[a]) == "number" and type(repos[b]) == "number" then
if repos[a] < repos[b] then return true end
end
end
----
-- arch sort function for kpairs
function M.sort_arch(a,b)
local archs = M.to_list(conf.archs)
if type(archs[a]) == "number" and type(archs[b]) == "number" then
if archs[a] < archs[b] then return true end
end
end
return M