fyrkantis

Publisher

Aug 4th, 2025 (edited)
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1. PAGE_WIDTH = 25
  2. PAGE_HEIGHT = 21
  3.  
  4. strings = require "cc.strings"
  5.  
  6. root = fs.getDir(shell.getRunningProgram())
  7. function toAbsolutePath(relativePath) return root.."/"..relativePath end
  8.  
  9. args = {...}
  10.  
  11. local logo_file = fs.open("disk/logo.txt", "r")--args[2]), "r")
  12. logo = logo_file.readAll():gsub("\r", "")
  13. logo_file.close()
  14. text_lines = strings.wrap(logo, PAGE_WIDTH)
  15. local lines_len = #text_lines
  16. pages_len = 1
  17.  
  18. local date = args[1]
  19. if date == nil then
  20.     print("Usage: publish <date>")
  21.     return
  22. end
  23. local news_path = "disk/news/Nyheter_"..date..".txt"
  24. if not fs.exists(news_path) then
  25.     print("No file \"Nyheter_"..date..".txt\" exist in \"disk/news/\".")
  26.     return
  27. end
  28. local text_file = fs.open(news_path, "r")
  29. text = text_file.readAll():gsub("\r", "")
  30. text_file.close()
  31. for i, line in ipairs(strings.wrap(text, PAGE_WIDTH)) do
  32.     if i % (PAGE_HEIGHT - 1) == PAGE_HEIGHT - 2 then
  33.         if pages_len > 1 then
  34.             lines_len = lines_len + 1
  35.             text_lines[lines_len] = "   -------- "..pages_len.." --------"
  36.         end
  37.         pages_len = pages_len + 1
  38.     end
  39.     lines_len = lines_len + 1
  40.     text_lines[lines_len] = line
  41. end
  42.  
  43. local redstone_file = fs.open("redstone.txt", "r")
  44. redstone_side = redstone_file.readLine()
  45. redstone_file.close()
  46.  
  47. monitor = peripheral.find("monitor")
  48. printer = peripheral.find("printer")
  49. speaker = peripheral.find("speaker")
  50.  
  51. page_index = 1
  52. scroll = 0
  53.  
  54. function renderPage(monitor)
  55.     if monitor.setTextScale ~= nil then monitor.setTextScale(0.5) end
  56.     monitor.setBackgroundColor(colors.black)
  57.     monitor.setTextColor(colors.white)
  58.     monitor.clear()
  59.     monitor.setBackgroundColor(colors.white)
  60.     monitor.setTextColor(colors.black)
  61.     sleep(0)
  62.     local w, h = monitor.getSize()
  63.     local min_x = 1 + (w - PAGE_WIDTH) / 2
  64.     for y=1, PAGE_HEIGHT+2 do
  65.         monitor.setCursorPos(min_x - 1, y)
  66.         for _=1, PAGE_WIDTH + 2 do monitor.write(" ") end
  67.     end
  68.    
  69.     local page_start_index = (page_index - 1) * PAGE_HEIGHT
  70.     for y=1, PAGE_HEIGHT do
  71.         local line = text_lines[page_start_index + y]
  72.         if line ~= nil then
  73.             monitor.setCursorPos(min_x, y + 1)
  74.             monitor.write(line)
  75.         end
  76.     end
  77.     monitor.setBackgroundColor(colors.black)
  78.     monitor.setTextColor(colors.white)
  79. end
  80. function renderMonitorButtons(monitor, buttons)
  81.     monitor.setBackgroundColor(colors.black)
  82.     monitor.setTextColor(colors.yellow)
  83.     for _, button in ipairs(buttons) do
  84.         local x, y, _w, s, _f = unpack(button)
  85.         monitor.setCursorPos(x, y)
  86.         monitor.write(s)
  87.     end
  88.     monitor.setTextColor(colors.white)
  89. end
  90.  
  91. function renderButtons()
  92.     renderMonitorButtons(term, term_buttons)
  93.     if monitor ~= nil then renderMonitorButtons(monitor, monitor_buttons) end
  94. end
  95. function renderPages()
  96.     renderPage(term)
  97.     if monitor ~= nil then renderPage(monitor) end
  98. end
  99. function renderPagesAndButtons()
  100.     renderPages()
  101.     renderButtons()
  102. end
  103.  
  104. function previousPage()
  105.     if page_index > 1 then
  106.         page_index = page_index - 1
  107.         renderPagesAndButtons()
  108.     end
  109. end
  110. function nextPage()
  111.     if page_index < pages_len then
  112.         page_index = page_index + 1
  113.         renderPagesAndButtons()
  114.     end
  115. end
  116. function printStatus(status)
  117.     term.setCursorPos(1, 1)
  118.     term.write(status)
  119.     if monitor ~= nil then
  120.         monitor.setCursorPos(1, 1)
  121.         monitor.write(status)
  122.     end
  123. end
  124. function printPages()
  125.     renderPages()
  126.     printStatus("Printing...")
  127.     for i=1, pages_len do
  128.         local page_start_index = (i - 1) * PAGE_HEIGHT
  129.         if not printer.newPage() then
  130.             printStatus("Printing paused!")
  131.             sleep(1)
  132.             while not printer.newPage() do
  133.                 sleep(1)
  134.             end
  135.             printStatus("Printing...")
  136.         end
  137.         for y=1, PAGE_HEIGHT do
  138.             local line = text_lines[page_start_index + y]
  139.             if line ~= nil then
  140.                 printer.setCursorPos(1, y)
  141.                 printer.write(line)
  142.             end
  143.         end
  144.         if i == 1 then printer.setPageTitle("FF Nytt "..date)
  145.         else printer.setPageTitle("FF Nytt "..date.." (sida "..i..")") end
  146.         printer.endPage()
  147.         if i < pages_len then
  148.             if speaker ~= nil then speaker.playNote("chime", 3, 0) end
  149.             sleep(0.5)
  150.         end
  151.     end
  152.     if speaker ~= nil then speaker.playNote("chime", 3, 12) end
  153.     renderPagesAndButtons()
  154. end
  155. function publishPapers()
  156.     local addresses = {"MLC", "MHH", "MBT", "MMP", "MWH", "MDC", "NFR"}
  157.     local dl = peripheral.find("Create_DisplayLink")
  158.     for _, address in pairs(addresses) do
  159.         printStatus("...")
  160.         for _, side in pairs(redstone.getSides()) do
  161.             redstone.setOutput(side, false)
  162.         end
  163.         printPages()
  164.         printStatus("Packaging \""..address.."\"...")
  165.         dl.clear()
  166.         dl.setCursorPos(1, 1)
  167.         dl.write(address)
  168.         dl.update()
  169.         sleep(4)
  170.         for _, side in pairs(redstone.getSides()) do
  171.             redstone.setOutput(side, true)
  172.         end
  173.         if speaker ~= nil then speaker.playNote("bell", 3, 7) end
  174.         sleep(0)
  175.         if speaker ~= nil then speaker.playNote("bell", 3, 4) end
  176.     end
  177.     for _, side in pairs(redstone.getSides()) do
  178.         redstone.setOutput(side, false)
  179.     end
  180.     renderPagesAndButtons()
  181. end
  182.  
  183. local term_x, term_y = term.getSize()
  184. term_buttons = {
  185.     {1, term_y, 10, "[Previous]", previousPage},
  186.     {term_x - 5, term_y, 6, "[Next]", nextPage},
  187.     {term_x - 6, 1, 7, "[Print]", printPages},
  188.     {term_x - 8, 3, 9, "[Publish]", publishPapers},
  189. }
  190. if monitor ~= nil then
  191.     local mon_x, mon_y = monitor.getSize()
  192.     monitor_buttons = {
  193.         {1, mon_y, 10, "[Previous]", previousPage},
  194.         {mon_x - 5, mon_y, 6, "[Next]", nextPage},
  195.     }
  196. end
  197.  
  198. renderPagesAndButtons()
  199.  
  200. function detectButton(t_x, t_y, buttons)
  201.     for _, button in ipairs(buttons) do
  202.         local x, y, w, _s, f = unpack(button)
  203.         if t_y == y and t_x >= x and t_x < x + w then f() end
  204.     end
  205. end
  206.  
  207. while true do
  208.     local eventData = {os.pullEvent()}
  209.     local event = eventData[1]
  210.     if event == "mouse_click" then
  211.         local _event, _button, x, y = unpack(eventData)
  212.         detectButton(x, y, term_buttons)
  213.     elseif event == "monitor_touch" then
  214.         local _event, _side, x, y = unpack(eventData)
  215.         if monitor ~= nil then detectButton(x, y, monitor_buttons) end
  216.     elseif event == "key" then
  217.         local _event, key, held = unpack(eventData)
  218.         if not held then
  219.             if key == keys.left and page_index > 1 then previousPage()
  220.             elseif key == keys.right and page_index < pages_len then nextPage() end
  221.         end
  222.     end
  223. end
Advertisement
Add Comment
Please, Sign In to add comment