diff --git a/src/backend.lua b/src/backend.lua index 805f0e0..0ec5387 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -1,9 +1,8 @@ local kristly = require("/src/libs/kristly") local utils = require("/src/utils") -local loggerImport = require("/src/libs/logger") -local logger = loggerImport:new({ debugging = true }) +local logger = require("/src/libs/logger"):new({ debugging = true }) -logger:info("Starting Kristify! Thanks for choosing us.") +logger:info("Starting Kristify! Thanks for choosing Kristify. <3") logger:debug("Debugging mode is enabled!") local config = require("/data/config") diff --git a/src/libs/logger.lua b/src/libs/logger.lua index 389fe4c..fc876b1 100644 --- a/src/libs/logger.lua +++ b/src/libs/logger.lua @@ -8,64 +8,37 @@ return o end --- Inspiration by "Mads" --- Inspiration source: http://www.computercraft.info/forums2/index.php?/topic/11771-print-coloured-text-easily/ -local function printWithFormat(...) - local s = "&1" - for k, v in ipairs(arg) do - s = s .. v +-- Inspiration by "Kingdaro" +-- Inspiration source: https://www.computercraft.info/forums2/index.php?/topic/6201-changing-colours-of-text-in-a-single-printwrite/ +local function colorPrint(...) + local curColor + for i = 1, #arg do + if type(arg[i]) == 'number' then + curColor = arg[i] + else + if curColor then term.setTextColor(curColor) end + write(arg[i]) + end end - s = s .. "&0" - - local fields = {} - local lastcolor, lastpos = "0", 0 - for pos, clr in s:gmatch "()&(%x)" do - table.insert(fields, { s:sub(lastpos + 2, pos - 1), lastcolor }) - lastcolor, lastpos = clr, pos - end - - for i = 2, #fields do - term.setTextColor(2 ^ (tonumber(fields[i][2], 16))) - io.write(fields[i][1]) - end -end - -local function fixup() - print(" ") + print() term.setTextColor(colors.white) - term.setBackgroundColor(colors.black) end function logger:info(...) - local args = { ... } - - printWithFormat("&8[&dINFO&8] &0", table.unpack(args)) - fixup() + colorPrint(colors.lightGray, "[", colors.green, "INFO", colors.lightGray, "] ", colors.white, table.unpack(arg)) end function logger:warn(...) - local args = { ... } - - printWithFormat("&8[&1WARN&8] &0", table.unpack(args)) - fixup() + colorPrint(colors.lightGray, "[", colors.orange, "WARN", colors.lightGray, "] ", colors.white, table.unpack(arg)) end function logger:error(...) - local args = { ... } - - printWithFormat("&8[&eERROR&8] &e", table.unpack(args)) - fixup() + colorPrint(colors.lightGray, "[", colors.red, "ERROR", colors.lightGray, "] ", colors.red, table.unpack(arg)) end function logger:debug(...) - if self.debugging == nil or self.debugging == false then - return - end - - local args = { ... } - - printWithFormat("&8[&7DEBUG&8] &0", table.unpack(args)) - fixup() + if self.debugging ~= true then return end + colorPrint(colors.lightGray, "[", colors.gray, "DEBUG", colors.lightGray, "] ", colors.gray, table.unpack(arg)) end return logger