Last active
December 6, 2023 23:19
-
-
Save Be1zebub/e899e6f606d99c31f279323860d0c771 to your computer and use it in GitHub Desktop.
table.Print deprecated version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Deprecated version, see https://github.com/Facepunch/garrysmod/pull/2033 for more details | |
-- Published for the archive | |
--[[--------------------------------------------------------- | |
table.Print | |
A better version of PrintTable | |
-----------------------------------------------------------]] | |
local tostringGmodTypes | |
do | |
local format1 = "%s( %s )" | |
local format2 = "%s( %s, %s )" | |
local format3 = "%s( %s, %s, %s )" | |
local format4 = "%s( %s, %s, %s, %s )" | |
local function Round( num ) | |
return math.Round( num, 3 ) | |
end | |
local formaters = {} | |
function formaters.Vector( vec ) | |
if ( vec.z == 0 ) then | |
if ( vec.y == 0 ) then | |
return format1:format( "Vector", Round( vec.x ) ) | |
end | |
return format2:format( "Vector", Round( vec.x ), Round( vec.y ) ) | |
end | |
return format3:format( "Vector", Round( vec.x ), Round( vec.y ), Round( vec.z ) ) | |
end | |
function formaters.Angle( ang ) | |
if ( ang.r == 0 ) then | |
if ( ang.y == 0 ) then | |
return format1:format( "Angle", Round( ang.p ) ) | |
end | |
return format2:format( "Angle", Round( ang.p ), Round( ang.y ) ) | |
end | |
return format3:format( "Angle", Round( ang.p ), Round( ang.y ), Round( ang.r ) ) | |
end | |
function formaters.Color( col ) | |
if ( col.a == 255 ) then | |
return format3:format( "Color", col.r, col.g, col.b ) | |
end | |
return format4:format( "Color", col.r, col.g, col.b, col.a ) | |
end | |
function tostringGmodTypes( val ) | |
local format = formaters[ type( val ) ] | |
if ( format ) then | |
return format(val) | |
end | |
if ( IsColor( val ) ) then | |
return formaters.Color( val ) | |
end | |
return tostring( val ) | |
end | |
end | |
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 | |
function table.ToPlain( tbl, nopretty, lvl, already ) | |
already = already or {[ tbl ] = true} | |
lvl = lvl or 1 | |
local out = {} | |
local len = 0 | |
for _ in pairs( tbl ) do | |
len = len + 1 | |
end | |
if ( len == 0 ) then | |
return "{}" | |
end | |
local isSeq = len == #tbl | |
local not_isSeq = not isSeq | |
local latest | |
if ( not_isSeq ) then | |
local new = {} | |
local id = 0 | |
for k, v in pairs( tbl ) do | |
id = id + 1 | |
new[ id ] = { 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 tostringGmodTypes( a.k ) < tostringGmodTypes( b.k ) | |
end ) | |
tbl = new | |
end | |
local iter = isSeq and ipairs or pairs | |
for k, v in iter( tbl ) do | |
if ( not_isSeq ) then | |
k, v = v.k, v.v | |
end | |
if ( isSeq ) then | |
k = "" | |
elseif ( isstring( k ) == false ) then | |
k = "[ " .. tostringGmodTypes( k ) .. " ] = " | |
elseif ( IsValidKeyName( k ) ) then | |
k = k .. " = " | |
else | |
k = "[ \"" .. k .. "\" ] = " | |
end | |
if ( type( v ) == "table" and already[ v ] == nil ) then | |
latest = v | |
already[ v ] = true | |
out[ #out + 1 ] = k .. table.ToPlain( v, nopretty, lvl + 1, already ) | |
else | |
latest = v | |
out[ #out + 1 ] = k .. ( | |
isstring( v ) and string.format( "%q", v ) or tostringGmodTypes( v ) | |
) | |
end | |
end | |
if ( len == 1 and istable( latest ) == false ) then | |
return "{" .. out[ 1 ] .. "}" | |
end | |
if ( nopretty ) then | |
return "{" .. table.concat( out, ", " ) .. "}" | |
else | |
local indent = string.rep( "\t", lvl ) | |
return "{\n" .. indent .. table.concat( out, ",\n" .. indent ) .. "\n" .. string.rep( "\t", lvl - 1 ) .. "}" | |
end | |
end | |
function table.Print( tbl, nopretty ) | |
print( table.ToPlain( tbl, nopretty ) ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment