CelticCoder

monitorDisplay

Dec 13th, 2024 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. local textScale = 1
  2.  
  3. local textLines = {
  4.     "",
  5.     "Text",
  6.     ""
  7. }
  8.  
  9. -- Get all peripherals connected to the computer
  10. local monitors = {}
  11. for _, name in ipairs(peripheral.getNames()) do
  12.     if peripheral.getType(name) == "monitor" then
  13.         table.insert(monitors, peripheral.wrap(name))
  14.     end
  15. end
  16.  
  17. -- Ensure exactly two monitors are detected
  18. if #monitors ~= 2 then
  19.     print("This script requires exactly two monitors connected to the computer.")
  20.     return
  21. end
  22.  
  23. -- Determine the monitors' sizes
  24. local mon1, mon2 = monitors[1], monitors[2]
  25. local width1, height1 = mon1.getSize()
  26. local width2, height2 = mon2.getSize()
  27.  
  28. if height1 ~= height2 then
  29.     print("Both monitors must have the same height.")
  30.     return
  31. end
  32.  
  33. mon1.setTextScale(textScale)
  34. mon2.setTextScale(textScale)
  35.  
  36. -- Clear both monitors
  37. mon1.clear()
  38. mon2.clear()
  39.  
  40. -- Display an array of text on both monitors
  41. local function displayTextOnMonitors(lines)
  42.     local maxWidth = math.min(width1, width2)
  43.     local yStart = math.floor((height1 - #lines) / 2)
  44.  
  45.     for i, line in ipairs(lines) do
  46.         local y = yStart + i - 1
  47.         if y > 0 and y <= height1 then
  48.             local truncatedLine = line:sub(1, maxWidth)
  49.             mon1.setCursorPos(math.floor((maxWidth - #truncatedLine) / 2), y)
  50.             mon1.write(truncatedLine)
  51.             mon2.setCursorPos(math.floor((maxWidth - #truncatedLine) / 2), y)
  52.             mon2.write(truncatedLine)
  53.         end
  54.     end
  55. end
  56.  
  57. displayTextOnMonitors(textLines)
Advertisement
Add Comment
Please, Sign In to add comment