Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PAGE_WIDTH = 25
- PAGE_HEIGHT = 21
- strings = require "cc.strings"
- root = fs.getDir(shell.getRunningProgram())
- function toAbsolutePath(relativePath) return root.."/"..relativePath end
- args = {...}
- local logo_file = fs.open("disk/logo.txt", "r")--args[2]), "r")
- logo = logo_file.readAll():gsub("\r", "")
- logo_file.close()
- text_lines = strings.wrap(logo, PAGE_WIDTH)
- local lines_len = #text_lines
- pages_len = 1
- local date = args[1]
- if date == nil then
- print("Usage: publish <date>")
- return
- end
- local news_path = "disk/news/Nyheter_"..date..".txt"
- if not fs.exists(news_path) then
- print("No file \"Nyheter_"..date..".txt\" exist in \"disk/news/\".")
- return
- end
- local text_file = fs.open(news_path, "r")
- text = text_file.readAll():gsub("\r", "")
- text_file.close()
- for i, line in ipairs(strings.wrap(text, PAGE_WIDTH)) do
- if i % (PAGE_HEIGHT - 1) == PAGE_HEIGHT - 2 then
- if pages_len > 1 then
- lines_len = lines_len + 1
- text_lines[lines_len] = " -------- "..pages_len.." --------"
- end
- pages_len = pages_len + 1
- end
- lines_len = lines_len + 1
- text_lines[lines_len] = line
- end
- local redstone_file = fs.open("redstone.txt", "r")
- redstone_side = redstone_file.readLine()
- redstone_file.close()
- monitor = peripheral.find("monitor")
- printer = peripheral.find("printer")
- speaker = peripheral.find("speaker")
- page_index = 1
- scroll = 0
- function renderPage(monitor)
- if monitor.setTextScale ~= nil then monitor.setTextScale(0.5) end
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.clear()
- monitor.setBackgroundColor(colors.white)
- monitor.setTextColor(colors.black)
- sleep(0)
- local w, h = monitor.getSize()
- local min_x = 1 + (w - PAGE_WIDTH) / 2
- for y=1, PAGE_HEIGHT+2 do
- monitor.setCursorPos(min_x - 1, y)
- for _=1, PAGE_WIDTH + 2 do monitor.write(" ") end
- end
- local page_start_index = (page_index - 1) * PAGE_HEIGHT
- for y=1, PAGE_HEIGHT do
- local line = text_lines[page_start_index + y]
- if line ~= nil then
- monitor.setCursorPos(min_x, y + 1)
- monitor.write(line)
- end
- end
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- end
- function renderMonitorButtons(monitor, buttons)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.yellow)
- for _, button in ipairs(buttons) do
- local x, y, _w, s, _f = unpack(button)
- monitor.setCursorPos(x, y)
- monitor.write(s)
- end
- monitor.setTextColor(colors.white)
- end
- function renderButtons()
- renderMonitorButtons(term, term_buttons)
- if monitor ~= nil then renderMonitorButtons(monitor, monitor_buttons) end
- end
- function renderPages()
- renderPage(term)
- if monitor ~= nil then renderPage(monitor) end
- end
- function renderPagesAndButtons()
- renderPages()
- renderButtons()
- end
- function previousPage()
- if page_index > 1 then
- page_index = page_index - 1
- renderPagesAndButtons()
- end
- end
- function nextPage()
- if page_index < pages_len then
- page_index = page_index + 1
- renderPagesAndButtons()
- end
- end
- function printStatus(status)
- term.setCursorPos(1, 1)
- term.write(status)
- if monitor ~= nil then
- monitor.setCursorPos(1, 1)
- monitor.write(status)
- end
- end
- function printPages()
- renderPages()
- printStatus("Printing...")
- for i=1, pages_len do
- local page_start_index = (i - 1) * PAGE_HEIGHT
- if not printer.newPage() then
- printStatus("Printing paused!")
- sleep(1)
- while not printer.newPage() do
- sleep(1)
- end
- printStatus("Printing...")
- end
- for y=1, PAGE_HEIGHT do
- local line = text_lines[page_start_index + y]
- if line ~= nil then
- printer.setCursorPos(1, y)
- printer.write(line)
- end
- end
- if i == 1 then printer.setPageTitle("FF Nytt "..date)
- else printer.setPageTitle("FF Nytt "..date.." (sida "..i..")") end
- printer.endPage()
- if i < pages_len then
- if speaker ~= nil then speaker.playNote("chime", 3, 0) end
- sleep(0.5)
- end
- end
- if speaker ~= nil then speaker.playNote("chime", 3, 12) end
- renderPagesAndButtons()
- end
- function publishPapers()
- local addresses = {"MLC", "MHH", "MBT", "MMP", "MWH", "MDC", "NFR"}
- local dl = peripheral.find("Create_DisplayLink")
- for _, address in pairs(addresses) do
- printStatus("...")
- for _, side in pairs(redstone.getSides()) do
- redstone.setOutput(side, false)
- end
- printPages()
- printStatus("Packaging \""..address.."\"...")
- dl.clear()
- dl.setCursorPos(1, 1)
- dl.write(address)
- dl.update()
- sleep(4)
- for _, side in pairs(redstone.getSides()) do
- redstone.setOutput(side, true)
- end
- if speaker ~= nil then speaker.playNote("bell", 3, 7) end
- sleep(0)
- if speaker ~= nil then speaker.playNote("bell", 3, 4) end
- end
- for _, side in pairs(redstone.getSides()) do
- redstone.setOutput(side, false)
- end
- renderPagesAndButtons()
- end
- local term_x, term_y = term.getSize()
- term_buttons = {
- {1, term_y, 10, "[Previous]", previousPage},
- {term_x - 5, term_y, 6, "[Next]", nextPage},
- {term_x - 6, 1, 7, "[Print]", printPages},
- {term_x - 8, 3, 9, "[Publish]", publishPapers},
- }
- if monitor ~= nil then
- local mon_x, mon_y = monitor.getSize()
- monitor_buttons = {
- {1, mon_y, 10, "[Previous]", previousPage},
- {mon_x - 5, mon_y, 6, "[Next]", nextPage},
- }
- end
- renderPagesAndButtons()
- function detectButton(t_x, t_y, buttons)
- for _, button in ipairs(buttons) do
- local x, y, w, _s, f = unpack(button)
- if t_y == y and t_x >= x and t_x < x + w then f() end
- end
- end
- while true do
- local eventData = {os.pullEvent()}
- local event = eventData[1]
- if event == "mouse_click" then
- local _event, _button, x, y = unpack(eventData)
- detectButton(x, y, term_buttons)
- elseif event == "monitor_touch" then
- local _event, _side, x, y = unpack(eventData)
- if monitor ~= nil then detectButton(x, y, monitor_buttons) end
- elseif event == "key" then
- local _event, key, held = unpack(eventData)
- if not held then
- if key == keys.left and page_index > 1 then previousPage()
- elseif key == keys.right and page_index < pages_len then nextPage() end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment