daily pastebin goal
14%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local key_col = Color(0,255,255)
  2. local str_col = Color(255,255,0)
  3. local vec_ang_col = Color(255,0,255)
  4. local ent_col = Color(0,255,0)
  5. local func_col = Color(0,0,255)
  6. local white = Color(255,255,255)
  7. local red = Color(255,0,0)
  8. local green = Color(0,255,0)
  9.  
  10. local function print_var(no_recurse, longest_tbl, longest, indentation_level, indent, k, v, no_recurse)
  11.     local pretty_spacing = (" "):rep(math.max(longest, longest_tbl) - utf8.len(tostring(k)))
  12.     if (type(v) == "table") then
  13.         if (IsColor(v)) then
  14.             if (v.a < 255) then
  15.                 MsgC(indent, key_col, k, white, pretty_spacing, " = ", v, "Color(r: " .. v.r .. ", g: " .. v.b .. ", b: " .. v.g .. ", a: " .. v.a .. ")\n")
  16.             else
  17.                 MsgC(indent, key_col, k, white, pretty_spacing, " = ", v, "Color(r: " .. v.r .. ", g: " .. v.b .. ", b: " .. v.g .. ")\n")
  18.             end
  19.         else
  20.             if (no_recurse ~= nil and no_recurse[v]) then
  21.                 MsgC(indent, key_col, k, white, pretty_spacing, " = ", red, "[recursive table]\n")
  22.             else
  23.                 local new_no_recurse = table.Copy(no_recurse or {})
  24.                 new_no_recurse[v] = true
  25.  
  26.                 if (next(v) == nil) then
  27.                     MsgC(indent, key_col, k, white, pretty_spacing, " = ", str_col, "[0] ", key_col, "{}\n")
  28.                 else
  29.                     MsgC(indent, key_col, k, white, " = ", str_col, "[" .. table.Count(v) .. "] ", key_col, "{\n")
  30.                     var_dump(k, v, indentation_level + 1, new_no_recurse)
  31.                     MsgC(indent, key_col, "}\n")
  32.                 end
  33.             end
  34.         end
  35.     elseif (type(v) == "string") then
  36.         MsgC(indent, key_col, k, white, pretty_spacing, " = ", str_col, "\"" .. v .. "\"\n")
  37.     elseif (isvector(v)) then
  38.         MsgC(indent, key_col, k, white, pretty_spacing, " = ", vec_ang_col, "Vector(x: " .. v.x .. ", y: " .. v.y .. ", z: " .. v.z .. ")\n")
  39.     elseif (isangle(v)) then
  40.         MsgC(indent, key_col, k, white, pretty_spacing, " = ", vec_ang_col, "Angle(y: " .. v.y .. ", p: " .. v.p .. ", r: " .. v.r .. ")\n")
  41.     elseif (isentity(v)) then
  42.         if (not IsValid(v)) then
  43.             MsgC(indent, key_col, k, white, pretty_spacing, " = ", red, "[INVALID]", ent_col, tostring(v), "\n")
  44.         else
  45.             if (v:IsWorld()) then
  46.                 MsgC(indent, key_col, k, white, pretty_spacing, " = ", ent_col, "[0] worldspawn\n")
  47.             elseif (v:IsPlayer()) then
  48.                 if (v:IsBot()) then
  49.                     MsgC(indent, key_col, k, white, pretty_spacing, " = ", team.GetColor(v:Team()), "[BOT #" .. v:SteamID64() - 90071996842377216 + 1 .. "] " .. v:Nick() .. "\n")
  50.                 else
  51.                     MsgC(indent, key_col, k, white, pretty_spacing, " = ", team.GetColor(v:Team()), "[" .. v:SteamID() .. " #" .. v:UserID() .. "] " .. v:Nick() .. "\n")
  52.                 end
  53.             elseif (v.GetPrintName ~= nil) then
  54.                 MsgC(indent, key_col, k, white, pretty_spacing, " = ", ent_col, "[" .. v:EntIndex() .. "] [" .. v:GetClass() .. "] " .. v:GetPrintName() .. "\n")
  55.             else
  56.                 MsgC(indent, key_col, k, white, pretty_spacing, " = ", ent_col, "[" .. v:EntIndex() .. "] " .. v:GetClass() .. "\n")
  57.             end
  58.         end
  59.     elseif (isfunction(v)) then
  60.         local params = ""
  61.         for i=1,debug.getinfo(v).nparams do
  62.             params = params .. i .. ", "
  63.         end
  64.         MsgC(indent, key_col, k, white, pretty_spacing, " = ", func_col, "[" .. (tostring(v):gsub("^function: ", "")) .. "] function(" .. (params:gsub(", $", "")) .. ")" .. "\n")
  65.     elseif (type(v) == "number") then
  66.         MsgC(indent, key_col, k, white, pretty_spacing, " = ", str_col, tostring(v) .. "\n")
  67.     elseif (type(v) == "boolean") then
  68.         if (v == true) then
  69.             MsgC(indent, key_col, k, white, pretty_spacing, " = ", green, tostring(v) .. "\n")
  70.         else
  71.             MsgC(indent, key_col, k, white, pretty_spacing, " = ", red, tostring(v) .. "\n")
  72.         end
  73.     else
  74.         MsgC(indent, key_col, k, white, pretty_spacing, " = ", str_col, tostring(v) .. "\n")
  75.     end
  76. end
  77.  
  78. function var_dump(str, tbl, indentation_level, no_recurse)
  79.     local indentation_level = indentation_level or 0
  80.     local indent = (" "):rep(indentation_level * 4)
  81.     if (tbl == nil) then return MsgC(indent, red, "Input was nil\n") end
  82.     if (type(tbl) == "table") then
  83.         local no_recurse = no_recurse or {}
  84.         local keys = table.GetKeys(tbl)
  85.  
  86.         table.sort(keys, function(a, b)
  87.             if (isnumber(a) and isnumber(b)) then return a < b end
  88.             return tostring(a) < tostring(b)
  89.         end)
  90.  
  91.         local longest = 0
  92.         local longest_tbl = 0
  93.         for _,k in ipairs(keys) do
  94.             local len = utf8.len(tostring(k))
  95.             if (len > longest) then
  96.                 longest = len
  97.             end
  98.             if (len > longest_tbl and type(tbl[k]) == "table") then
  99.                 longest_tbl = len
  100.             end
  101.         end
  102.         for _,k in ipairs(keys) do
  103.             local v = tbl[k]
  104.             print_var(no_recurse, longest_tbl, longest, indentation_level, indent, k, v, no_recurse)
  105.         end
  106.     else
  107.         print_var(nil, 0, 0, indentation_level, indent, str, tbl, no_recurse)
  108.     end
  109. end
  110.  
  111. concommand.Add("var_dump", function(ply, __, ___, args)
  112.     if (SERVER and IsValid(ply)) then return end
  113.     local worked = xpcall(function() RunString("var_dump(\"" .. (args:gsub("\"", "\\\"")) .. "\"," .. args .. ")") end, function() end)
  114.     if (not worked) then
  115.         MsgC(red, "Failed\n")
  116.     end
  117. end)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top