BrainStone

Lua Logger

Dec 5th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.89 KB | None | 0 0
  1. -- GLOBAL VARS
  2.  
  3. mon = nil
  4. monSizeX = nil
  5. monSizeY = nil
  6. supportsColor = nil
  7. processActive = nil
  8.  
  9. -- LOCAL VARS
  10.  
  11. local xReset, yReset
  12. local xEnd, yEnd
  13.  
  14. local function setTextColor(color)
  15.   if supportsColor then
  16.     mon.setTextColor(color)
  17.   end
  18. end
  19.  
  20. local function setBackgroundColor(color)
  21.   if supportsColor then
  22.     mon.setBackgroundColor(color)
  23.   end
  24. end
  25.  
  26. function init(side, scale)
  27.   if side then
  28.     mon = peripheral.wrap(side)
  29.   else
  30.     mon = term.current()
  31.   end
  32.  
  33.   setBackgroundColor(colors.black)
  34.   mon.clear()
  35.   mon.setCursorPos(1, 1)
  36.   mon.setCursorBlink(true)
  37.  
  38.   if mon.isColor == nil then
  39.     supportsColor = false
  40.     scale = scale or 1
  41.   else
  42.     supportsColor = mon.isColor()
  43.     scale = scale or 0.5
  44.   end
  45.  
  46.   mon.setTextScale(scale)
  47.  
  48.   monSizeX, monSizeY = mon.getSize()
  49. end
  50.  
  51. function newline()
  52.   local _, y = mon.getCursorPos()
  53.  
  54.   if y >= monSizeY then
  55.     mon.scroll(1)
  56.    
  57.     mon.setCursorPos(1, y)
  58.    
  59.     yReset = yReset - 1
  60.      
  61.     if processActive then
  62.       yEnd = yEnd - 1
  63.     end
  64.   else
  65.     mon.setCursorPos(1, y + 1)
  66.   end
  67. end
  68.  
  69. function message(varMessage, status, color)
  70.   status = status or "info"
  71.  
  72.   if processActive then
  73.     mon.setCursorPos(xReset, yReset)
  74.     newline()
  75.   end
  76.  
  77.   mon.clearLine()
  78.  
  79.   local _, y = mon.getCursorPos()
  80.   mon.setCursorPos(1, y)
  81.  
  82.   mon.write("[")
  83.  
  84.   if status == " ok " or status == "ok" then
  85.     status = " ok "
  86.    
  87.     setTextColor(colors.lime)
  88.   elseif status == "fail" then
  89.     setTextColor(colors.red)
  90.   elseif status == "warn" then
  91.     setTextColor(colors.orange)
  92.   elseif status == "info" then
  93.     setTextColor(colors.lightBlue)
  94.   elseif color then
  95.     setTextColor(color)
  96.   end
  97.  
  98.   mon.write(status)
  99.    
  100.   setTextColor(colors.white)
  101.  
  102.   mon.write("] " .. varMessage)
  103.  
  104.   if processActive then
  105.     xReset, yReset = mon.getCursorPos()
  106.   else
  107.     newline()
  108.   end
  109. end
  110.  
  111. function log(varMessage, func, ...)
  112.   local ans
  113.   local status, ret
  114.   local args
  115.  
  116.   local function dots()
  117.     local i = 3
  118.    
  119.     while true do
  120.       i = i + 1
  121.      
  122.       if i >= 4 then
  123.         mon.setCursorPos(xEnd, yEnd)
  124.         mon.write("   ")
  125.         mon.setCursorPos(xEnd, yEnd)
  126.        
  127.         i = 0
  128.       else
  129.         mon.setCursorPos(xEnd + i - 1, yEnd)
  130.         mon.write(".")
  131.       end
  132.      
  133.       pcall(sleep, 0.5)
  134.     end
  135.   end
  136.  
  137.   local function wrapper()
  138.     status, ret = pcall(func, unpack(args))
  139.    
  140.     if status and ret == nil then
  141.       ans = 0
  142.     else
  143.       ans = ret
  144.     end
  145.   end
  146.  
  147.   if processActive then
  148.     error("There is already a active logging process", 2)
  149.   end
  150.  
  151.   arg["n"] = nil
  152.   args = arg
  153.   processActive = true
  154.   xStart, yStart = mon.getCursorPos()
  155.   mon.setCursorBlink(false)
  156.  
  157.   mon.write("[....] " .. varMessage .. " ")
  158.  
  159.   xEnd, yEnd = mon.getCursorPos()
  160.   xReset = xEnd
  161.   yReset = yEnd
  162.  
  163.   parallel.waitForAny(dots, wrapper)
  164.  
  165.   mon.setCursorPos(xEnd, yEnd)
  166.   mon.write("   ")
  167.   processActive = false
  168.  
  169.   if ans == 0 then
  170.     message(varMessage, "ok")
  171.   elseif ans == 1 then
  172.     message(varMessage, "warn")
  173.   else
  174.     if status then
  175.       message(varMessage .. " (" .. tostring(ans) .. ")", "fail")
  176.     else
  177.       message(varMessage .. " <ERROR>:", "fail")
  178.     end
  179.   end
  180.  
  181.   mon.setCursorPos(xReset, yReset)
  182.   newline()
  183.  
  184.   if not status then
  185.     if ans == nil then
  186.       message("nil", "fail")
  187.     else
  188.       message(ans, "fail")
  189.     end
  190.   end
  191.  
  192.   mon.setCursorBlink(true)
  193.  
  194.   return ans
  195. end
  196.  
  197. function close(program)
  198.   program = program or "Program"
  199.  
  200.   local message = " " .. program .. " halted! "
  201.   local _, y = mon.getCursorPos()
  202.  
  203.   mon.setCursorBlink(false)
  204.   mon.write(string.rep("-", monSizeX))
  205.  
  206.   setTextColor(colors.red)
  207.   mon.setCursorPos(math.floor((monSizeX - message:len()) / 2), y)
  208.   mon.write(message)
  209.   setTextColor(colors.white)
  210. end
Advertisement
Add Comment
Please, Sign In to add comment