aboutsummaryrefslogtreecommitdiffstats
path: root/Utils.lua
blob: 5e06f97e67663a5096b23f44da7a319d4e182f35 (plain)
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
local Utils = {}

function Utils.percentCoordinates(x, y)
    x = (x/100)*Config.width
    y = (y/100)*Config.height
    return x, y
end

function Utils.tprint(tbl, indent)
if not indent then indent = 0 end
for k, v in pairs(tbl) do
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
Utils.tprint(v, indent+1)
else
print(formatting .. v)
end
end
end

function Utils.tuple2ToIndexHash(table)
	local tmp = {}
	for k,v in pairs(table) do
		tmp[v[1]] = k
	end
	return tmp
end

-- From http://lua-users.org/wiki/CopyTable
function Utils.copyTable(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[Utils.copyTable(orig_key)] = Utils.copyTable(orig_value)
        end
        setmetatable(copy, Utils.copyTable(getmetatable(orig)))
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end

function Utils.sortByArtistAndTitle(a, b)
	if a.artist:lower() < b.artist:lower() then
		return true
	elseif a.artist:lower() == b.artist:lower() and a.title:lower() < b.title:lower() then
		return true
	else
		return false
	end
end

return Utils