Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Splits a string according to a pattern into a table
- local function split(str, pattern)
- local t = { }
- local fpat = "(.-)" .. pattern
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- --[[ Main code ]]--
- --The list of each page send by the server
- page = { }
- --The server ID
- local server = 382
- --The counts for the line and characters
- local charCount = 0, 0
- local charMax, lineMax = printer.getPageSize()
- --The wrapped peripheral
- local printer = nil
- --The number of pages in the current job
- local pageCount = 0
- --Whether or not we're still printing
- local stillPrinting = false
- --We locate the printer first
- for k,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "printer" then
- printer = peripheral.wrap(v)
- end
- end
- --Finishes the page, fires the new page down and keeps going
- function finishPage()
- printer.endPage()
- lineCount = 0
- wordCount = 0
- rs.setOutput("bottom", true)
- sleep(0.1)
- rs.setOutput("bottom", false)
- sleep(1)
- end
- --Starts a new page
- function startNewPage()
- rednet.send(server, string.char(6))
- printer.newPage()
- local receivedPage = false
- while not receivedPage do
- local id, msg = rednet.receive()
- if id == server and msg == string.char(2) then
- page = split(string.sub(msg, 2, 2), "\n")
- receivedPage = true
- elseif id == server and msg == string.char(19) then
- stillPrinting = false
- return
- end
- end
- printer.newPage()
- charMax, lineMax = printer.getPageSize()
- end
- --Checks to see if there are any issues with the printer
- function checkPrinterErrors()
- if printer.getInkLevel() < pageCount then
- rednet.send(server, string.char(23))
- return false
- elseif printer.getPaperLevel() < pageCount then
- rednet.send(server, string.char(25))
- return false
- else
- return true
- end
- end
- --This will print the word with a basic write command but updates
- --the char and line fields, and starts a new page if it needs to
- function safePrint(word)
- printer.write(word)
- charCount = charCount + #word
- if charCount >= charMax then
- nextLine()
- end
- end
- --Moves to the next line, or the next page if need be
- function nextLine()
- charCount = 0
- local _,oy = printer.getCursorPos()
- printer.setCursorPos(1,oy + 1)
- end
- --The pagination script.
- function printDocument()
- startNewPage()
- while stillPrinting do
- charMax, lineMax = printer.getPageSize()
- for i=1,#page do
- local word = page[i]
- --We replace tab characters with spaces, for convenience
- string.gsub(word, "\t", " ")
- --We split by space for each subword
- local subwords = split(word, " ")
- for j=1,#subwords do
- local subword = subwords[j]
- --If the word goes over the line, we just print as much of it as we can
- --until it's short enough to fit on one line
- while #subword > charMax do
- safePrint(subword)
- subword = string.sub(subword, charMax - charCount + 1)
- end
- --If the word is blocked by other stuff on the line, we just go to the next line
- if #subword + charCount > charMax then
- nextLine()
- end
- --We then print our word (or what's left of it)
- safePrint(subword)
- --Finally, if there's room a space character is added
- if charCount < charMax then
- safePrint(" ")
- end
- end
- nextLine()
- end
- finishPage()
- startNewPage()
- end
- end
- function listenForJobs
- while true do
- local id,msg = rednet.receive()
- print(msg)
- local runnable = false
- if id == server then
- if msg == string.char(17) then
- pageCount = tonumber(string.sub(msg, 2, 2))
- currentPage = 1
- stillPrinting = true
- --Perform check to ensure printer is able to function
- --Or send error code
- runnable = checkPrinterErrors()
- elseif msg == string.char(5)
- rednet.send(server, string.char(16)..printer.getPaperLevel().." "..printer.getInkLevels())
- end
- end
- if runnable then
- printDocument()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement