Advertisement
nitrogenfingers

paginate

Feb 9th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 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. textStore = { }
  24. --The number of characters on the present line, and the number of lines on the present page
  25. local charCount, lineCount = 0, 0
  26. --The maximum char and line count
  27. local charMax, lineMax = 25, 21
  28. --The command line arguments
  29. local tArgs = {...}
  30. --The wrapped peripheral
  31. local printer = nil
  32.  
  33. --We locate the printer first
  34. for k,v in pairs(rs.getSides()) do
  35.     if peripheral.getType(v) == "printer" then
  36.         printer = peripheral.wrap(v)
  37.     end
  38. end
  39. if not printer then
  40.     print("No printer attached!")
  41.     return
  42. end
  43.  
  44. --Catching missing arguments
  45. if #tArgs == 0 then
  46.     print("Usage: printpage <filename>")
  47.     return
  48. end
  49.  
  50. local filePath = shell.resolve(".").."/"..tArgs[1]
  51.  
  52. --Files use relative pathing, catching missing files
  53. if not fs.exists(filePath) then
  54.     print("File not found")
  55.     return
  56. end
  57. --Catches trying to print entire directories
  58. if fs.isDir(filePath) then
  59.     print("Cannot print directory")
  60.     return
  61. end
  62.  
  63. --We rip the file open and cut it up here
  64. local file = fs.open(filePath, "r")
  65. textStore = split(file.readAll(), "\n")
  66. file.close()
  67.  
  68. --Finishes the page, fires the new page down and keeps going
  69. function finishPage()
  70.     printer.endPage()
  71.     lineCount = 0
  72.     wordCount = 0
  73.    
  74.     rs.setOutput("bottom", true)
  75.     sleep(0.1)
  76.     rs.setOutput("bottom", false)
  77.    
  78.     printer.newPage()
  79.     charMax, lineMax = printer.getPageSize()
  80. end
  81.  
  82. --This will print the word with a basic write command but updates
  83. --the char and line fields, and starts a new page if it needs to
  84. function safePrint(word)
  85.     printer.write(word)
  86.     charCount = charCount + #word
  87.     if charCount >= charMax then
  88.         nextLine()
  89.     end
  90. end
  91.  
  92. --Moves to the next line, or the next page if need be
  93. function nextLine()
  94.     charCount = 0
  95.     lineCount = lineCount + 1
  96.     if lineCount >= lineMax then
  97.         finishPage()
  98.     else
  99.         local _,oy = printer.getCursorPos()
  100.         printer.setCursorPos(1,oy + 1)
  101.     end
  102. end
  103.  
  104. --The pagination script.
  105. printer.newPage()
  106. charMax, lineMax = printer.getPageSize()
  107. for i=1,#textStore do
  108.     local word = textStore[i]
  109.     --We replace tab characters with spaces, for convenience
  110.     string.gsub(word, "\t", " ")
  111.     --We split by space for each subword
  112.     local subwords = split(word, " ")
  113.     for j=1,#subwords do
  114.         local subword = subwords[j]
  115.        
  116.         --If the word goes over the line, we just print as much of it as we can
  117.         --until it's short enough to fit on one line
  118.         while #subword > charMax do
  119.             safePrint(subword)
  120.             subword = string.sub(subword, charMax - charCount)
  121.         end
  122.    
  123.         --If the word is blocked by other stuff on the line, we just go to the next line
  124.         if #subword + charCount > charMax then
  125.             nextLine()
  126.         end
  127.    
  128.         --We then print our word (or what's left of it)
  129.         safePrint(subword)
  130.        
  131.         --Finally, if there's room a space character is added
  132.         if charCount < charMax then
  133.             safePrint(" ")
  134.         end
  135.         os.pullEvent("key")
  136.     end
  137.     nextLine()
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement