-
Notifications
You must be signed in to change notification settings - Fork 11
/
printtable_v2.lua
196 lines (157 loc) · 4.43 KB
/
printtable_v2.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
-- a better version of https://github.com/Be1zebub/Small-GLua-Things/blob/master/printtable.lua
-- created for pr: https://github.com/Facepunch/garrysmod/pull/2033
-- features: lua syntax correct output, syntax highlighting, some gmod types support, show function src
local white = Color( 230, 230, 230 )
local red = Color( 212, 53, 109 )
local yellow = Color( 212, 202, 107 )
local purple = Color( 157, 110, 212 )
local cyan = Color( 102, 217, 239 )
local dark = Color( 105, 105, 105 )
local IsValidKeyName
do
local reserved = { [ "and" ] = true, [ "break" ] = true, [ "do" ] = true, [ "else" ] = true, [ "elseif" ] = true, [ "end" ] = true, [ "false" ] = true, [ "for" ] = true, [ "function" ] = true, [ "if" ] = true, [ "in" ] = true, [ "local" ] = true, [ "nil" ] = true, [ "not" ] = true, [ "or" ] = true, [ "repeat" ] = true, [ "return" ] = true, [ "then" ] = true, [ "true" ] = true, [ "until" ] = true, [ "while" ] = true}
function IsValidKeyName( key )
-- name cant be reserved word
if reserved[ key ] then return false end
-- it should start with letters or underscore, It should continue with alphanumerics
if ( key:match( "^[%a_][%w_]*$" ) == nil ) then return false end
return true
end
end
local MsgC = MsgC
if SERVER and system.IsLinux() then
function MsgC( ... ) -- polyfill to make MsgC works in linux srcds
for _, v in ipairs( { ... } ) do
if ( IsColor( v ) ) then
Msg( string.format( "\27[38;2;%d;%d;%dm", v.r, v.g, v.b ) )
else
Msg( v )
end
end
Msg("\27[0m")
end
end
local function Round( num )
return math.Round( num, 3 )
end
local gmodObjects = {}
function gmodObjects.Player( v )
MsgC( cyan, "Player", white, "( ", purple, v:UserID(), white, " )" )
end
function gmodObjects.Entity( v )
MsgC( cyan, "Entity", white, "( ", purple, v:EntIndex(), white, " )" )
end
local gmodAxisObjects = { Vector = true, Angle = true }
local function MsgValue( v )
if isstring( v ) then
if v:find( "\n", 1, true ) then
MsgC( yellow, "[[", v, "]]" )
else
MsgC( yellow, "\"", v, "\"" )
end
elseif IsColor( v ) then
MsgC(
cyan, "Color", white, "( ",
purple, v.r, red, ", ",
purple, v.g, red, ", ",
purple, v.b
)
if v.a ~= 255 then
MsgC(
red, ", ",
purple, v.a
)
end
MsgC( white, " )" )
elseif gmodObjects[ type( v ) ] then
gmodObjects[ type( v ) ]( v )
elseif gmodAxisObjects[ type( v ) ] then
MsgC(
cyan, type( v ), white, "( ",
purple, Round( v[ 1 ] )
)
if v[ 2 ] ~= 0 then
MsgC( red, ", ", purple, Round( v[ 2 ] ) )
end
if v[ 3 ] ~= 0 then
MsgC( red, ", ", purple, Round( v[ 3 ] ) )
end
MsgC( white, " )" )
elseif isfunction( v ) then
local info = debug.getinfo( v )
local defined = info.linedefined == info.lastlinedefined and info.linedefined or info.linedefined .. "-" .. info.lastlinedefined
MsgC( purple, v, dark, " --[[ ", info.short_src, ":", defined, " ]]" )
else
MsgC( purple, v )
end
end
local function MsgKey( indent, k )
if ( isstring( k ) == false ) then
MsgC( indent, red, "[ " )
MsgValue( k )
MsgC( red, " ] = " )
elseif ( IsValidKeyName( k ) ) then
MsgC( indent, white, k, red, " = " )
else
MsgC( indent, red, "[ ", yellow, "\"", k, yellow, "\"", red, " ] = " )
end
end
function table.Print( tbl, lvl, already )
already = already or { [ tbl ] = true }
lvl = lvl or 1
local len = 0
for _ in pairs( tbl ) do
len = len + 1
end
if ( len == 0 ) then
return MsgC( red, "{}" )
end
local isSeq = len == #tbl
local not_isSeq = not isSeq
if ( not_isSeq ) then
local new = {}
local index = 0
for k, v in pairs( tbl ) do
index = index + 1
new[ index ] = { k = k, v = v }
end
table.sort( new, function( a, b )
if ( isnumber( a.k ) and isnumber( b.k ) ) then
return a.k < b.k
end
return tostring( a.k ) < tostring( b.k )
end )
tbl = new
end
local iter = isSeq and ipairs or pairs
local indent = string.rep( "\t", lvl )
local table_indent = string.rep( "\t", lvl - 1 )
local i = 1
MsgC( red, "{\n" )
for k, v in iter( tbl ) do
if ( isSeq ) then
Msg( indent )
else
k, v = v.k, v.v
MsgKey( indent, k )
end
if ( istable( v ) and IsColor( v ) == false ) then
if already[ v ] then
MsgC( purple, tostring( v ) )
else
already[ v ] = true
table.Print( v, lvl + 1, already )
end
else
MsgValue( v )
end
if i == len then
Msg( "\n" )
else
MsgC( red, ",\n" )
end
i = i + 1
end
MsgC( table_indent, red, "}" )
if ( lvl == 1 ) then Msg( "\n" ) end
end