Advertisement
melzneni

sys2_service_log

Feb 10th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.44 KB | None | 0 0
  1. --sys2_service_log
  2. os.loadAPI("sys/syslib")
  3.  
  4. --syslib end
  5. local lines = {}
  6. local monitor = peripheral.find("monitor")
  7. local width, height = monitor.getSize()
  8. local line = 1
  9.  
  10. function exLog(group, txt)
  11.     txt = group .. ": " .. txt
  12.     local pts = {}
  13.     while string.len(txt) > width do
  14.         table.insert(pts, string.sub(txt, 1, width))
  15.         txt = string.sub(txt, width + 1, -1)
  16.     end
  17.     table.insert(pts, txt)
  18.     for i, txt in pairs(pts) do
  19.         monitor.setCursorPos(1, line)
  20.         lines[line] = txt
  21.         monitor.write(lines[line])
  22.         if line > height then
  23.             for i = 1, line - 1 do
  24.                 lines[i] = lines[i + 1]
  25.             end
  26.             lines[line] = nil
  27.             monitor.clear()
  28.             for i = 1, line - 1 do
  29.                 monitor.setCursorPos(1, i)
  30.                 monitor.write(lines[i])
  31.             end
  32.         else
  33.             line = line + 1
  34.         end
  35.     end
  36. end
  37.  
  38. monitor.clear()
  39. exLog("root", "started system")
  40. while true do
  41.     local id, msg = syslib.receiveRednet()
  42.     local tag, pts = syslib.getMsgData(msg)
  43.     if tag == "@log" then
  44.         print("recv: ", msg)
  45.         if pts[1] == nil or pts[2] == nil then
  46.             if pts[1] == "clear" then
  47.                 monitor.clear()
  48.                 line=1
  49.             else print("unsupported log-data: " .. msg)
  50.             end
  51.         else
  52.             exLog(pts[1], pts[2])
  53.         end
  54.     end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement