Skip to content

Commit d00aed1

Browse files
authored
Merge pull request #99 from OrangeCash090/master
BetterPrint.lua
2 parents 48eb595 + d77f0a9 commit d00aed1

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

Libs/BetterPrint.lua

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
-- Made by OrangeCash
2+
3+
function isArray(t)
4+
if type(t) ~= "table" then return false end
5+
local count = 0
6+
for k, _ in pairs(t) do
7+
if type(k) ~= "number" or k % 1 ~= 0 or k < 1 then return false end
8+
count = count + 1
9+
end
10+
for i = 1, count do
11+
if t[i] == nil then return false end
12+
end
13+
return true
14+
end
15+
16+
local typeOrder = {
17+
number = 1,
18+
string = 2,
19+
boolean = 3,
20+
table = 4,
21+
["function"] = 5,
22+
thread = 6,
23+
userdata = 7
24+
}
25+
26+
local bracketColors = {"§6", "§u", "§8"}
27+
28+
local function typeRank(v)
29+
return typeOrder[type(v)] or 99
30+
end
31+
32+
local function isOneDimensionalArray(tbl)
33+
for _, v in ipairs(tbl) do
34+
if type(v) == "table" then
35+
return false
36+
end
37+
end
38+
return true
39+
end
40+
41+
local function stringify(value, depth)
42+
local maxValues = 8
43+
depth = depth or 1
44+
45+
local bracketColor = bracketColors[(depth - 1) % #bracketColors + 1]
46+
47+
local function applyColor(val, color)
48+
return color .. val .. "§r"
49+
end
50+
51+
local function indentLine(level)
52+
return string.rep(" ", level)
53+
end
54+
55+
local t = type(value)
56+
57+
if t == "nil" then
58+
return applyColor("nil", "§t")
59+
elseif t == "number" then
60+
return applyColor(tostring(value), "§a")
61+
elseif t == "string" then
62+
return applyColor('"' .. value .. '"', "§n")
63+
elseif t == "boolean" then
64+
return applyColor(tostring(value), "§t")
65+
elseif t == "function" then
66+
return applyColor("<function>", "§m")
67+
elseif t == "thread" then
68+
return applyColor("<thread>", "§m")
69+
elseif t == "userdata" then
70+
return applyColor("<userdata>", "§m")
71+
elseif t == "table" then
72+
if getmetatable(value) and getmetatable(value).__tostring then
73+
return getmetatable(value).__tostring(value)
74+
end
75+
76+
local result = {}
77+
local count = 0
78+
local is_array = isArray(value)
79+
local oneDim = is_array and isOneDimensionalArray(value)
80+
local shouldIndent = not oneDim
81+
82+
if is_array then
83+
for _, v in ipairs(value) do
84+
count = count + 1
85+
if count >= maxValues then
86+
table.insert(result, shouldIndent and indentLine(depth) .. "..." or "...")
87+
break
88+
end
89+
local valStr = stringify(v, depth + 1)
90+
table.insert(result, shouldIndent and indentLine(depth) .. valStr or valStr)
91+
end
92+
if shouldIndent then
93+
return bracketColor .. "[\n" .. table.concat(result, ",\n") .. "\n" .. indentLine(depth - 1) .. bracketColor .. "]§r"
94+
else
95+
return bracketColor .. "[" .. "§r" .. table.concat(result, ", ") .. bracketColor .. "]§r"
96+
end
97+
else
98+
local keys = {}
99+
for k in pairs(value) do
100+
table.insert(keys, k)
101+
end
102+
103+
table.sort(keys, function(a, b)
104+
local ta, tb = typeRank(a), typeRank(b)
105+
if ta ~= tb then return ta < tb end
106+
return tostring(a) < tostring(b)
107+
end)
108+
109+
for _, k in ipairs(keys) do
110+
count = count + 1
111+
if count >= maxValues then
112+
table.insert(result, shouldIndent and indentLine(depth) .. "..." or "...")
113+
break
114+
end
115+
116+
local keyStr = applyColor(stringify(k), "§q")
117+
local valStr = stringify(value[k], depth + 1)
118+
local line = keyStr .. " = " .. valStr
119+
table.insert(result, shouldIndent and indentLine(depth) .. line or line)
120+
end
121+
122+
if shouldIndent then
123+
return bracketColor .. "{\n" .. table.concat(result, ",\n") .. "\n" .. indentLine(depth - 1) .. bracketColor .. "}§r"
124+
else
125+
return bracketColor .. "{" .. "§r" .. table.concat(result, ", ") .. bracketColor .. "}§r"
126+
end
127+
end
128+
end
129+
end
130+
131+
---Prints a string with formatting, accepts multiple arguments.
132+
---@param ... unknown
133+
function brint(...)
134+
local args = { ... }
135+
136+
if #args == 1 or args[1] == nil then
137+
print(stringify(args[1]))
138+
else
139+
print(stringify(args))
140+
end
141+
end

0 commit comments

Comments
 (0)