Guest User

PrintTableDepth.lua

a guest
Mar 7th, 2015
272
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //PrintTableDepth by Uke
  2.  
  3. local tcol = {
  4.     ["string"] = Color(130,130,130),
  5.     ["table"] = Color(160,50,50),
  6.     ["number"] = Color(255,130,0),
  7.     ["Vector"] = Color(255,130,0),
  8.     ["Angle"] = Color(255,130,0),
  9.     ["Color"] = Color(255,130,0),
  10.     ["boolean"] = Color(0,255,160),
  11.     ["function"] = Color(20,130,180)
  12. }
  13.  
  14. local bc = Color(120,255,70)
  15. local ec = Color(200,200,50)
  16. local nc = Color(255,255,255)
  17. local sc = Color(180,180,180)
  18. local cc = Color(50,150,50)
  19. local tc = Color(70,200,255)
  20.  
  21. oldtostring = tostring
  22.  
  23. tostring2 = function(val)
  24.     local tp = type(val)
  25.     local tco = tcol[tp]
  26.     if tp == "string" then
  27.         MsgC(tco,"\""..string.Replace(val,"\n","\\n").."\"",nc,",\n")
  28.     elseif tp == "Vector" then
  29.         MsgC(tc,"Vector",nc,"(",tco,val.x,nc,", ",tco,val.y,nc,", ",tco,val.z,nc,"),\n")
  30.     elseif tp == "Angle" then
  31.         MsgC(tc,"Angle",nc,"(",tco,val.p,nc,", ",tco,val.y,nc,", ",tco,val.r,nc,"),\n")
  32.     elseif tp == "Color" then
  33.         if val.a != nil then
  34.             MsgC(tc,"Color",nc,"(",tco,val.p,nc,", ",tco,val.y,nc,", ",tco,val.r,nc,", ",tco,val.a,nc,"),\n")
  35.         else
  36.             MsgC(tc,"Color",nc,"(",tco,val.p,nc,", ",tco,val.y,nc,", ",tco,val.r,nc,"),\n")
  37.         end
  38.     elseif tp == "function" then
  39.         MsgC(tco,"\""..oldtostring(val).."\"",nc,",\n")
  40.     else
  41.         MsgC(tco,oldtostring(val),nc,",\n")
  42.     end
  43. end
  44.  
  45. local function tbl_rec(tbl,depth,current,max)
  46.     current = current + 1
  47.     local indent = string.rep( "\t", current )
  48.     if current <= depth or depth == -1 then
  49.         local c = 0
  50.         local c2 = 0
  51.         for k,v in pairs(tbl) do
  52.             c = c + 1
  53.             if c <= max or max == -1 then
  54.                 if type(v) == "table" then
  55.                     MsgC(sc,indent.."[\"",nc,k,sc,"\"] ",ec,"=",nc," ",bc,"{\n")
  56.                     tbl_rec(v,depth,current,max)
  57.                     MsgC(sc,indent,bc,"}",nc,",\n")
  58.                 else
  59.                     MsgC(sc,indent.."[\"",nc,k,sc,"\"] ",ec,"=",nc," ")
  60.                     tostring2(v)
  61.                 end
  62.             else
  63.                 c2 = c2 + 1
  64.             end
  65.         end
  66.         if c2 > 0 then
  67.             if c2 != 1 then
  68.                 MsgC(cc,indent.."/* ",c2," more elements. (amount restriced)*/\n")
  69.             else
  70.                 MsgC(cc,indent.."/* ",c2," more element. (amount restriced)*/\n")
  71.             end
  72.         end
  73.     else
  74.         local c = 0
  75.         for k,v in pairs(tbl) do
  76.             c = c + 1
  77.         end
  78.         if c != 1 then
  79.             MsgC(cc,indent.."/* ",c," elements. (depth restriced)*/\n")
  80.         else
  81.             MsgC(cc,indent.."/* ",c," element. (depth restriced)*/\n")
  82.         end
  83.     end
  84. end
  85.  
  86. function PrintTableDepth(tab,depth,max)
  87.     if tab == nil or type(tab) != "table" or depth == nil or type(depth) != "number" or depth < -1 or type(max) != "number" or max < -1 or max == nil then MsgC(Color(255,0,0),"\n\nManual:",Color(200,255,255),"\nPrintTableDepth( Table toPrint, Number depth, Number max )\ntoPrint: \tThe table to be printed\ndepth: \t\tAmount of child-tables to open (-1 = inf, -1 <= depth)\nmax: \t\tAmount of elements to print per table (-1 = inf, -1 <= max)\n\n") return end
  88.     MsgC(tc,"ROOT ",ec,"=",nc," ",bc,"{\n")
  89.     tbl_rec(tab,depth,0,max)
  90.     MsgC(bc,"}\n\n")
  91. end
RAW Paste Data