forked from mhx/dwarfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwarfs.lua
87 lines (81 loc) · 2.46 KB
/
dwarfs.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
81
82
83
84
85
86
87
--
-- Copyright (c) Marcus Holland-Moritz
--
-- This file is part of dwarfs.
--
-- dwarfs is free software: you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation, either version 3 of the License, or (at your option) any later
-- version.
--
-- dwarfs is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- dwarfs. If not, see <https://www.gnu.org/licenses/>.
--
function filter(f)
-- if f.name == 'Jamroot' or (f.name == 'test' and f.type == 'dir') then
-- return false
-- end
return true
end
function autovivify(C, args, num)
for i = 1, num do
local v = args[i]
if C[v] == nil then C[v] = {} end
C = C[v]
end
return C
end
function incr(C, ...)
local args = { n = select("#", ...), ... }
C = autovivify(C, args, args.n - 2)
local field = args[args.n - 1]
C[field] = (C[field] or 0) + args[args.n]
end
function push(C, ...)
local args = { n = select("#", ...), ... }
C = autovivify(C, args, args.n - 1)
table.insert(C, args[args.n])
end
function sortbysize(tbl)
return function (a, b)
return tbl[b]["size"]/tbl[b]["num"] < tbl[a]["size"]/tbl[a]["num"]
end
end
function order(filelist)
local C = {}
for _, f in pairs(filelist) do
local _, _, base, ext = string.find(f.name, "(.*)(%.%w+)$")
if ext == nil or string.find(ext, "[a-z]") == nil then
base, ext = f.name, ""
end
incr(C, ext, "size", f.size)
incr(C, ext, "num", 1)
incr(C, ext, "name", base, "size", f.size)
incr(C, ext, "name", base, "num", 1)
push(C, ext, "name", base, "files", f)
end
local ordered = {}
local exts = {}
for k, _ in pairs(C) do table.insert(exts, k) end
table.sort(exts, sortbysize(C))
for _, ext in ipairs(exts) do
local N = C[ext]["name"]
local bases = {}
for k, _ in pairs(N) do table.insert(bases, k) end
table.sort(bases, sortbysize(N))
for _, base in ipairs(bases) do
local files = N[base]["files"]
table.sort(files, function (a, b)
return b.size < a.size
end)
for _, file in ipairs(files) do
table.insert(ordered, file)
end
end
end
return ordered
end