Advertisement
toast_account_i_made

Untitled

Oct 27th, 2023 (edited)
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1.  
  2. -- PostMonitor module - Basic debugging / logging put on a monitor
  3. local displayModem = dbModem
  4.  
  5. function checkMonitor(monitorName)
  6.     if not displayModem.isPresentRemote(monitorName) then
  7.         print("Missing post monitor")
  8.    
  9.         print("Change 'displayMonitorName' to any of the following values if applicable")
  10.         for _, name in displayModem.getNamesRemote() do
  11.             print("   >'"..name.."'")
  12.         end
  13.         return
  14.     end
  15. end
  16.  
  17. function wrapPostMonitor(monitorName)
  18.  
  19.     local display = peripheral.wrap(monitorName)
  20.  
  21.     display.setBackgroundColour(0x8000)
  22.     display.clear()
  23.     display.setCursorPos(1,1)
  24.     display.setCursorBlink(true)
  25.     display.setTextScale(0.5)
  26.  
  27.     nextLine = function (monitor, screenHeight)
  28.         local newYPos = ({monitor.getCursorPos()})[2]
  29.         if (newYPos >= screenHeight) then
  30.             monitor.scroll(1)
  31.         else
  32.             newYPos = newYPos + 1
  33.         end
  34.         monitor.setCursorPos(1, newYPos)
  35.     end
  36.  
  37.     writeWrapped = function(postMonitor, log)
  38.         local maxLineLength, screenHeight  = postMonitor.display.getSize()
  39.         local lineLength = 0
  40.  
  41.         local words = {}
  42.         for word in log:gmatch("%S*%s?") do table.insert(words, word) end
  43.  
  44.         for index, word in pairs(words) do
  45.             lineLength = lineLength + word:len() +1
  46.             if (lineLength > maxLineLength) then
  47.                 nextLine(postMonitor.display, screenHeight)
  48.                 lineLength = 0
  49.             end
  50.             postMonitor.display.write(word .. (index ~= #words and " " or ""))
  51.         end
  52.  
  53.         nextLine(postMonitor.display, screenHeight)
  54.     end
  55.  
  56.     return {
  57.         display = display,
  58.         postLog = function(postMonitor, log)
  59.             postMonitor:resetColor()
  60.             writeWrapped(postMonitor, log)
  61.         end,
  62.         postError = function(postMonitor, log)
  63.             postMonitor:textColor(0x4000)
  64.             writeWrapped(postMonitor, log)
  65.         end,
  66.         resetColor = function(postMonitor)
  67.             postMonitor.display.setBackgroundColour(0x8000)
  68.             postMonitor.display.setTextColour(0x1)
  69.         end,
  70.         textColor = function(postMonitor, color)
  71.             postMonitor:resetColor()
  72.             postMonitor.display.setTextColour(color)
  73.         end,
  74.         paintColor = function(postMonitor, color)
  75.             postMonitor.display.setBackgroundColour(color)
  76.             postMonitor.display.setTextColour(color)
  77.         end,
  78.     }
  79. end
  80.  
  81. function safeWrapPostMonitor(monitorName)
  82.     checkMonitor(monitorName)
  83.     return wrapPostMonitor(monitorName)
  84. end
  85.  
  86. --End of PostMonitor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement