Advertisement
nitrogenfingers

printclient

Feb 9th, 2013
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. --Splits a string according to a pattern into a table              
  2. local function split(str, pattern)
  3.   local t = { }
  4.   local fpat = "(.-)" .. pattern
  5.   local last_end = 1
  6.   local s, e, cap = str:find(fpat, 1)
  7.   while s do
  8.     if s ~= 1 or cap ~= "" then
  9.       table.insert(t,cap)
  10.     end
  11.     last_end = e+1
  12.     s, e, cap = str:find(fpat, last_end)
  13.   end
  14.   if last_end <= #str then
  15.     cap = str:sub(last_end)
  16.     table.insert(t, cap)
  17.   end
  18.   return t
  19. end
  20.  
  21. --[[ Main code ]]--
  22.  
  23. --The list of each page send by the server
  24. page = { }
  25. --The server ID
  26. local server = 382
  27. --The counts for the line and characters
  28. local charCount = 0, 0
  29. local charMax, lineMax = printer.getPageSize()
  30. --The wrapped peripheral
  31. local printer = nil
  32. --The number of pages in the current job
  33. local pageCount = 0
  34. --Whether or not we're still printing
  35. local stillPrinting = false
  36.  
  37. --We locate the printer first
  38. for k,v in pairs(rs.getSides()) do
  39.     if peripheral.getType(v) == "printer" then
  40.         printer = peripheral.wrap(v)
  41.     end
  42. end
  43.  
  44.  
  45. --Finishes the page, fires the new page down and keeps going
  46. function finishPage()
  47.     printer.endPage()
  48.     lineCount = 0
  49.     wordCount = 0
  50.    
  51.     rs.setOutput("bottom", true)
  52.     sleep(0.1)
  53.     rs.setOutput("bottom", false)
  54.     sleep(1)
  55. end
  56.  
  57. --Starts a new page
  58. function startNewPage()
  59.     rednet.send(server, string.char(6))
  60.     printer.newPage()
  61.     local receivedPage = false
  62.     while not receivedPage do
  63.         local id, msg = rednet.receive()
  64.         if id == server and msg == string.char(2) then
  65.             page = split(string.sub(msg, 2, 2), "\n")
  66.             receivedPage = true
  67.         elseif id == server and msg == string.char(19) then
  68.             stillPrinting = false
  69.             return
  70.         end
  71.     end
  72.  
  73.     printer.newPage()
  74.     charMax, lineMax = printer.getPageSize()
  75. end
  76.  
  77. --Checks to see if there are any issues with the printer
  78. function checkPrinterErrors()
  79.     if printer.getInkLevel() < pageCount then
  80.         rednet.send(server, string.char(23))
  81.         return false
  82.     elseif printer.getPaperLevel() < pageCount then
  83.         rednet.send(server, string.char(25))
  84.         return false
  85.     else
  86.         return true
  87.     end
  88. end
  89.  
  90. --This will print the word with a basic write command but updates
  91. --the char and line fields, and starts a new page if it needs to
  92. function safePrint(word)
  93.     printer.write(word)
  94.     charCount = charCount + #word
  95.     if charCount >= charMax then
  96.         nextLine()
  97.     end
  98. end
  99.  
  100. --Moves to the next line, or the next page if need be
  101. function nextLine()
  102.     charCount = 0
  103.     local _,oy = printer.getCursorPos()
  104.     printer.setCursorPos(1,oy + 1)
  105. end
  106.  
  107. --The pagination script.
  108.  
  109. function printDocument()
  110.     startNewPage()
  111.     while stillPrinting do
  112.         charMax, lineMax = printer.getPageSize()
  113.         for i=1,#page do
  114.             local word = page[i]
  115.             --We replace tab characters with spaces, for convenience
  116.             string.gsub(word, "\t", " ")
  117.             --We split by space for each subword
  118.             local subwords = split(word, " ")
  119.             for j=1,#subwords do
  120.                 local subword = subwords[j]
  121.        
  122.                 --If the word goes over the line, we just print as much of it as we can
  123.                     --until it's short enough to fit on one line
  124.                 while #subword > charMax do
  125.                     safePrint(subword)
  126.                     subword = string.sub(subword, charMax - charCount + 1)
  127.                 end
  128.        
  129.                 --If the word is blocked by other stuff on the line, we just go to the next line
  130.                 if #subword + charCount > charMax then
  131.                     nextLine()
  132.                 end
  133.        
  134.                 --We then print our word (or what's left of it)
  135.                 safePrint(subword)
  136.            
  137.                 --Finally, if there's room a space character is added
  138.                 if charCount < charMax then
  139.                     safePrint(" ")
  140.                 end
  141.             end
  142.             nextLine()
  143.         end
  144.         finishPage()
  145.         startNewPage()
  146.     end
  147. end
  148.  
  149. function listenForJobs
  150.     while true do
  151.         local id,msg = rednet.receive()
  152.         print(msg)
  153.         local runnable = false
  154.         if id == server then
  155.             if msg == string.char(17) then
  156.                 pageCount = tonumber(string.sub(msg, 2, 2))
  157.                 currentPage = 1
  158.                 stillPrinting = true
  159.                 --Perform check to ensure printer is able to function
  160.                 --Or send error code
  161.                 runnable = checkPrinterErrors()
  162.             elseif msg == string.char(5)
  163.                 rednet.send(server, string.char(16)..printer.getPaperLevel().." "..printer.getInkLevels())
  164.             end
  165.         end
  166.        
  167.         if runnable then
  168.             printDocument()
  169.         end
  170.     end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement