Ceci est une ancienne révision du document !


Version 2.01 : - correction de deep_print(<table>)

-- tools.isNumber(num)
--	  // tools.isNumber(20) -- return true
--	  // tools.isNumber("20") -- return true
-- tools.iif(condition, messageTrue, messageFalse)
--	  // tools.iif(false, "OK", "KO") -- return "KO"
-- tools.cut(message, 10)
--	  // tools.cut("1234567890", 6) -- return "123..." (6 caractère max)
--	  // tools.cut("123456", 6) -- return "123456" (6 caractère max)
-- tools.isNil(variable)
--	  // tools.isNil(coucou) -- return true
-- tools.isNotNil(variable)
--	  // tools.isNotNil(coucou) -- return false
-- tools.split(s:string, delimiter: string)     
--        // tools.split("Comment;Allez;Vous", ";") -- return table {1="Comment", 2="Allez", 3="Vous"}
-- tools.trim(s:string)     
--        // tools.trim("   Comment   ") -- return "Comment"
-- tools.tostring(string | boolean | table | ...)   
--        // tools.tostring({Value=2})  -- return "{'value': 2}"
--        // tools.tostring(true)       -- return "true"
--        // tools.tostring(100)        -- return "1000"
-- tools.log(message: string [, color: string] [, force: boolean])
--        // tools.log("Message", "red", true)  -- display "Message" in red in the debug window
-- tools.info(message: string [, color: string])
--        // tools.info("Message")      -- always display "Message" in white in the debug window
-- tools.error(message: string [, color: string])
--        // tools.error("Message")      -- always display "Message" in red in the debug window
-- tools.warning(message: string [, color: string])
--        // tools.warning("Message")      -- always display "Message" in orange in the debug window
-- tools.debug(message: string [, color: string])
--        // tools.debug("Message")      -- display "Message" in gray in the debug window if debug = true only
-- tools.deep_print(table)
--        // tools.deep_print({1="Comment", 2="Allez", 3="Vous"})   -- print line by line in the debug window
-- tools.getStringTime([seconds: boolean])
--        // tools.getStringTime(true)  -- return "20:35:59"
--        // tools.getStringTime()      -- return "20:35"
-- tools.getStringDate()
--        // tools.getStringDate()      -- return "23/09/2016"
-- tools.toTime(hour: String)
--        // result = tools.toTime("20:35") -- get os.time() object corresponding to the next 20:35 hours

Original :

tools.lua
if (not tools) then 
  tools = { 
    version = "2.01",
    addstyle = "",
    isdebug = false,
    log = function(msg, color, force) 
      msg = tools.tostring(msg)
      for spacestodo, nombre in string.gmatch(msg, "(#spaces(%d+)#)") do
        local dots = ""
        for i = 1, nombre do
          dots = dots .. "."
        end
        msg = string.gsub(msg, spacestodo, "<span style=\"color:black;"..tools.addstyle.."\">"..dots .."</span>")
      end
      if (tools.isdebug or force) then 
        fibaro:debug("<span style=\"color:"..(color or "white")..";"..tools.addstyle.."\">"..msg.."</span>") 
      end 
    end,
    error = function(msg, color) tools.log(msg, color or "red", true) end,
    warning = function(msg, color) tools.log(msg, color or "orange", true) end,
    info = function(msg, color) tools.log(msg, color or "white", true) end,
    debug = function(msg, color) tools.log(msg, color or "gray", false) end,
    tostring = function(what)
      if (type(what) == "boolean") then
        if (what) then return "true" else return "false" end
      elseif (type(what) == "table") then
        if (json) then
          return json.encode(what)
        else
          return "table found"
        end
      else
        return tostring(what)
      end
    end,
    split = function(text, sep)
      local sep, fields = sep or ":", {}
      local pattern = string.format("([^%s]+)", sep)
      text:gsub(pattern, function(c) fields[#fields+1] = c end)
      return fields
    end,
    trim = function(s)
      return (s:gsub("^%s*(.-)%s*$", "%1"))
    end,
    deep_print = function(tbl)
      for i, v in pairs(tbl) do
        if type(v) == "table" then 
          tools.deep_print(v)
        else 
          print(i, v) 
        end
      end
    end,
    iif = function(condition, messagetrue, messagefalse)
      if (condition) then return messagetrue else return messagefalse end
    end, 
    cut = function(message, max)
      max = max or 10
      if (max < message:len()) then return message:sub(1, max-3) .. "..." end
      return message
    end,   
    isNumber = function(num)
      if (type(num)=="number") then return true end
      if (type(num)=="string") then return type(tonumber(num))=="number" end
      return false
    end,
    getStringTime = function(seconds)
      if (seconds) then return os.date("%H:%M:%S") end
      return os.date("%H:%M")
    end,
    toTime = function(hour) 
      local h,m = string.match(hour, "(%d+):(%d+)")
      local d = os.date("*t")
      local t = os.time{year=d.year, month=d.month, day=d.day, hour=h, min=m, sec=0}
      if (t < os.time()) then
        t= os.time{year=d.year, month=d.month, day=(d.day+1), hour=h, min=m, sec=0}
      end
      return t      
    end,
    getStringDate = function()
      return os.date("%d/%m/%Y")
    end,
    isNil = function(variable) 
      return type(variable) == "nil"
    end,
    isNotNil = function(variable) 
      return not tools.isNil(variable)
    end,
  }
end

Version minifiée

toolsminifier.lua
if not tools then tools={version="2.01",addstyle="",isdebug=false,log=function(a,b,c)a=tools.tostring(a)for d,e in string.gmatch(a,"(#spaces(%d+)#)")do local f=""for g=1,e do f=f.."."end;a=string.gsub(a,d,"<span style=\"color:black;"..tools.addstyle.."\">"..f.."</span>")end;if tools.isdebug or c then fibaro:debug("<span style=\"color:"..(b or"white")..";"..tools.addstyle.."\">"..a.."</span>")end end,error=function(a,b)tools.log(a,b or"red",true)end,warning=function(a,b)tools.log(a,b or"orange",true)end,info=function(a,b)tools.log(a,b or"white",true)end,debug=function(a,b)tools.log(a,b or"gray",false)end,tostring=function(h)if type(h)=="boolean"then if h then return"true"else return"false"end elseif type(h)=="table"then if json then return json.encode(h)else return"table found"end else return tostring(h)end end,split=function(i,j)local j,k=j or":",{}local l=string.format("([^%s]+)",j)i:gsub(l,function(m)k[#k+1]=m end)return k end,trim=function(n)return n:gsub("^%s*(.-)%s*$","%1")end,deep_print=function(o)for g,p in pairs(o)do if type(p)=="table"then tools.deep_print(p)else print(g,p)end end end,iif=function(q,r,s)if q then return r else return s end end,cut=function(t,u)u=u or 10;if u<t:len()then return t:sub(1,u-3).."..."end;return t end,isNumber=function(v)if type(v)=="number"then return true end;if type(v)=="string"then return type(tonumber(v))=="number"end;return false end,getStringTime=function(w)if w then return os.date("%H:%M:%S")end;return os.date("%H:%M")end,toTime=function(x)local y,z=string.match(x,"(%d+):(%d+)")local A=os.date("*t")local B=os.time{year=A.year,month=A.month,day=A.day,hour=y,min=z,sec=0}if B<os.time()then B=os.time{year=A.year,month=A.month,day=A.day+1,hour=y,min=z,sec=0}end;return B end,getStringDate=function()return os.date("%d/%m/%Y")end,isNil=function(C)return type(C)=="nil"end,isNotNil=function(C)return not tools.isNil(C)end}end