always sort contacts alphabetically

- introduce pairsByKeys() (from http://www.lua.org/pil/19.3.html but adding current index and total length as iterator return values)
- use pairsByKeys() everywhere, where contacts are iterated over (for now only when displaying in gui.lua) so the order stays the same although the structure is a hashmap
- correctly update selected position when adding/removing contacts to TO/CC/BCC fields
- remove print statements in storage.lua
This commit is contained in:
Peter Nerlich 2020-08-15 13:52:26 +02:00
parent 39f47437b6
commit 93d1af947c
2 changed files with 51 additions and 21 deletions

View file

@ -36,6 +36,25 @@ mail.getContacts = function(playername)
return mail.read_json_file(mail.getContactsFile(playername))
end
function pairsByKeys(t, f)
-- http://www.lua.org/pil/19.3.html
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function() -- iterator function
i = i + 1
if a[i] == nil then
return nil
else
--return a[i], t[a[i]]
-- add the current position and the length for convenience
return a[i], t[a[i]], i, #a
end
end
return iter
end
mail.setContacts = function(playername, contacts)
if mail.write_json_file(mail.getContactsFile(playername), contacts) then
return true
@ -51,7 +70,6 @@ function mail.read_json_file(path)
local content = {}
if file then
local json = file:read("*a")
print(string.format('read from %s: %s', path, json))
content = minetest.parse_json(json or "[]") or {}
file:close()
end
@ -61,7 +79,6 @@ end
function mail.write_json_file(path, content)
local file = io.open(path,"w")
local json = minetest.write_json(content)
print(string.format('writing to %s: %s', path, json))
if file and file:write(json) and file:close() then
return true
else