Krishna (owner)

Revisions

gist: 99829 Download_button fork
public
Public Clone URL: git://gist.github.com/99829.git
Embed All Files: show embed
Lua #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
--
-- vardump
-- original verstion by Tobias Sulzenbruck and Christoph Beckmann
-- source: Lua Gems, page 29
-- modifications: Krishna Kotecha
--
 
--[[
value - the value to dump
 
key - checked for adding a prefix. Used for printing tables
and for printing out the index of the table cell.
depth - used for indentation
options - a table used to pass in additional information to this function:
options.metatable - a boolean flag used to indicate whether
the current table passed in value is a metatable.
--]]
function vardump(value, depth, key, options)
options = options or {}
local linePrefix = ""
local spaces = ""
 
-- add a line prefix if necessary...
if key ~= nil then
linePrefix = "["..key.."] = "
end
 
-- depending on the table depth, add spaces to
-- the front of each line for readability
if depth == nil then
depth = 0
else
depth = depth + 1
for i = 1, depth do
spaces = spaces .. " "
end
end
 
if type(value) == 'table' then
tableDescription = '(table) '
if options.metatable ~= nil then
tableDescription = '(- metatable) '
end
 
print(spaces .. linePrefix .. tableDescription)
 
-- deal with the current table's metatable...
mTable = getmetatable(value)
if mTable ~= nil then
vardump(mTable, depth + 1, nil, {metatable = true})
end
 
-- now print out the table...
for tableKey, tableValue in pairs(value) do
vardump(tableValue, depth, tableKey)
end
 
elseif type(value) == 'function'
or type(value) == 'thread'
or type(value) == 'userdata'
or type(value) == 'nil'
then
-- print(spaces .. tostring(value))
print(spaces .. linePrefix .. tostring(value))
else
print(spaces .. linePrefix .. "(" .. type(value) .. ") " .. tostring(value))
end
 
end