Guest User

PrintTableDepth.lua

a guest
Mar 8th, 2015
206
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[---------------------------------------------------------
  2.     Prints also a table to the console but a bit nicer
  3.     PrintTableDepth( Table toPrint, Number depth, Number max )
  4.     toPrint:    The table to be printed
  5.     depth:      Amount of child-tables to open (-1 = inf, -1 <= depth)
  6.     max:        Amount of elements to print per table (-1 = inf, -1 <= max)
  7. -----------------------------------------------------------]]
  8. local tcol = {
  9.     ["string"] = Color(180,180,180),
  10.     ["table"] = Color(160,50,50),
  11.     ["number"] = Color(255,130,0),
  12.     ["Vector"] = Color(255,130,0),
  13.     ["Angle"] = Color(255,130,0),
  14.     ["Color"] = Color(255,130,0),
  15.     ["boolean"] = Color(0,255,160),
  16.     ["function"] = Color(20,130,180),
  17.     ["Entity"] = Color(70,255,160),
  18.     ["Weapon"] = Color(70,255,160),
  19.     ["Player"] = Color(70,255,160)
  20. }
  21.  
  22. local bc = Color(120,255,70)    --bracket color
  23. local ec = Color(200,200,50)    --equal sign color
  24. local nc = Color(255,255,255)   --normal color
  25. local sc = Color(180,180,180)   --shadow color
  26. local cc = Color(50,150,50)     --comment color
  27. local tc = Color(70,200,255)    --type function color
  28.  
  29. tostring2 = function(val,new)
  30.     local tp = type(val)
  31.     local tco = (tcol[tp] and new) and tcol[tp] or Color(200,200,200)
  32.     if tp == "string" then
  33.         MsgC(tco,"\""..string.Replace(val,"\n","\\n").."\"")
  34.     elseif tp == "Vector" then
  35.         MsgC(tc,"Vector",nc,"(",tco,val.x,nc,", ",tco,val.y,nc,", ",tco,val.z,nc,")")
  36.     elseif tp == "Angle" then
  37.         MsgC(tc,"Angle",nc,"(",tco,val.p,nc,", ",tco,val.y,nc,", ",tco,val.r,nc,")")
  38.     elseif tp == "Color" then
  39.         if val.a != nil then
  40.             MsgC(tc,"Color",nc,"(",tco,val.p,nc,", ",tco,val.y,nc,", ",tco,val.r,nc,", ",tco,val.a,nc,")")
  41.         else
  42.             MsgC(tc,"Color",nc,"(",tco,val.p,nc,", ",tco,val.y,nc,", ",tco,val.r,nc,")")
  43.         end
  44.     elseif tp == "function" then
  45.         MsgC(tco,"\""..tostring(val).."\"")
  46.     elseif tp == "Entity" or tp == "Weapon" or tp == "Player" then
  47.         local str = ""
  48.         for w in string.gmatch(tostring(val), "%[(%d+)%]") do
  49.             str = w
  50.             break
  51.         end
  52.         MsgC(tc,"Entity",nc,"(",tco,str,nc,")",cc,"/*"..tp.."*/")
  53.     else
  54.         MsgC(tco,tostring(val))
  55.     end
  56.     if new then
  57.         MsgC(nc,",\n")
  58.     end
  59. end
  60.  
  61. local function tbl_rec(tbl,depth,current,max)
  62.     current = current + 1
  63.     local indent = string.rep( "\t", current )
  64.     if current <= depth or depth == -1 then
  65.         local c = 0
  66.         local c2 = 0
  67.         for k,v in pairs(tbl) do
  68.             c = c + 1
  69.             if c <= max or max == -1 then
  70.                 if type(v) == "table" then
  71.                     MsgC(sc,indent.."[") tostring2(k,false) MsgC(sc,"] ",ec,"=",nc," ",bc,"{\n")
  72.                     tbl_rec(v,depth,current,max)
  73.                     MsgC(sc,indent,bc,"}",nc,",\n")
  74.                 else
  75.                     MsgC(sc,indent.."[") tostring2(k,false) MsgC(sc,"] ",ec,"=",nc," ")
  76.                     tostring2(v,true)
  77.                 end
  78.             else
  79.                 c2 = c2 + 1
  80.             end
  81.         end
  82.         if c2 > 0 then
  83.             if c2 != 1 then
  84.                 MsgC(cc,indent.."/* ",c2," more elements. (amount restriced)*/\n")
  85.             else
  86.                 MsgC(cc,indent.."/* ",c2," more element. (amount restriced)*/\n")
  87.             end
  88.         end
  89.     else
  90.         local c = 0
  91.         for k,v in pairs(tbl) do
  92.             c = c + 1
  93.         end
  94.         if c != 1 then
  95.             MsgC(cc,indent.."/* ",c," elements. (depth restriced)*/\n")
  96.         else
  97.             MsgC(cc,indent.."/* ",c," element. (depth restriced)*/\n")
  98.         end
  99.     end
  100. end
  101.  
  102. function PrintTableDepth(tab,depth,max)
  103.     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
  104.     MsgC(tc,"ROOT ",ec,"=",nc," ",bc,"{\n")
  105.     tbl_rec(tab,depth,0,max)
  106.     MsgC(bc,"}\n\n")
  107. end
RAW Paste Data