From 8f77cd456cab4f33acb7b43b588b430e88b661a7 Mon Sep 17 00:00:00 2001 From: Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:29:54 +0200 Subject: [PATCH] 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. --- base.lua | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 104 insertions(+), 8 deletions(-) diff --git a/base.lua b/base.lua index 31711ce..d00badb 100644 --- a/base.lua +++ b/base.lua @@ -324,6 +324,89 @@ check.pattern = function(pos1, pos2, pattern) 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() if failed == 0 then io.close(io.open(minetest.get_worldpath() .. "/tests_ok", "w")) @@ -344,22 +427,35 @@ end local display_tests_summary = function() 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 if test.func == nil then - local s = ":"..test.mod..":---- " .. test.name .. " " - print(s .. string.rep("-", 60 - #s)) + local s = ":".. test.mod ..":---- " .. test.name + pprint.light_gray(":".. test.mod ..":").blue("---- " .. test.name) + pprint.blue(string.rep("-", 60 - #s).."\n") elseif test.result ~= nil then - print(string.format(":%s:%-60s %s", test.mod,test.name, test.result.ok and "pass" or "FAIL")) - if not test.result.ok then - print(" " .. test.result.err) + local s = ":"..test.mod..":" + local rest = s .. test.name + 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 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 + pprint.baby_blue(string.rep("-",60),"\n") 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 test_harness.dump = function(o)