Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GLOBAL VARS
- mon = nil
- monSizeX = nil
- monSizeY = nil
- supportsColor = nil
- processActive = nil
- -- LOCAL VARS
- local xReset, yReset
- local xEnd, yEnd
- local function setTextColor(color)
- if supportsColor then
- mon.setTextColor(color)
- end
- end
- local function setBackgroundColor(color)
- if supportsColor then
- mon.setBackgroundColor(color)
- end
- end
- function init(side, scale)
- if side then
- mon = peripheral.wrap(side)
- else
- mon = term.current()
- end
- setBackgroundColor(colors.black)
- mon.clear()
- mon.setCursorPos(1, 1)
- mon.setCursorBlink(true)
- if mon.isColor == nil then
- supportsColor = false
- scale = scale or 1
- else
- supportsColor = mon.isColor()
- scale = scale or 0.5
- end
- mon.setTextScale(scale)
- monSizeX, monSizeY = mon.getSize()
- end
- function newline()
- local _, y = mon.getCursorPos()
- if y >= monSizeY then
- mon.scroll(1)
- mon.setCursorPos(1, y)
- yReset = yReset - 1
- if processActive then
- yEnd = yEnd - 1
- end
- else
- mon.setCursorPos(1, y + 1)
- end
- end
- function message(varMessage, status, color)
- status = status or "info"
- if processActive then
- mon.setCursorPos(xReset, yReset)
- newline()
- end
- mon.clearLine()
- local _, y = mon.getCursorPos()
- mon.setCursorPos(1, y)
- mon.write("[")
- if status == " ok " or status == "ok" then
- status = " ok "
- setTextColor(colors.lime)
- elseif status == "fail" then
- setTextColor(colors.red)
- elseif status == "warn" then
- setTextColor(colors.orange)
- elseif status == "info" then
- setTextColor(colors.lightBlue)
- elseif color then
- setTextColor(color)
- end
- mon.write(status)
- setTextColor(colors.white)
- mon.write("] " .. varMessage)
- if processActive then
- xReset, yReset = mon.getCursorPos()
- else
- newline()
- end
- end
- function log(varMessage, func, ...)
- local ans
- local status, ret
- local args
- local function dots()
- local i = 3
- while true do
- i = i + 1
- if i >= 4 then
- mon.setCursorPos(xEnd, yEnd)
- mon.write(" ")
- mon.setCursorPos(xEnd, yEnd)
- i = 0
- else
- mon.setCursorPos(xEnd + i - 1, yEnd)
- mon.write(".")
- end
- pcall(sleep, 0.5)
- end
- end
- local function wrapper()
- status, ret = pcall(func, unpack(args))
- if status and ret == nil then
- ans = 0
- else
- ans = ret
- end
- end
- if processActive then
- error("There is already a active logging process", 2)
- end
- arg["n"] = nil
- args = arg
- processActive = true
- xStart, yStart = mon.getCursorPos()
- mon.setCursorBlink(false)
- mon.write("[....] " .. varMessage .. " ")
- xEnd, yEnd = mon.getCursorPos()
- xReset = xEnd
- yReset = yEnd
- parallel.waitForAny(dots, wrapper)
- mon.setCursorPos(xEnd, yEnd)
- mon.write(" ")
- processActive = false
- if ans == 0 then
- message(varMessage, "ok")
- elseif ans == 1 then
- message(varMessage, "warn")
- else
- if status then
- message(varMessage .. " (" .. tostring(ans) .. ")", "fail")
- else
- message(varMessage .. " <ERROR>:", "fail")
- end
- end
- mon.setCursorPos(xReset, yReset)
- newline()
- if not status then
- if ans == nil then
- message("nil", "fail")
- else
- message(ans, "fail")
- end
- end
- mon.setCursorBlink(true)
- return ans
- end
- function close(program)
- program = program or "Program"
- local message = " " .. program .. " halted! "
- local _, y = mon.getCursorPos()
- mon.setCursorBlink(false)
- mon.write(string.rep("-", monSizeX))
- setTextColor(colors.red)
- mon.setCursorPos(math.floor((monSizeX - message:len()) / 2), y)
- mon.write(message)
- setTextColor(colors.white)
- end
Advertisement
Add Comment
Please, Sign In to add comment