feat(colors): add color management for console output

Add color management functionality sourced from 'lua-chroma' library, introducing various color escape sequences for enhanced console output formatting. Implemented a `pprint` function for formatted printing with color support, enabling structured and visually enhanced display in the console for tests and results.

This commit enhances readability and visual differentiation in test summary displays through color-coded outputs, highlighting test status and errors effectively.
This commit is contained in:
Yves-Marie Haussonne 2024-10-04 01:29:54 +02:00
parent f99c66daed
commit 8f77cd456c

112
base.lua
View File

@ -324,6 +324,89 @@ check.pattern = function(pos1, pos2, pattern)
end end
end end
---------------------------------------
--- Colors Management
--- from https://github.com/ldrumm/lua-chroma
--------------------------------------
rawset(_G, "pprint", setmetatable({
escapes = {
clear = "\027[0m",
red = "\027[31m",
green = "\027[32m",
orange = "\027[33m",
navy = "\027[34m",
magenta = "\027[35m",
cyan = "\027[36m",
gray = "\027[90m",
grey = "\027[90m",
light_gray = "\027[37m",
light_grey = "\027[37m",
peach = "\027[91m",
light_green = "\027[92m",
yellow = "\027[93m",
blue = "\027[94m",
pink = "\027[95m",
baby_blue = "\027[96m",
highlight = {
red = "\027[41m",
green = "\027[42m",
orange = "\027[43m",
navy = "\027[44m",
magenta = "\027[45m",
cyan = "\027[46m",
gray = "\027[47m",
grey = "\027[47m",
light_gray = "\027[100m",
light_grey = "\027[100m",
peach = "\027[101m",
light_green = "\027[102m",
yellow = "\027[103m",
blue = "\027[104m",
pink = "\027[105m",
baby_blue = "\027[106m",
},
strikethrough = "\027[9m",
underline = "\027[4m",
bold = "\027[1m",
},
_sequence = '',
_highlight = false,
print = io.write
},
{
__call = function(self, ...) return io.write(...) end,
__index = function(self, index)
local esc = self._highlight and rawget(self, 'escapes').highlight[index]
or rawget(self, 'escapes')[index]
self._highlight = index == 'highlight'
if esc ~= nil then
if type(esc) == 'string' then
self._sequence = self._sequence .. esc
end
return setmetatable({}, {
__call = function(proxy, ...)
if self._sequence then io.write(self._sequence) end
self.print(...)
self._sequence = ''
io.write(rawget(self,'escapes').clear)
return self
end,
__index = function(proxy, k)
return self[k]
end,
})
else
return rawget(self, index)
end
end,
}))
--------------------------------------
local request_shutdown = function() local request_shutdown = function()
if failed == 0 then if failed == 0 then
io.close(io.open(minetest.get_worldpath() .. "/tests_ok", "w")) io.close(io.open(minetest.get_worldpath() .. "/tests_ok", "w"))
@ -344,22 +427,35 @@ end
local display_tests_summary = function() local display_tests_summary = function()
for mod, tests_list in pairs(tests_by_mod) do for mod, tests_list in pairs(tests_by_mod) do
print(string.format("%#70s", mod)) pprint.baby_blue(string.format("%#60s\n", mod))
for _, test in ipairs(tests_list) do for _, test in ipairs(tests_list) do
if test.func == nil then if test.func == nil then
local s = ":"..test.mod..":---- " .. test.name .. " " local s = ":".. test.mod ..":---- " .. test.name
print(s .. string.rep("-", 60 - #s)) pprint.light_gray(":".. test.mod ..":").blue("---- " .. test.name)
pprint.blue(string.rep("-", 60 - #s).."\n")
elseif test.result ~= nil then elseif test.result ~= nil then
print(string.format(":%s:%-60s %s", test.mod,test.name, test.result.ok and "pass" or "FAIL")) local s = ":"..test.mod..":"
if not test.result.ok then local rest = s .. test.name
print(" " .. test.result.err) pprint.light_gray(s)
pprint(" ")
pprint(test.name)
pprint(string.rep(" ", 60 - #rest))
if test.result.ok then pprint.green("pass") else pprint.red("FAIL") end
pprint("\n")
if not test.result.ok and test.result.err then
pprint.yellow(" " .. test.result.err .. "\n")
end end
else else
print(string.format(":%s:%-60s %s", test.mod, test.name, "No result")) pprint.light_gray(string.format(":%s:%-60s %s\n", test.mod, test.name, "No result"))
end end
end end
pprint.baby_blue(string.rep("-",60),"\n")
end end
print("Tests done, " .. failed .. " tests failed.") print(string.rep("-",60))
pprint.bold("Tests done, ")
if failed == 0 then pprint.bold.green(failed) else pprint.bold.red(failed) end
pprint.bold(" tests failed.\n")
print(string.rep("-",60))
end end
test_harness.dump = function(o) test_harness.dump = function(o)