--- Delay for a number of seconds.
-- @param delay Number of seconds
function delay_s(delay)
delay = delay or 1
local time_to = os.time() + delay
while os.time() < time_to do end
end
--- Returns string representation of object obj
-- @return String representation of obj
function dir(obj,level)
local s,t = '', type(obj)
level = level or ' '
if (t=='nil') or (t=='boolean') or (t=='number') or (t=='string') then
s = tostring(obj)
if t=='string' then
s = '"' .. s .. '"'
end
elseif t=='function' then s='function'
elseif t=='userdata' then s='userdata'
elseif t=='thread' then s='thread'
elseif t=='table' then
s = '{'
for k,v in pairs(obj) do
local k_str = tostring(k)
if type(k)=='string' then
k_str = '["' .. k_str .. '"]'
end
s = s .. k_str .. ' = ' .. dir(v,level .. level) .. ', '
end
s = string.sub(s, 1, -3)
s = s .. '}'
end
return s
end
--- Pads str to length len with char from left
string.rpad = function(str, len, char)
if char == nil then char = ' ' end
return string.rep(char, len - #str) .. str
end
--- Pads str to length len with char from right
string.lpad = function(str, len, char)
if char == nil then char = ' ' end
return str .. string.rep(char, len - #str)
end
function dump(table, indent)
local indent = indent or ''
for key, value in pairs(table) do
io.write(indent, '[', tostring(key), ']')
if type(value) == "table" then
io.write(':\n') dump(value, indent .. '\t')
else
io.write(' = ', tostring(value), '\n')
end
end
end
-- "Sorted by key" table iterator
-- Extracted from http://www.lua.org/pil/19.3.html
function pairsKeySorted(t, f)
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]]
end
end
return iter
end