aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils.lua')
-rw-r--r--lib/utils.lua47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/utils.lua b/lib/utils.lua
new file mode 100644
index 0000000..909ec49
--- /dev/null
+++ b/lib/utils.lua
@@ -0,0 +1,47 @@
+local json = require "lib.json"
+
+local M = {}
+
+function M.get_palette()
+ local f = io.open("palette.json", "r")
+ if not f then error("couldn't open palette.json") end
+ local data = f:read("a")
+ f:close()
+ return json.decode(data)
+end
+
+-- This is taken from http://lua-users.org/wiki/SortedIteration
+-- This version is stripped of comments and empty lines + some stuff is renamed
+
+local function __gen_oindex(t)
+ local oindex = {}
+ for key in pairs(t) do
+ table.insert(oindex, key)
+ end
+ table.sort(oindex)
+ return oindex
+end
+
+function M.onext(t, state)
+ local key = nil
+ if state == nil then
+ t.__oindex = __gen_oindex(t)
+ key = t.__oindex[1]
+ else
+ for i = 1, #t.__oindex do
+ if t.__oindex[i] == state then
+ key = t.__oindex[i + 1]
+ end
+ end
+ end
+ if key then
+ return key, t[key]
+ end
+ t.__oindex = nil
+end
+
+function M.opairs(t)
+ return M.onext, t, nil
+end
+
+return M