annotate Resources/Toolbox.lua @ 384:18fe778eeb95 lua-scripting

wrapper around lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Apr 2013 16:59:31 +0200
parents
children 9784f19f7e1b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
384
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 --[[ rPrint(struct, [limit], [indent]) Recursively print arbitrary data.
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 Set limit (default 100) to stanch infinite loops.
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 Indents tables as [KEY] VALUE, nested tables as [KEY] [KEY]...[KEY] VALUE
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 Set indent ("") to prefix each line: Mytable [KEY] [KEY]...[KEY] VALUE
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 https://gist.github.com/stuby/5445834#file-rprint-lua
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 --]]
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 function rPrint(s, l, i) -- recursive Print (structure, limit, indent)
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 l = (l) or 100; i = i or ""; -- default item limit, indent string
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 if (l<1) then print "ERROR: Item limit reached."; return l-1 end;
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 local ts = type(s);
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 if (ts ~= "table") then print (i,ts,s); return l-1 end
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 print (i,ts); -- print "table"
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 for k,v in pairs(s) do -- print "[KEY] VALUE"
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 l = rPrint(v, l, i.."\t["..tostring(k).."]");
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 if (l < 0) then break end
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 end
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 return l
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 end